The path problem of SpringMVC configuration front controller

When using SpringMVC, you need to configure a front-end controller DispatcherServlet in web.xml

The controller is a servlet, but there are many kinds of path configurations. Before, the path was configured with an extension, such as *.do, and then the path of *.do was accessed in the project.

But the recent projects follow the Restful style, and the configuration path is as follows

You can see that the configured path is "/". Due to the doubts that I searched online, it was said that both /* and / were intercepting all requests, so I changed the path here to /*, and then the project came out with a 404 resource not found. question.

By checking the information later, I understand the difference between /* and / as follows

  First we understand the matching rules of a servlet

    Precision matching>Path matching>Extension matching>Introspection matching (There are no specific examples here, students who want to know can Baidu by themselves)

    All here / belongs to introspection matching pattern (TomCat has one such path by default), /* belongs to path matching. So when the controller returns a result view access, if the configuration is /*., SrpingMVC will intercept it again, but because the relevant controller cannot be found, it will report 404 resource not found.

    If the configuration here is /, the default in Tomcat will be overwritten first. Tomcat is used to process static resources by default, such as html, jsp and other static resources. After this is covered, we have to consider the processing of static resources by ourselves.

    

  Secondly, I found that some such paths were configured in web.xml. Since the default of Tomcat was overwritten before, some static resources should be manually called to Tomcat's default processor for processing, but we did not find jsp pages in it. , then the jsp returned by the controller will still be/intercepted into SpringMVC.

  So in order to prevent the resource not being found and reporting 404, the following configuration information is configured in the SpringMVC configuration file

The first function is to process the static resources that enter SpringMVC, the function is similar to Tomcat's default DefaultServlet function

Secondly , the function of this is that when you enter SpringMVC and the path is /, it will jump to the /index.html interface by default, which seems to be the function of jumping to the home page.

Finally  , SpringMVC provides processing of static resources, and the detailed usage is Baidu.

 

There is a doubt, if I access a js file, web.xml is configured with *.js and handed over to Tomcat for processing (extension matching), DispatcherServlet is configured with '/' (introspection matching), and the extension priority is greater than Introspection, so when I access a JS static resource, does it enter SpringMVC, but here I configure some static resource processing in SpringMVC, what is the use? ? ?

 

Summarize:

  1. Both /* and / can intercept all requests, but the priority is different. The priority of /* is higher than that of /, which is why the configuration of /* will report the problem that the resource cannot be found, because the default Tomcat cannot be reached at all. Servlet to handle static resources.

  2. It is said on the Internet that /* blocks extensions, / will not block extensions with extensions. I think this is always wrong. Still have to understand the priority of the path.

 

 

 

 

 

Guess you like

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