Confusion of resource requests under static resource release, custom view parsing, restful and WEB-INF in spring MVC

empirical conclusion

This article records the problems of static resource release, custom view parser and resource request under WEB-INF in spring MVC. Although these concepts are clear when learning, they are often confused in development. This article is written after two hours of testing

Next, let's talk about my conclusion:
1. jsp files will not go through unit method matching (but html, css, js, image, etc. will go through unit method matching)
2. The priority of static resource release is lower than the matching of cell methods. The url of static resource release should not conflict with the cell methods. If there is a conflict, the static resource release will not take effect
3. Although the resources under WEB-INF cannot be accessed directly through the browser, we can access them through the mechanism of request forwarding and static resource release
4. When accessing jsp, it will not be released by static resources (but html will)

Pit record

Scenario 1:

First configure the view parser, forward the request to the WEB-INF/page, and then configure the restful unit method. This method only returns the request address, which is used to cooperate with the view parser to enable the browser to access the resources under the web-inf. Let's see if jsp requests can be matched by unit methods and forwarded correctly?

code

Static resource release, custom view parser

<!-- 3.Configure static resource release    -->
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"></mvc:resources>
    
    <mvc:resources mapping="/css/**" location="WEB-INF/css/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="WEB-INF/images/"></mvc:resources>

    <!--  4.Configure custom view parser  -->
    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/"></property>
        <property name="suffix" value=" "></property>
    </bean>

This code configures the static resource release and user-defined view. We can see that when the url matches js/xxx, css/xxx and images/xxx, we can jump to the resources under WEB-INF/js(css and images) through static resource release. The idea that I do not configure the suffix name is that I want to access any resources under the WEB-INF/page through the view parser. If the suffix name is set, the resource type will be restricted.

restful code

@RequestMapping("{page}/{url}")
    public String getPage(@PathVariable String url){
        System.out.println("restful:"+url);
        return url;
    }

The function of this code is to cooperate with the custom view parser to enable the browser to access the resources under WEB-INF

Record Category


We can see that there is a page folder under web-inf. next, we try to access this WEB-INF/page/index.jsp by matching the getPage method

result


We can see that 404 appears when accessing page/index.jsp. What's going on? The first article of the conclusion says that JSP files will not match unit methods without DispatcherServlet.
But I changed restful.

@RequestMapping("{url}/{xxx}")
    public String getPage(@PathVariable String url){
        System.out.println("restful:"+url);
        return url;
    }

Many partners asked that the request of jsp file will not match the unit method. It still matches the restful method. Yes, it does, but it is not a jsp request at all. Let's take a closer look
http://localhost:8080/springMVC_ 02_ war_ The format expanded / index.jsp/xxx is a JSP request. JSP is just a section of the path. At this time, the server parses index.jsp into a level instead of a JSP file.

Scenario 2

Just now we have tested that the request of jsp file will not pass through the unit method. Will it pass through the static resource release?

code

 <!-- 3.Configure static resource release    -->
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"></mvc:resources>
    <mvc:resources mapping="/page/**" location="/WEB-INF/page/"></mvc:resources>
    <mvc:resources mapping="/css/**" location="WEB-INF/css/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="WEB-INF/images/"></mvc:resources>

    <!--  4.Configure custom view parser  -->
    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/"></property>
        <property name="suffix" value=""></property>
    </bean>
Unit method
@RequestMapping("{url}")
    public String getPage(@PathVariable String url){
        System.out.println("restful:"+url);
        return url;
    }

Record Category

result


We can see that we initiated the page/index.jsp request. According to the resource release mechanism, we should jump to the WEB-INF/page/index.jsp resource, but the error / page/index.jsp was not found, indicating that the JSP request has not been released by static resources.
Root cause: jsp requests do not go through DispatcherServlet, and whether static resource release or unit method matching is based on DispatcherServlet, so static resource release cannot forward jsp requests
Let's see if the html was accessed successfully to verify the results.

No accident, the access was successful.

Scenario 3

At this point, I want to compare the forwarding of static resource release with the matching of unit methods, who has the higher priority.

code

 <!-- 3.Configure static resource release    -->
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"></mvc:resources>
    <mvc:resources mapping="/page/**" location="/page/"></mvc:resources>
    <mvc:resources mapping="/css/**" location="WEB-INF/css/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="WEB-INF/images/"></mvc:resources>

    <!--  4.Configure custom view parser  -->
    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    @RequestMapping("{url}")
    public String getPage(@PathVariable String url){
        System.out.println("restful:"+url);
        return url;
    }

We notice that at this time, I suffix the custom view, and I can jump to the / WEB-INF/page/index.jsp file by accessing / index.

Record Category


According to the file level, when I access / page/index, an error will be reported if static resources are preferred, and if unit methods are preferred, the access will be successful

result


After successful access, we change the file level to verify again. If the unit method fails, can the static resource release be executed?

We moved the index.html under the WEB-INF/page to the page and added it without the suffix of the custom view parser, because HTML can be matched by both static resource release and unit methods. At this time, page/html is requested, and the cell method matching must be invalid. If the static resource release can still be executed, it should be accessed correctly. If the static resource release is no longer executed after the cell method fails, it will report 404

Obviously, the url settings of the unit method and the static resource release should not conflict. If there is a conflict, the static resource release will not be executed.

Tags: Spring MVC

Posted on Tue, 09 Nov 2021 20:31:43 -0500 by ectraz