搜索
简帛阁>技术文章>SpringMVC接收页面表单参数

SpringMVC接收页面表单参数

一个普通的表单。

表单的代码如下:

<%@ 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">  
    //添加用户  
    function addUser() {  
        var form = document.forms[0];  
        form.action = "${pageContext.request.contextPath}/user/addUser1";  
        //form.action = "${pageContext.request.contextPath}/user/addUser2";  
        //form.action = "${pageContext.request.contextPath}/user/addUser3";  
        form.method = "post";  
        form.submit();  
    }  
</script>  
  
</head>  
<body>  
    <form>  
        <table>  
            <tr>  
                <td>账号</td>  
                <td>  
                    <input type="text" name="userName">  
                </td>  
            </tr>  
            <tr>  
                <td>密码</td>  
                <td>  
                    <input type="password" name="password">  
                </td>  
            </tr>  
            <tr>  
                <td> </td>  
                <td>  
                    <input type="button" value="提交" onclick="addUser()">  
                </td>  
            </tr>  
        </table>  
    </form>  
</body>  
</html>  

1、直接把表单的参数写在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、通过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、通过一个bean来接收

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

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来封装接收的参数

@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数据接收

<%@ 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>  

依然可以使用bean来接收json数据

@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、使用jQuery的serializeArray() 方法序列化表单元素

    如果表单元素很多,手工拼装成json数据非常麻烦,可以使用jQuery提供的serializeArray()方法序列化表单元素,返回json数据结构数据。

<%@ 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> 

依然可以使用bean来接收json数据:

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

 

1、直接把表单参数写在Controller相应的方法的形参中@RequestMapping("/addUser1")publicStringaddUser1(StringuserName,Stri
一个普通的表单表单的代码如下:<%@pagelanguagejavacontentTypetext/html;charsetUTF8pageEncodingUTF8%><!DOC
目录接收文件流上传和表单参数JAVA服务端代码HTML页面代码SpringMVC接收文件上传,并对文件做处理springmvc配置controller代码如下接收文件流上传和表单参数在SpringM
这是前辈们对于SpringMVC接收表单数据记录下来的总结经验:SpringMVC接收页面表单参数springmvc请求参数获取的几种方法下面是我自己在使用时发现的,前辈们没有记录的细节和注意点:使
简单的做法是:前台使用表单提交,注意methodpost,后台在controller的方法参数里面写:你要用来接收的实体对象,不要加@RequestBody代码示例:@RequestMapping(v
直接在方法上使用List<Bean>报不能实例化错误!必须将List<Bean>放在某个对象里作为属性方可接收。具体见如下例子前端代码<!DOCTYPEhtml>&
接收参数的方式:1HttpServletRequest方式接收publicModelAndViewtest1(HttpServletRequestreq){StringuserNamereqgetP
目录表单提交表单提交二后台代码接收方式一:含有单个的checkbox参数接收后台代码接收方式二:表单提交此处的表单时使用JSONstringify()函数将数组转换成json类型提交后台,后台使用@
实体类@DatapublicclassDept{<!>privateIntegerdeptno;privateStringdname;privateStringloc;}后台控制器@Con
&x1f345;程序员小王的博客:程序员小王的博客&x1f345;欢迎点赞&x1f44d;收藏⭐留言&x1f4dd;&x1f345;如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去