1. Directly write the parameters of the form 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);
p>return "/user/success";
}
3. Receive through a bean
1) Create a parameter with the form Corresponding bean
public class User { private String userName; private String password; public String getUserName() { return userName; }
//getter, setter method. .
. }
2) Use this bean to encapsulate the received parameters
@RequestMapping("/addUser3")
public String addUser3(User user) {< /p>
System.out.println("userName is:"+user.getUserName());
System.out.println("password is:"+user.getPassword()) ;
return "/user/success";
}
4. Receive via json data
<%@ page language= "java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
< /p>$(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",
< p>url:"${pageContext.request.contextPath}/user/addUser4",data:user,
success:function(data){
alert("Success");
},
error:function(e) {
alert("Error: "+e);
p>}
});
});
});
You can still use beans to receive json data
@ RequestMapping("/addUser4")
public String addUser4(User user) {
System.out.println("userName is:"+user.getUserName());
p>System.out.println("password is:"+user.getPassword());
return "/user/success";
}
5. Use jQuery’s serializeArray() method to serialize form elements
If there are many form elements, it is very troublesome to manually assemble json data. You can use the serializeArray() method provided by jQuery to serialize the form elements. , returns json data structure data.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
< p>< html>
$(document).ready(function(){
$ ("#button_submit").click(function(){
//Serialize form elements and return json data
var params = $("#userForm").serializeArray( );
//You can also save elements outside the form in the name value format
//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);
}
});
});
});
< body>