SpringMVC provides examples of passing values, forwarding, and redirection, and springmvc redirection.
- Parameter Value of the exercise receiving page
- Use request
- Use the @ RequestParam Annotation
- Use object
- Exercise outgoing data to the page
- Use ModelAndView object
- Use ModelMap object
- Use @ ModelAttribute Annotation
- Exercise session
- You can directly declare HttpSession on Controller method parameters.
- Exercise redirection
- Use RedirectView
- Use redirect:
Package web; import java. util. hashMap; import java. util. map; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; import org. springframework. stereotype. controller; import org. springframework. ui. modelMap; import org. springframework. web. bind. annotation. modelAttribute; import org. springframework. web. bind. annotation. requestMapping; im Port org. springframework. web. bind. annotation. requestParam; import org. springframework. web. servlet. modelAndView; import org. springframework. web. servlet. view. redirectView; import entity. user; // non-annotation method // public class HelloController implements Controller {// public ModelAndView handleRequest (HttpServletRequest request, // HttpServletResponse response) throws Exception {// System. out. println ("Hello, Cont Roller. "); // return new ModelAndView (" jsp/hello "); //} @ Controller @ RequestMapping ("/demo ") public class HelloController {private Integer age = 22; @ RequestMapping ("hello. do ") public ModelAndView hello (HttpServletRequest request, HttpServletResponse response) throws Exception {return new ModelAndView (" jsp/hello ");} /*** test the request receiving parameter */@ RequestMapping ("test1.do") public ModelAndView test1 (HttpSe RvletRequest req) {String userName = req. getParameter ("userName"); String password = req. getParameter ("password"); System. out. println (userName); System. out. println (password); return new ModelAndView ("jsp/hello ");} /*** the sping test automatically injects the form parameters into the method parameters */@ RequestMapping ("test2.do") public ModelAndView test2 (String userName, @ RequestParam ("password ") string pwd) {System. out. println (userName + "," + pwd); retur N new ModelAndView ("jsp/hello");}/*** test object receiving parameter */@ RequestMapping ("test3.do") public ModelAndView test3 (User user) {System. out. println (user); return new ModelAndView ("jsp/hello ");} /*** use the Attribute of HttpServletRequest in the ModelAndView parameter to pass it to the jsp page */@ RequestMapping ("test4.do") public ModelAndView test4 (User user User) {Map <String, object> data = new HashMap <String, Object> (); data. put ("user", user ); Return new ModelAndView ("jsp/hello", data);}/*** use ModelMap to transmit the Attribute of HttpServletRequest In the parameter to the jsp page */@ RequestMapping ("test5.do ") public ModelAndView test5 (User user, ModelMap modelMap) {modelMap. put ("user", user); return new ModelAndView ("jsp/hello ");} /*** use the Attribute of HttpServletRequest in ModelAttribute to pass it to the jsp page * use the */@ RequestMapping ("test6.do") public ModelAnd In the parameter section of contoeller or the bean Attribute Method View test6 (@ ModelAttribute ("user") User user) {return new ModelAndView ("jsp/hello") ;}@ ModelAttribute ("age") public Integer getAge () {return age;}/*** session storage can use the getSession method of HttpServletRequest to access */@ RequestMapping ("test7.do") public ModelAndView test7 (HttpServletRequest req) {HttpSession session = req. getSession (); session. setAttribute ("salary", 6000.0); return new ModelAndView ("jsp/hello ") ;} // Return String forwarding @ RequestMapping ("/test8.do") public String test8 (User user, ModelMap model) {model. addattriing ("user", user); return "jsp/hello";}/*** error page */@ RequestMapping ("test9.do") public String test9 () {return "error/error";}/*** use RedirectView to redirect */@ RequestMapping ("test10") public ModelAndView test10 (User user) {if (user. getUserName (). equals ("123") {return new ModelAndView ("jsp/h Ello "); // test10.do forwarding} else {return new ModelAndView (new RedirectView (" test9.do "); // test9.do? Age = 22 redirection}/*** use redirect redirection */@ RequestMapping ("test11") public String test11 (User user) {if (User. getUserName (). equals ("123") {return "jsp/hello";} else {return "redirect: test9.do ";}}}
User entity
package com.tarena.entity;import java.io.Serializable;public class User implements Serializable { private Integer id; private String userName; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}