SpringMVC Preliminary Exploration of View Resolver - InternalResourceViewResolver

Springmvc usually returns a logical view in the handler method. How to locate the real page needs to pass through the view parser.

There are multiple view resolvers provided in springmvc, InternalResourceViewResolver is one of them:

Most commonly used view resolver: InternalResourceViewResolver

image

 

When the handler returns "index", the InternalResourceViewResolver resolver automatically adds the prefix and suffix: /WEB-INF/jsp/index.jsp

Note: The order here represents the priority of view resolution. The smaller the number, the higher the priority (ie: 0 is the highest priority, so the view is processed first),

The priority of InternalResourceViewResolver in the project must be set to the lowest, that is, the order must be the largest. Otherwise it will hinder other

View resolver. why?

The explanation is as follows:

We know that when the processor returns a logical view (that is, return "string"), it has to go through the view parser chain, and the previous parser can handle it, and it will not continue to propagate down.

If it cannot be processed, continue to search along the parser chain until a suitable view parser is found (in summary: if it can be parsed, do not continue to look down, if it cannot be parsed, continue to look for the parser).

See the picture below:

Suppose we have several view resolvers: view resolver 1, view resolver 2, view resolver 3. As shown

image

 

When the processor (@controller) returns the logical view resolution process:

When passing through view resolver 1, if it can be parsed, it will be parsed and will not continue. Returns null if it cannot be executed so that the parser below can handle it.

But for the resolver InternalResourceViewResolver, it will not return null whether it can be parsed or not, that is to say it intercepts all logical views,

Make subsequent resolvers not executed, so InternalResourceViewResolver must be placed last.

One thing to remember: if you can't parse it, return null so that subsequent parsers can parse it

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325587185&siteId=291194637