The main purpose is to record my own doubts, and I generally don't ask for trouble like me.
When using SpringMVC, we need to configure the front controller DispatcherServlet with web.xml
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
What does DispatcherServlet do?
In 拦截请求(浏览器的访问),然后根据自己定义的规则,去匹配处理器(Controller)来处理
short, .
Then specifically what requests to intercept
url-pattern configures this.
It has three configurations
- Absolute path/UserServlet
- Multi-level directory /aaa/bbb/UserServlet
- Wildcard matches *.action *.do (as long as the request is followed by .action or .do, the servlet will be executed)
There are two special ones (better not to write)
- <url-pattern >/< /url-pattern>
is a /, if written in this way, DispatcherServlet will静态资源
obtain requests from, such as .css, .js, .jpg, .png and other resources当作一个普通的Controller请求。中央调度器会调用处理器映射器为其查找相应的处理器
. This is not found, it will report 404. - <URL-pattern> / * </ URL-pattern>
* represents all, i.e.静态资源
,动态页面(jsp页面,servlet)
other requests are intercepted.把静态资源、jsp页面的获取请求当作Controller请求去找处理器
, Then can't find and then 404.
If you insist on writing /, it can be solved.
You need to use the <mvc:resources/> tag in the springmvc configuration file to solve the problem of inaccessibility of static resources
. The login.html and qqq.jpg in the picture are not accessible at this time.
We just write the following code in the springmvc.xml file.
<mvc:resources location="/page/" mapping="/page/**"/>
location
Indicates all directories of static resources, here can also be the /WEB-INF/ directory and its subdirectories.
mapping
Indicates a request for this resource. Two stars* behind.
This configuration will directly map the request for access to the static resource to the static resource handler object ResourceHttpRequestHandler via HandlerMapping.