The correct advanced case of spring MVC is explained in detail. 60% of the people don't understand it


code implementation

Configuration of springMVC:

public class MVCConfiguration {

     * A view resolver is set up and handed over to spring management
     * @return
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        //Set prefix
        //Set suffix
        return internalResourceViewResolver;



    public String run5(){
//        return "/demo5.jsp";
        //The view parser will automatically complete as follows:/ demo5.jsp
        return "demo5";

Note: light theory is not enough. By the way, I'll send you ten sets of practical tutorials and interview question bank of the latest JAVA architecture project in 2020, which can be found under the transformation of seven bar umbrella bar and Zero clothing umbrella (Digital homophony), and also can communicate with the old architect



View resolver: the return value of the controller will pass through the view resolver to automatically complete the prefix and suffix.



2, Utility 2: bypass view parser

code implementation


    public String run6(){
        //Bypass view parser mode 1: redirection
//        return "redirect:/demo6.html";
        //Bypass view parser mode 2: full version request forwarding
        return "forward:/demo6.html";



Bypass the view parser:

Redirect: return "redirect: / path";

Full version request forwarding: return "forward: / path";



3, Knowledge point: return value of Controller method

Return string

Three types:

Use the view parser (request forwarding): return "resource name";

Bypass view Parser - request forwarding: return "forward: / path";

Bypass view resolver - redirect: return "redirect: / path";


Return to modelandview (spring MVC specific)

    * Need to jump to demo5.jsp ModelAndView on
    * */
    public ModelAndView run7(){
        ModelAndView mav = new ModelAndView();
        //Setting the request scope pass parameter is equivalent to request.setAttribute
        //Set jump path
        mav.setViewName("demo5");//Using view resolver, request forwarding
//        mav.setViewName("forward:/demo5.jsp "); / / bypass the view parser and request forwarding
//        mav.setViewName("redirect:/demo5.jsp "); / / bypass the view parser and redirect
        return mav;


ModelAndView: Spring MVC is specifically used to encapsulate scope data and jump to pages




4, Knowledge points: @ RequestMapping

Narrow request path

@RequestMapping is placed above the class name to set the request prefix

@RequestMapping is placed above the method name, and the corresponding request path of the method is set.

Full request: prefix + request path


Multipath mapping

*   /Project name / d1/run8.action
*   /Project name / d1/8run.action
*   /Project name / d1/run88.action
* */
@RequestMapping(value = {"/run8","/8run","/run88"})
public String run8(){
    return "index";



Request mode limit

By default, the request method is not limited, and can be accessed by get/post or other methods


    Request mode of this method, only POST mode is allowed
* */
@RequestMapping(value = {"/run9"},method = {RequestMethod.POST})
public String run9(){
    return "index";




Today's summary:

View parser:

After controller execution, prefixes and suffixes are completed for the controller.


Return value of Controller method:


Use the view parser: return "demo1";

Bypass the view parser:

Request forwarding: return "forward:/demo1.jsp ”;

Redirect: return "redirect:/demo1.jsp ”;


ModelAndView: encapsulate request data, jump to page

addObject(); save key value pairs to the request scope

setViewName(); sets the jump path.


@RequestMapping: set the access path for the method / class.



Note: in the end, I will send you ten sets of 2020 latest JAVA architecture project practice tutorials and large factory interview question bank, which can be put into my skirt: seven bar umbrella bar zero and clothes zero umbrella (Digital homophony) can be found under the conversion, and can also communicate with the old architect

The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Tags: Java JSP Spring

Posted on Sun, 17 May 2020 20:05:53 -0700 by fhil85