标签:src document servlet doctype 后台 rip 3.1 int class
以下几种方式只有在已搭好的SpringMVC环境中,才能执行成功!
首先,写一个登陆页面和一个Bean类
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>yyx博客后台登录</title> </head> <body> <form action="${pageContext.request.contextPath}/user/login.do" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="userName"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="userPwd"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登陆"></td> </tr> </table> </form> </body> </html>
package com.yyx.model; public class User { private String userName; private String userPwd; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } }
1、直接把表单的参数写在Controller相应的方法的形参中
package com.yyx.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login") public String loginByUser(String userName, String userPwd) { System.out.println("userName is:" + userName); System.out.println("userPwd is:" + userPwd); return "success"; } }
控制器中方法的形参String userName,String userPwd名称必须和登陆页面的name="userName",name="userPwd"相同
2.通过HttpServletRequest接收
package com.yyx.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login") public String loginByUser(HttpServletRequest request) { String userName=request.getParameter("userName"); String userPwd=request.getParameter("userPwd"); System.out.println("userName is:" + userName); System.out.println("userPwd is:" + userPwd); return "success"; } }
控制器中String userName=request.getParameter("userName");String userPwd=request.getParameter("userPwd");的参数名称必须和登陆页面的name="userName",name="userPwd"相同
3.通过一个bean来接收
package com.yyx.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.yyx.model.User; @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login") public String loginByUser(User user) { System.out.println("userName is:" + user.getUserName()); System.out.println("userPwd is:" + user.getUserPwd()); return "success"; } }
登陆页面的name="userName",name="userPwd"必须和Bean类的属性名称相同
4.通过json数据接收
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-3.1.1.min.js"></script> <title>yyx博客后台登录</title> <script type="text/javascript"> $(document).ready(function() { $("#button_submit").click(function() { var name = $("#userName").val(); var pwd = $("#userPwd").val(); var user = { userName : name, userPwd : pwd };//拼装成json格式 $.ajax({ type : "POST", url : "${pageContext.request.contextPath}/user/login.do", 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"></td> </tr> <tr> <td>密码:</td> <td><input type="password" id="userPwd"></td> </tr> <tr> <td colspan="2"><input type="button" id="button_submit" value="提交"></td> </tr> </table> </form> </body> </html>
package com.yyx.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.yyx.model.User; @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login") public String loginByUser(User user) { System.out.println("userName is:" + user.getUserName()); System.out.println("userPwd is:" + user.getUserPwd()); return "success"; } }
var user={userName:name,userPwd:pwd};//拼装成json格式 语句中userName,userPwd必须和Bean类中属性名称相同
标签:src document servlet doctype 后台 rip 3.1 int class
原文地址:http://www.cnblogs.com/yyxyqh/p/7749990.html