Spring MVC program Get static resource file Css,js, picture file path Problem summary

Source: Internet
Author: User

Previous post | Next article get static resource file Css,js in Spring MVC program, picture

Path to the file

Using Spring MVC to develop the application, for beginners have a very headache, that is, the program data has been queried, but the interface style is still very ugly, loading css,js, pictures and other resource files. When you enter the path of a CSS file directly on the browser, you get a 404 error directly, and the path is definitely not wrong, the reason is that a spring servlet similar to the following is configured in Web. XML:

Program code
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


Please note <url-pattern>/</url-pattern> This means that the servlet intercepts all requests, including CSS,JS. So the situation described above appears. How to solve this css,js and other path problems, there are several solutions.

1. Use spring MVC resource to read static files
For example, create a static folder under Wen-inf, create a CSS folder inside the static folder, and then create a 1.css file.
Modify the Mvc-dispatcher-servlet.xml file

Program code
<mvc:resources mapping= "/static/**" location= "/web-inf/static/"/>


After this configuration, it can be accessed directly through the HTTP://YOUR-IP:PORT/YOUR-APP/STATIC/CSS/1.CSS.

2. Using the default servlet
Configure in Web. xml

Program code
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>


Once this is done, all requests that have a static prefix are given to the default servlet for processing. If you request Http://your-ip:port/your-app/static/css/1.css, it represents the 1.css file under the CSS folder in the WebContent directory.

3. Processing by file name extension
Configure in Web. xml

Program code
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
</servlet-mapping>



4. Do not use restful style for spring servlet. Prefix the URL

Program code
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/app</url-pattern>
</servlet-mapping>


The purpose of this is to allow Dispatchservlet to intercept those URLs that begin with/app, and do not intercept css,js.

5. Do not use restful style for spring servlet. Add a suffix to the URL

Program code
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


The purpose of this is to let Dispatchservlet only intercept *.do URLs. Do not intercept css,js and so on.

The 5 methods described above, according to the circumstances of their own arbitrary choice, there is no way to say that kind of good, the method is not good, there are desirable places, used in different scenes only.

Spring MVC program Get static resource file Css,js, picture file path Problem summary

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.