SpringMVC Response Control

Posted by FarhanKhalaf on Thu, 09 Sep 2021 18:46:21 +0200

1. SpringMVC Response Control

1.1 Default Page

  • You can configure a default jump page for Controller after processing is complete

  • In the configuration file of mvc

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/pages/" />
            <property name="suffix" value=".jsp"/>
    </bean>
    
    1. If the returned String string is specified in the processor, the actual jump path is obtained by prefixing and suffixing the string before and after it
    2. If the returned String string is not specified in the processor (that is, the return value is of void type), then the path to @RequestMapping("/response1") jumping to the processor is defaulted as the return value to access the address where the prefix is added, for example, the access LRU for this instance is: /pages/response2.jsp

1.2 Page Jump

1.2.1 Request Forwarding (default)

  • The return value is a string, meaning the address of the forwarded page
  • The string begins with "forward:"
    @RequestMapping("/response1")
    public String response1(){
        System.out.println("response1 mvc controller running ....");
        return "forward:success.jsp";
    }

1.2.2 Redirection

  • The return value is a string, meaning the address that needs to be redirected to the page
  • String begins with "redirect:"
 @RequestMapping("/response1")
    public String response2(){
        System.out.println("response1 mvc controller running ....");
        return "redirect:success.jsp";
    }

1.3 Page jumps with data

1.3.1 HttpServletRequest type transfer data

Add a parameter of type HttpServletRequest to the processor to assign data to the processor for parameter passing

    @RequestMapping("/response3")
    public String response3(HttpServletRequest request){
        System.out.println("response3 mvc controller running ....");
        request.setAttribute("username","zhangsan");
        request.setAttribute("age",12);
        return "success3";
    }

1.3.2 Model Type Transfer Data

  • Model object:
    • An object officially provided by SpringMVC, created by Dispatcher Servlet and passed as an argument to the cell method for use
    • Role: Carriers for data transfer
    • Usage method:
      • Declare Model objects directly, created by Dispatcher Servlet,
      • In the front end you can use it directly through an EL expression
    • Matters needing attention:
      • When requesting forwarding, the model's data can be extracted using an EL expression directly
      • When redirection is requested, the data in the Model object processed for the first time is used as a parameter for the second request, and the Model object created by the first request is destroyed. Data loss (loss of non-basic data types) may occur.
    @RequestMapping("/response4")
    public String response4(Model model){
        System.out.println("response4 mvc controller running ....");
        //Set Value
        model.addAttribute("username","lijiok");
        model.addAttribute("age",12);
        return "success3";
    }

1.3.3 ModelAndView type parameters for data transfer

  • ModelAndView object:

    • Type is passed to caller as return value

    • Models and views, which can carry both data information and view information

    • ModelAndView specifies the returned page name

      modelAndView.setViewName("page name");
      
    • ModelAndView addObject():

      modelAndView.addObject("username","wangwu");
      

      Equivalent to adding to the request domain, where the data can be fetched directly from the EL expression at the front end

    @RequestMapping("/response5")
    public ModelAndView response5(ModelAndView modelAndView){
        System.out.println("response5 mvc controller running ....");
        modelAndView.setViewName("success3");
        modelAndView.addObject("username","wangwu");
        modelAndView.addObject("age",23);
        return modelAndView;
    }

Topics: Java Spring mvc