springMvc requests a jump and a method for passing values

  • 2020-06-07 04:33:26
  • OfStack

3 ways to skip the forword page:

1. Use serlvet


/**
   *  use forward jump , Pass the basic type parameters to the page 
   *    Pay attention to :
   *     1. use servlet native API Request scope 
   *     
   */
  @RequestMapping("/test")
  public String test(HttpServletRequest request,HttpServletResponse response){
    String name = " A small 3";
    request.setAttribute("name",name);
    return "/back/attr";
  }

2. Use Model objects


/**
   *  use forward jump , Pass the basic type parameters to the page 
   *    Pay attention to :
   *     1. use springmvc  encapsulated Model object ( The bottom is request scope )
   */
  @RequestMapping("/test1")
  public String test1(Model model){
    String name = " A small 4";
    model.addAttribute("name", name);
    return "back/attr";
    
  }

3. Use ModelAndView


/**
   *  use modelAndView
   *    Matters needing attention 
   *     modelAndView The data in the object can only be ModelAndView View fetch for an object 
   */
  @RequestMapping("/test2")
  public ModelAndView test2(ModelAndView modelAndView){
    String name = " A small 5";
    modelAndView.setViewName("back/attr");
    modelAndView.addObject("name", name);
    return modelAndView;
     
  }

It can also be done with new 1 ModelAndView object


@RequestMapping("/test3")
  public ModelAndView test3(){
    String name = " A small 6";
    return new ModelAndView("back/attr", "name", name);
  }

forword Jump to Controller

Jump to a method in the same class


/**
   *  use forword Jump to something in the same class 1 methods 
   *  Note: 
   *     1. You don't have to add the class @RequestMapping The value of the 
   */
  @RequestMapping("/test00")
  public String test00(){
    return "forward:test1";
  }

Methods to jump to different classes:


/**
   *  use forword Jump to one of different classes 1 methods 
   *  Note: 
   *     1. I need to add the class @RequestMapping Value of: for example   : /hello
   */
  @RequestMapping("/test01")
  public String test01(){
    return "forward:/hello/test";
  }

redirect Jump to page:

Using servlet


/**
   *  use redirect jump   Pass data to the page 
   *     1. use Servlet native API Session ServletContext
   */
  
  @RequestMapping("/test4")
  public String test4(HttpServletRequest request,HttpSession session){
    String name = " Xiao-xia zhang ";
    session.setAttribute("name", name);
    return "redirect:/back/attr.jsp";
  }

Using ModelAndView


/**
   *  use redirect jump   Pass data to the page 
   *     1.. use ModelAndView object  modelAndView Object to model The data in ? The form is spliced into the address bar   You can use ${param.key} accept 
   */
  @RequestMapping("/test5")
  public ModelAndView test5(){
    return new ModelAndView("redirect:/back/attr.jsp","name"," Zhang zhang ");
  }

redirect Jump to Controller:

Jumping to both class and class methods requires adding @RequestMapping to the class to keep the test code from sticking out


Related articles: