Spring receive request parameters:
1. Use HttpServletRequest to get it
@RequestMapping("/login.do") public String login(HttpServletRequest request){ String name = request.getParameter("name") String pass = request.getParameter("pass") }2. Spring will automatically inject form parameters into method parameters, which is consistent with the form name attribute. Same as Struts2
@RequestMapping("/login.do") public String login(HttpServletRequest request, String name, @RequestParam("pass")String password) // The form attribute is pass, and the variable password is received { syso(name); syso(password) }3. Automatically inject Bean attributes
<form action="login.do"> Username: <input name="name"/> Password: <input name="pass"/> <input type="submit" value="Login"> </form> //Embroidered User class Public class User{ private String name; private String pass; } @RequestMapping("/login.do") public String login(User user) { syso(user.getName()); syso(user.getPass()); } Pass values to the page:
After the Controller component is processed, pass the value to the jsp page.
1. Use HttpServletRequest and Session and then setAttribute(), just like in Servlet
2. Use ModelAndView object
3. Use ModelMap object
4. Use @ModelAttribute annotation
Model data will be used to pass the value into success.jsp using the Attribute of HttpServletRequest
@RequestMapping("/login.do") public ModelAndView login(String name,String pass){ User user = userService.login(name,pwd); Map<String,Object> data = new HashMap<String,Object>(); data.put("user",user); return new ModelAndView("success",data); } Example of using ModelMap parameter object:
ModelMap data will be passed into success.jsp using the Attribute of HttpServletRequest
@RequestMapping("/login.do") public String login(String name,String pass,ModelMap model){ User user = userService.login(name,pwd); model.addAttribute("user",user); model.put("name",name); return "success"; } Example using @ModelAttribute
Use on the parameter part of the Controller method or on the Bean property method
@ModelAttribute data will use the Attribute of HttpServletRequest to pass the value into success.jsp
@RequestMapping("/login.do") public String login(@ModelAttribute("user") User user){ //TODO return "success"; } @ModelAttribute("name") public String getName(){ return name; } Session storage:
You can use the getSession() method of HttpServletReequest
@RequestMapping("/login.do") public String login(String name,String pwd ModelMap model,HttpServletRequest request){ User user = serService.login(name,pwd); HttpSession session = request.getSession(); session.setAttribute("user",user); model.addAttribute("user",user); return "success"; } Spring MVC uses forwarding to locate views by default. If you want to use redirection, you can do the following.
1. Use RedirectView
2. Use redirect: prefix
public ModelAndView login(){ RedirectView view = new RedirectView("regirst.do"); return new ModelAndView(view); }Or use the following methods, commonly used methods in work:
public String login(){ //TODO return "redirect:regirst.do"; }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.