Spring MVC 4.2.2 中最好的集成静态资源的方法

Spring MVC 4.2.2 中最好的集成静态资源的方法

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。



Spring MVC 4.2.2 – Best way to Add/Integrate JS, CSS and images into JSP file using ‘mvc:resources mapping’


Sometime back I’ve written a tutorial on  Hello World Spring MVC. Spring MVC web is a model-view-control framework and you can find more information here.

As you could see Sayan and Arturo asked few questions while working on Spring MVC tutorial on how to add JavaScript/.js and CSS/.css file to their project.

Spring MVC 4.2.2 中最好的集成静态资源的方法_第1张图片

Both .js and .css are static resources. Do you also have similar question like:

  • In spring mvc where to put css/js/img files?
  • Why my project can’t find css, images and js static files, etc?

In this tutorial we will go over all detailed steps on how to include both into your Spring MVC java project simplest way.

Let’s get started:

Below is an updated project structure for your reference.

Spring MVC 4.2.2 中最好的集成静态资源的方法_第2张图片

Step-1

Please go ahead and implement your HelloWorld Spring MVC project by following all detailed steps. Make sure you have it working correctly.

Now, in below steps we are going to – create 1 folder, add 2 files, modify 2 files.

Step-2

Create resources folder under WebContent folder.

Step-3

Create crunchify.js file under resources folder.

Step-4

Create crunchify.css file under resources folder.

Step-5

Modify welcome.jsp file with below content. Please checkout highlighted lines.

Here spring:url tag based on the JSTL c:url tag. This variant is fully backwards compatible with the standard tag. Enhancements include support for URL template parameters.

Step-6

Modify crunchify-servlet.xml file. Add below two lines at the bottom of file before </beans> tag.

Here is a complete file content:

mvc:resources configures a handler for serving static resources such as images, js, and, css files with cache headers optimized for efficient loading in a web browser. Allows resources to be served out of any path that is reachable via Spring’s Resource handling.

Step-7

  1. Perform Project -> Clean
  2. Re-deploy CrunchifySpringMVCTutorial application on Tomcat web server
  3. Visit URL: http://localhost:8080/CrunchifySpringMVCTutorial/welcome.jsp
  4. Check out the result

Before

Spring MVC 4.2.2 中最好的集成静态资源的方法_第3张图片

After

Spring MVC 4.2.2 中最好的集成静态资源的方法_第4张图片

Have anything to add to this article? Please chime in and join the conversion.
SHARE ON
Twitter Facebook Google+ Buffer Pin It Follow @Crunchify

Enjoyed this post?

Be sure to subscribe to the Crunchify newsletter and get regular updates about awesome posts just like this one and more! Join more than 13000 subscribers!

Enter your email address...

About Crunchify

Hello & Good Day from greater New York. Crunchify is founded by App Shah. He is a professional blogger & loves Web development hence Crunchify.com is his publication with more than 10 millions pageviews per month, that covers all aspects and tactics on Java, WordPress, J2EE (Enterprise Java), Spring MVC, Github, etc Technologies.






你可能感兴趣的:(Spring MVC 4.2.2 中最好的集成静态资源的方法)