SpringMVC provides examples of passing values, forwarding, and redirection, and springmvc redirection.

Source: Internet
Author: User

SpringMVC provides examples of passing values, forwarding, and redirection, and springmvc redirection.

  1. Parameter Value of the exercise receiving page
    1. Use request
    2. Use the @ RequestParam Annotation
    3. Use object
  2. Exercise outgoing data to the page
    1. Use ModelAndView object
    2. Use ModelMap object
    3. Use @ ModelAttribute Annotation
  3. Exercise session
    1. You can directly declare HttpSession on Controller method parameters.
  4. Exercise redirection
    1. Use RedirectView
    2. 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;    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.