首页 > 代码库 > SpringMVC接收页面表单参数

SpringMVC接收页面表单参数

一个一般的表单。


表单的代码如下:


[java] view plaincopy

<%@ page language="java" contentType="text/html; charset=UTF-8"  

    pageEncoding="UTF-8"%>  

 

 

 


1、直接把表单的参数写在Controller相应的办法的号码大全形参中

[java] view plaincopy

@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、经过HttpServletRequest接纳关键词挖掘工具

[java] view plaincopy

@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、经过一个bean来接纳


    1)树立一个和表单中参数对应的bean


[java] view plaincopy

public class User {  

      

    private String userName;  

      

    private String password;  

  

    public String getUserName() {  

        return userName;  

    }  

  

    public void setUserName(String userName) {  

        this.userName = userName;  

    }  

  

    public String getPassword() {  

        return password;  

    }  

  

    public void setPassword(String password) {  

        this.password = password;  

    }  

}  


2)用这个bean来封装接纳的参数

[java] view plaincopy

@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、经过json数据接纳

[java] view plaincopy

<%@ page language="java" contentType="text/html; charset=UTF-8"  

    pageEncoding="UTF-8"%>  

 


仍然能够运用bean来接纳json数据

[java] view plaincopy

@RequestMapping("/addUser4")  

    public String addUser4(User user) {  

        System.out.println("userName is:"+user.getUserName());  

        System.out.println("password is:"+user.getPassword());  

        return "/user/success";  

-        indexRead arguments from command-line "http://www.shoudashou.com"

-        indexRead arguments from command-line "http://www.4lunwen.cn"

-        indexRead arguments from command-line "http://www.zx1234.cn"

-        indexRead arguments from command-line "http://www.penbar.cn"

-        indexRead arguments from command-line "http://www.whathappy.cn"

-        indexRead arguments from command-line "http://www.lunjin.net"

-        indexRead arguments from command-line "http://www.ssstyle.cn"

-        indexRead arguments from command-line "http://www.91fish.cn"

-        indexRead arguments from command-line "http://www.fanselang.com"

    }  

5、运用jQuery的serializeArray() 办法序列化表单元素


    假如表单元素很多,手艺拼装成json数据十分费事,能够运用jQuery供给的serializeArray()办法序列化表单元素,回来json数据结构数据。


[java] view plaincopy

<%@ page language="java" contentType="text/html; charset=UTF-8"  

    pageEncoding="UTF-8"%>  

 

 

 

仍然能够运用bean来接纳json数据:

[java] view plaincopy

@RequestMapping("/addUser5")  

    public String addUser5(User user) {  

        System.out.println("userName is:"+user.getUserName());  

        System.out.println("password is:"+user.getPassword());  

        return "/user/success";  

    }