Controller accepted common method request parameters:
1, to accept the request by Bean parameters:
Create a POJO entity class
Create pojo package entity classes and create UserForm in the packet, the code:
package pojo; public class UserForm { private String uname;//与请求参数名称相同 private String upass; private String reupass; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } public String getReupass() { return reupass; } public void setReupass(String reupass) { this.reupass = reupass; } }
Create a controller class, and create a controller class IndexController UserController in the controller package
Wherein the role of the individual plain @RequestMapping annotation is to be understood that the method or class annotated with a "path" name to find when HTML, JSP (view) sends a request to the controller.
IndexController Code:
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller//在这里@Controller相当于@Controller(“indexController”) @RequestMapping("/index") public class IndexController { @RequestMapping("/login") public String login() { return "login";//跳转到“/WEB-INF/jsp/login.jsp” } @RequestMapping("/register") public String register() { return "register"; } }
UserController Code:
package controller; import javax.servlet.http.HttpSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import pojo.UserForm; import service.UserService; @Controller//在这里@Controller相当于@Controller(“userController”) @RequestMapping ( "/ User" ) public class the UserController { // get a log object is used, when a print information such that the information can be printed mark that class Private static Final the Log Logger = LogFactory.getLog (the UserController. Class ); // service dependency injection property to that userService @Autowired public UserService that userService; / ** * log processing * / @RequestMapping ( "/ Login" ) public String Login (the UserForm User, the HttpSession the session, the Model Model) { IF (that userService .login (User)) { session.setAttribute ( "U", User); logger.info ( "Success" ); return "main"; // login is successful, jump to main.jsp } the else { logger.info ( "failure" ); model.addAttribute ( "MessageError", " username or password error " ); return " Login " ; } } / ** * registration processing * / @RequestMapping ( " / Register " ) public String Register (@ModelAttribute (" user " ) the UserForm user) { IF (that userService.register(user)){ Logger.info ( "success"); Return "Login"; // registration is successful, jump to the login.jsp } the else { logger.info ( "failure" ); // Use @ModelAttribute ( "user") and model.addAttribute ( "user", user ) the same function // use EL expression $ {user.uname} is taken ModelAttribute register.jsp uname values on page return "Register"; // return register.jsp } } }
Create a page view, with a view to better understanding @RequestMapping notes the role which this knowledge about the content of the core code <body> tag:
Creating register.jsp Code:
<%@ 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"> <style type="text/css"> .textSize{ width: 100pt ; height : 15Pt } </ style > < title > registration screen </ title > < Script type = "text / JavaScript" > // registration check entry function allIsNull () { var name = document.registForm.uname .Value; var pwd = document.registForm.upass.value; var repwd = document.registForm.reupass.value; IF (name == "" ) { Alert ( " Please enter the name! " ); document.registForm.uname.focus (); return to false ; } IF (pwd == "" ) { Alert ( " Please enter your password! " ); document.registForm.upass.focus (); return to false ; } IF (repwd == "" ) { Alert ( " Please enter the confirmation password! " ); document.registForm.reupass.focus (); return to false ; } IF(pwd!=repwd){ alert("2次密码不一致,请重新输入!"); document.registForm.upass.value=""; document.registForm.reupass.value=""; document.registForm.upass.focus(); return false; } document.registForm.submit(); return true; } </script> </head> <body> <form action="${pageContext.request.contextPath }/user/register" method="post" name="registForm"> <table border=1 bgcolor="lightblue" align="center"> <tr> <td>姓名:</td> <td> <input class="textSize" type="text" name="uname" value="${user.uname }"/> </td> </tr> <tr> <td>密码:</td> <td><input class="textSize" type="password" maxlength="20" name="upass"/></td> </tr> <tr> <td>确认密码:</td> <td><input class="textSize" type="password" maxlength="20" name="reupass"/></td> </tr> <tr> <td colspan="2" align="center"><input type="button" value="注册" onclick="allIsNull()"/></td> </tr> </table> </form> </body> </html>
Creating login.jsp Code:
<%@ 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>后台登录</title> <style type="text/css"> table{ text-align: center; } .textSize{ width: 120px; height: 25px; } * { margin: 0px; padding: 0px; } body { font-family: Arial, Helvetica, sans-serif; font-size: 12px; margin: 10px 10px auto; background-image: url(${pageContext.request.contextPath }/images/bb.jpg); } </style> <script type="text/javascript"> //确定按钮 function gogo(){ document.forms[0].submit(); } //取消按钮 function cancel(){ document.forms[0].action = ""; } </script> </head> <body> <form action="${pageContext.request.contextPath }/user/login" method="post"> <table> <tr> <td colspan="2"><img src="${pageContext.request.contextPath }/images/login.gif"></td> </tr> <tr> <td>姓名:</td> <td><input type="text" name="uname" class="textSize"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="upass" class="textSize"></td> </tr> <tr> <td colspan="2"> <input type="image" src="${pageContext.request.contextPath }/images/ok.gif" onclick="gogo()" > <input type="image" src="${pageContext.request.contextPath }/images/cancel.gif" onclick="cancel()" > </td> </tr> </table> ${messageError } </form> </body> </html>
2, by forming the processing method of receiving a request parameter reference
UserConttroller control register method is about the class code changes as follows:
@RequestMapping ( "/ Register" ) / ** * requests received by the count parameter parameter parameter parameter name identical to the name of the request * / public String Register (the uname String, String uPASS, the Model Model) { IF (that userService. the equals (the uname) && userService.equals (uPASS)) { logger.info ( "success" ); return "Login"; // registration is successful, jump to the login.jsp } the else { logger.info ( "failure" ); return "Register"; // return register.jsp } }
3, accepting the request by HttpServetRequest parameter (recommended):
UserConttroller control register method is about the class code changes as follows:
@RequestMapping("/register") public String register(HttpServletRequest request,Model model) { String uname = request.getParameter("uname"); String upass = request.getParameter("upass"); if(userService.equals(uname)&&userService.equals(upass)){ logger.info("成功"); return "login";//注册成功,跳转到login.jsp }else{ logger.info("失败"); return "register";return register.jsp// } }
Other @ PathVariable, @ RequestParam and so much to say, the same principle.