您好,登錄后才能下訂單哦!
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; } //getter,setter方法。。. }
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"; }
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。