1. Write the parameters of the form directly in the formal parameters of the corresponding method of the Controller
@RequestMapping("/addUser1")
public String addUser1(String userName,String password) {
System.out.println("userName is:"+userName);
System.out.println("password is:"+password);
return "/user/success";
}
2. Receive through HttpServletRequest
@RequestMapping("/addUser2")
public String addUser2(HttpServletRequest request) {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
System.out.println("userName is:"+userName);
System.out.println("password is:"+password);
return "/user/success";
}
3. Receive through a bean
@RequestMapping("/addUser3")
public String addUser3(User user) {
System.out.println("userName is:"+user.getUserName());
System.out.println("password is:"+user.getPassword());
return "/user/success";
}
4. Receive through json data
$(document).ready(function(){
$("#button_submit").click(function(){
var name = $("#userName").val();
var pass = $("#password").val();
var user = {userName:name,password:pass};//Assemble into json format
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/user/addUser4",
data:user,
success:function(data){
alert("success");
},
error:function(e) {
alert("Error: "+e);
}
});
});
}); You can still use beans to receive json data
5. Use jQuery's serializeArray() method to serialize form elements
$(document).ready(function(){
$("#button_submit").click(function(){
//Serialize form elements and return json data
var params = $("#userForm").serializeArray();
//You can also store elements outside the form in the format of name value
//params.push({name:"hello",value:"man"});
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/user/addUser5",
data:params,
success:function(data){
alert("success");
},
error:function(e) {
alert("Error: "+e);
}
});
});
}); You can still use beans to receive json data: