@ModelAttribute spring mvc portlets和ajax表单提交 [英] @ModelAttribute spring mvc portlets and ajax form submit

查看:147
本文介绍了@ModelAttribute spring mvc portlets和ajax表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉我的英语不好。我脑子里有很多事情让我感到困惑。我想将表单提交的值作为@ModelAttribute来处理,这首先使我感到困惑。
不使用@ModelAttribute我很好并且一切正常。

Sorry for my poor english. I have many things going on in my mind which are confusing me literally. I want to handle the form submitted values as @ModelAttribute which is confusing me in the first place. Without using the @ModelAttribute I am good and have everything working perfectly.

我的要求是在portlet中处理ajax表单提交和spring mvc 3.0注释

My requirements are to handle ajax form submit in portlets and spring mvc 3.0 annotations

表格我有

<portlet:resourceURL var="userURL" id="addUser" escapeXml="false" />

<form id="<portlet:namespace />User>
<table>
<tr><td>First Name: </td>
    <td><input type="text" name="fname"></td></tr>
<tr><td>Last Name: </td>
    <td><input type="text" name="lname"></td></tr>
<tr><td>Address 1: </td>
    <td><input type="text" name="address_1"></td></tr>
<tr><td>Address 2: </td>
    <td><input type="text" name="address_2"></td></tr>
<tr><td>Zipcode </td>
    <td><input type="text" name="zipcode"></td></tr>
<tr><td>&nbsp; </td>
    <td><button id="submit">Submit</td></tr>

</table>
</form>

我使用以下jQuery将表格作为ajax电话提交

I use the following jQuery to submit the form as an ajax call

$('#submit').on('click',function() {
   var fname = $('#fname').val();
   var lname = $('#lname').val();
   var address_1 = $('#address_1').val();
   var address_2 = $('#address_2').val();
   var zipcode = $('#zipcode').val();

   $.ajax({
      type: "POST"
      url: "<c:out value="${userURL}" />"
      data: {fname: fname, lname: lname, address_1: address_1, address_2: address_2,         zipcode: zipcode }
      success: function(data) {
                   if(data == "success") {
                      $('#showError').hide();
                   } else {
                      $('#showError').show();
                   }
      } 
   })

});

我有以下控制器来处理ajax调用

I have the following controller to handle the ajax call

@Controller
@RequestMapping("VIEW")
public class UserController {

       @ResourceMapping("addUser")
       public String addUser(ResourceRequest request, ResourceResponse response) {
            String fName = request.getParameter("fname");
            String lName = request.getParameter("lname");
            String address_1 = request.getParameter("address_1");
            String address_2 = request.getParameter("address_2");
            String zipcode = request.getParameter("zipcode");

            // I do the processing of the form and add the user attributes to the database.
       }

}

我创建了一个User类,我想使用@ModelAttribute来设置/获取值。我已经通过许多链接试图找出使用它。其中一个示例使用表单taglib。我有jQuery将表单作为ajax调用提交,我不确定是否将表单更改为此模式会破坏我的代码。

I have created a User class and I want to use @ModelAttribute to set/get the values. I have gone through many links trying to figure out using it. One of the examples out uses the form taglib. I have jQuery to submit the form as an ajax call and I am not sure if I change the form to this pattern it will break my code.

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<form:form method="post" action="addContact.html">

    <table>
    <tr>
        <td><form:label path="firstname">First Name</form:label></td>
        <td><form:input path="firstname" /></td> 
    </tr>
    <tr>
        <td><form:label path="lastname">Last Name</form:label></td>
        <td><form:input path="lastname" /></td>
    </tr>
    <tr>
        <td><form:label path="lastname">Email</form:label></td>
        <td><form:input path="email" /></td>
    </tr>
    <tr>
        <td><form:label path="lastname">Telephone</form:label></td>
        <td><form:input path="telephone" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Add Contact"/>
        </td>
    </tr>
</table>    

</form:form>

感谢您的帮助!

推荐答案

我假设您可能正在使用名为User.class的Model类
使用 @SessionAttributes(types = User.class)注释您的控制器类
使用 @ModelAttribute(< model_attribute_name>)注释您的资源映射方法用户用户

I assume you may be using a Model class named User.class Annotate your controller class with @SessionAttributes(types = User.class) Annotate your resource mapping method with @ModelAttribute("<model_attribute_name>") User user

在你的JSP中,

<form:form method="post" commandName="<model_attribute_name>" action="addContact.html">

    <table>
    <tr>
        <td><form:label >First Name</form:label></td>
        <td><form:input path="firstName" /></td> 
    </tr>
....

在您的资源映射方法中,您可以使用以下方式访问表单数据以下代码

In your resource mapping method you could access the form data by using following code

 @ResourceMapping("addUser")
       public String addUser(ModeAttribute("<model_attribute_name>") User user ,ResourceRequest request, ResourceResponse response) {
            String fName = user.getFirstName();
           ...

            // I do the processing of the form and add the user attributes to the database.
       }

这篇关于@ModelAttribute spring mvc portlets和ajax表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆