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

太阳火神的美丽人生 (http://blog.csdn/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’

Short link:

Last Updated on October 22nd, 2015 by Crunchify 2 Comments


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.

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.

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.

crunchify.js
1 2 3 4 5 jQuery ( document ) . ready ( function ( $ ) {   $ ( '#crunchifyMessage' ) . html ( "<h4>This message is coming from 'crunchify.js' file...</h4>" )   } ) ;

Step-4

Create crunchify.css file under resources folder.

crunchify.css
1 2 3 4 5 6 h2 { color : #dd7127; } h4 { color : #DD2727; }

Step-5

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

welcome.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <% @ taglib prefix = "spring" uri = "http://www.springframework/tags" %> < ! DOCTYPE html > < html > < head >   < ! -- let 's add tag srping:url --> <spring:url value="/resources/crunchify.css" var="crunchifyCSS" /> <spring:url value="/resources/crunchify.js" var="crunchifyJS" /> <script src="https://ajax.googleapis/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <link href="{crunchifyCSS}" rel="stylesheet" /> <script src="${crunchifyJS}"></script> <!-- Finish adding tags -->   <title>Spring MVC Tutorial by Crunchify - Hello World Spring MVC Example</title> <style type="text/css"> body { background-image : url ( 'http://crunchify/bg.png' ) ; } </style> </head> <body>${message} <br> <div style="font-family: verdana; padding: 10px; border-radius: 10px; font-size: 12px; text-align: center;">   <h2>Checkout this font color. Loaded from ' crunchify . css ' file under ' / WebContent / resources / ' folder</h2>   <div id="crunchifyMessage"></div>   <br> Spring MCV Tutorial by <a href="http://crunchify">Crunchify</a>.   <br> <br> Click <a href="http://crunchify/category/java-web-development-tutorial/" target="_blank">here</a> for all Java and <a href=' http : //crunchify/category/spring-mvc/' target='_blank'>here</a> for all Spring MVC , Web Development examples . < br > < / div > < / body > < / html >

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.

1 2 < mvc : resources mapping = "/resources/**" location = "/resources/" / > < mvc : annotation - driven / >

Here is a complete file content:

crunchify-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 < beans xmlns = "http://www.springframework/schema/beans" xmlns : mvc = "http://www.springframework/schema/mvc" xmlns : context = "http://www.springframework/schema/context" xmlns : xsi = "http://www.w3/2001/XMLSchema-instance" xsi : schemaLocation = "         http://www.springframework/schema/beans             http://www.springframework/schema/beans/spring-beans.xsd         http://www.springframework/schema/mvc         http://www.springframework/schema/mvc/spring-mvc.xsd         http://www.springframework/schema/context         http://www.springframework/schema/context/spring-context.xsd" >   < context : component - scan base - package = "com.crunchify.controller" / >   < bean id = "viewResolver" class = "org.springframework.web.servlet.view.UrlBasedViewResolver" > < property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" / > < property name = "prefix" value = "/WEB-INF/jsp/" / > < property name = "suffix" value = ".jsp" / > < / bean >   < mvc : resources mapping = "/resources/**" location = "/resources/" / > < mvc : annotation - driven / > < / beans >

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

After

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

Some more articles you might also be interested in …

  1. Spring MVC: How to Declare a Bean in Spring Application?
  2. Working on Spring MVC Project? How to Report List of All Loaded Spring Beans during Startup?
  3. Read config.properties value using Spring ‘singleton’ Scope in your Java Enterprise Application
  4. Simplest Spring MVC Hello World Example / Tutorial – Spring Model – View – Controller Tips
  5. Spring MVC 4.1.X – Simple way to Send an Email using org.springframework.mail. javamail.JavaMailSenderImpl
  6. How to import all Spring MVC Dependencies to your Maven Project?
  7. Cannot be Read or is Not a Valid ZIP file – How to fix Maven Build Path Error with corrupted .jar file
  8. How to use AJAX, jQuery in Spring Web MVC (.jsp) – Example
  9. Spring MVC: How to Access ModelMap Values in a JSP?
  10. How to Update Sparkline Graph Every 3 Seconds in Spring MVC (Realtime Update)

Filed Under: Apache Tomcat Examples, Java Script, jQuery, JSON, My Collection, Spring MVC Tutorials, Tips n Tricks Tagged: Latest Spring MVC, Load JS and CSS spring mvc, Load static files JS and CSS in Spring MVC, MVC Resource Mapping, Spring MVC 4.2.2, Spring MVC 4.2.2 Tutorial, Spring MVC Hello World Example, Spring MVC resource mapping, Spring MVC tutorial, spring:url Tag

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!

更多推荐

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