SPRINGMVC dispatcherservlet interception and access to static resources HTML, JS, CSS 404 issues

Source: Internet
Author: User


Previously used *.do to do, but the *.do is very ugly, so instead of "/" To configure:

12345678910 <servlet>     <servlet-name>dispatcherServlet</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>     <servlet-name>dispatcherServlet</servlet-name>     <url-pattern>/</url-pattern> </servlet-mapping>

But the problem is how to access static files, such as Jpg,js,css?

If your dispatcherservlet blocks a URL with a suffix such as "*.do", there is no problem accessing static resources.

If your dispatcherservlet intercepts "/", in order to implement the rest style, all requests are intercepted, and access to static files such as *.js,*.jpg is intercepted.

We have to solve this problem.

Purpose: The static file can be accessed normally, and no static file 404 can be found.

Scenario One: Activate Tomcat's defaultservlet to handle static files

123456789101112 <servlet-mapping>      <servlet-name>default</servlet-name    <url-pattern>*.jpg</url-pattern>    </servlet-mapping>   <servlet-mapping>          <servlet-name>default</servlet-name>       <url-pattern>*.js</url-pattern>   </servlet-mapping>   <servlet-mapping>           <servlet-name>default</servlet-name>          <url-pattern>*.css</url-pattern>     </servlet-mapping>


To write in front of the Dispatcherservlet, let Defaultservlet first intercept the request, so the request will not enter spring, I think the performance is the best.

Tomcat, Jetty, JBoss, and GlassFish the name of the default servlet that comes with--"default"
Google App Engine comes with the name of the default servlet-"_ah_default"
The name of the default servlet that Resin comes with--"resin-file"
The name of the default servlet that WebLogic comes with--"Fileservlet"
The name of the default servlet that comes with WebSphere--"Simplefileservlet"

There are two other solutions, you can see: http://blog.csdn.net/this_super/article/details/7884383


Spring MVC static Resource 404 issue

When the Web. XML configuration servlet-mapping, if Url-pattern is set to "/" (below), many people will encounter import js,css, pictures and other static resources appear in the Firefox Debug window will report 404 errors, And you don't really have access to those resources.


Baidu for a long time, there are roughly 3 ways: But I still do not solve 404 problems, and then Google a bit, found a key place (in Baidu Search is not mentioned in a place, so that the more critical:)), is in the JSP page to import static resources when you need to use <C: url> tags.

For example:

<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>

<script type= "Text/javascript" src= ' <c:url value= '/js/jquery.js ' ></c:url> ' ></script>

The value in the C:url here also requires special attention to the place, see the following 3 ways to specify:

Method 1. Modify the Web. xml file to increase URL mappings to static resources

Such as:



When the configuration is added to Web. XML, these static resources can be referenced in the JSP page, but need to be <c:url value= "";

Such as:

<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>

<script type= "Text/javascript" src= ' <c:url value= '/js/jquery.js ' ></c:url> ' ></script>

Here also need to explain is: This method can not access the static resources under the Web-inf directory, that is, the JS directory must be the Web root (may be webapp,webcontent, etc.) directory, otherwise it can not be referenced;

If placed in the Web-inf directory, even if you use <c:url value= "/web-inf/js/jquery.js" > There will also be 404 errors.

Baidu found: The following container of the default servlet name, but also mentioned that the static resource servlet mapping needs to be written in front of Dispatcherservlet I have tested it in jboss-eap-5.1, it has nothing to do with it, so it may be a container or version of the relationship.

Tomcat, Jetty, JBoss, and GlassFish the default servlet name-"Default"
Google App Engine Default servlet name-"_ah_default"
Resin default servlet name-"Resin-file"
WebLogic default servlet name-"Fileservlet"
The name of the WebSphere default servlet-"Simplefileservlet"

Method 2: Add the spring Configuration to the corresponding-servlet.xml <mvc:default-serlvet-handler>

Such as:


This method only needs to add a line of code, the JSP page is referenced in the same way as Method 1, also cannot reference the resources under Web-inf.

Method 3. Using the new features of spring 3.0.4, add the configuration in the appropriate-servlet.xml <mvc:resource>

Such as:

3.1 <mvc:resources location= "/js/" mapping= "/js/**"/>


3.2 <mvc:resources location= "/web-inf/js/" mapping= "/js/**"/>

This method I wrote two configuration, different place is just the value of location, one is "/js/", One is "/web-inf/js/", both can, according to your own directory structure to refer to. This means that you can refer to static resources in the Web-inf directory using this method, and the value of the mapping property here uses the Ant wildcard method, "/js/**" (two "*") refers to the directory represented by the value of the location and all subdirectories However, it is important to note that when referencing in a JSP page:

<c:url value= "/js/jquery.js" > value must be similar to the value of the mapping property, if it is a 3.1-way configuration, the reference is under the Web root directory js/ Jquery.js, if it is a 3.2-way configuration, it refers to the Web root directory under Web-inf/js/jquery.js;

<c:url value= "/js/ui/jquery-ui.js" > references the location directory under the subdirectory UI under Jquery-ui.js.

SPRINGMVC dispatcherservlet interception and access to static resources HTML, JS, CSS 404 issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: