Home  >  Article  >  Java  >  How Spring receives form parameters in java

How Spring receives form parameters in java

Y2J
Y2JOriginal
2017-05-11 09:40:391797browse

This article mainly introduces the relevant knowledge of SpringMVC receiving page form parameters, which has a good reference value. Let’s take a look at it with the editor

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);
  return "/user/success";
 }

3. Receive through a bean

##1) Create a bean# corresponding to the parameters in the form ##

public class User { private String userName; private String password; public String getUserName() { return userName; }
  //getter,setter方法。。. }

2) Use this bean to encapsulate the received parameters

  @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. Through

jsonData reception

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add User</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resource/script/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
  $("#button_submit").click(function(){
   var name = $("#userName").val();
   var pass = $("#password").val();
   var user = {userName:name,password:pass};//拼装成json格式
   $.ajax({
    type:"POST",
    url:"${pageContext.request.contextPath}/user/addUser4",
    data:user,
    success:function(data){
     alert("成功");
    },
    error:function(e) {
     alert("出错:"+e);
    }
   });
  });
 });
</script>
</head>
<body>
 <form>
  <table>
   <tr>
    <td>账号</td>
    <td>
     <input type="text" id="userName" name="userName">
    </td>
   </tr>
   <tr>
    <td>密码</td>
    <td>
     <input type="password" id="password" name="password">
    </td>
   </tr>
   <tr>
    <td> </td>
    <td>
     <input type="button" id="button_submit" value="提交">
    </td>
   </tr>
  </table>
 </form>
</body>
</html>
You can still use beans to receive json data

@RequestMapping("/addUser4")
 public String addUser4(User user) {
  System.out.println("userName is:"+user.getUserName());
  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 and return the json data structure data.

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add User</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resource/script/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
  $("#button_submit").click(function(){
   //序列化表单元素,返回json数据
   var params = $("#userForm").serializeArray();
   //也可以把表单之外的元素按照name value的格式存进来
   //params.push({name:"hello",value:"man"});
   $.ajax({
    type:"POST",
    url:"${pageContext.request.contextPath}/user/addUser5",
    data:params,
    success:function(data){
     alert("成功");
    },
    error:function(e) {
     alert("出错:"+e);
    }
   });
  });
 });
</script>
</head>
<body>
 <form id="userForm">
  <table>
   <tr>
    <td>账号</td>
    <td>
     <input type="text" id="userName" name="userName">
    </td>
   </tr>
   <tr>
    <td>密码</td>
    <td>
     <input type="password" id="password" name="password">
    </td>
   </tr>
   <tr>
    <td> </td>
    <td>
     <input type="button" id="button_submit" value="提交">
    </td>
   </tr>
  </table>
 </form>
</body>
</html>

You can still use beans to receive json data:

@RequestMapping("/addUser5")
 public String addUser5(User user) {
  System.out.println("userName is:"+user.getUserName());
  System.out.println("password is:"+user.getPassword());
  return "/user/success";
 }

[Related recommendations]

1.

Java Free Video Tutorial

2.

JAVA Tutorial Manual

3.

Comprehensive analysis of Java annotations

The above is the detailed content of How Spring receives form parameters in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn