首页 > 代码库 > SpringMVC接收页面表单参数-java-电脑编程网

SpringMVC接收页面表单参数-java-电脑编程网

<style type="text/css" id="wiz_custom_css">body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;}html, body{ }h1 { font-size:1.5em; font-weight:bold;}h2 { font-size:1.4em; font-weight:bold;}h3 { font-size:1.3em; font-weight:bold;}h4 { font-size:1.2em; font-weight:bold;}h5 { font-size:1.1em; font-weight:bold;}h6 { font-size:1.0em; font-weight:bold;}img { border:0; max-width: 100%;}blockquote { margin-top:0px; margin-bottom:0px;}table { border-collapse:collapse; border:1px solid #bbbbbb;}td { border-collapse:collapse; border:1px solid #bbbbbb;}</style>SpringMVC接收页面表单参数-java-电脑编程网

使用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="http://www.mamicode.com/${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>&nbsp;</td>                <td>                    <input type="button" id="button_submit" value="http://www.mamicode.com/提交">                </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";    }


来自为知笔记(Wiz)


SpringMVC接收页面表单参数-java-电脑编程网