admin管理员组文章数量:1794759
Spring MVC 4.2.2 中最好的集成静态资源的方法
Spring MVC 4.2.2 中最好的集成静态资源的方法
太阳火神的美丽人生 (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-1Please 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-2Create resources folder under WebContent folder.
Step-3Create crunchify.js file under resources folder.
crunchify.js1 2 3 4 5 | jQuery ( document ) . ready ( function ( $ ) { $ ( '#crunchifyMessage' ) . html ( "<h4>This message is coming from 'crunchify.js' file...</h4>" ) } ) ; |
Create crunchify.css file under resources folder.
crunchify.css1 2 3 4 5 6 | h2 { color : #dd7127; } h4 { color : #DD2727; } |
Modify welcome.jsp file with below content. Please checkout highlighted lines.
welcome.jsp1 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 = "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="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 ( '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="crunchify">Crunchify</a>. <br> <br> Click <a href="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-6Modify 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.xml1 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 = "www.springframework/schema/beans" xmlns : mvc = "www.springframework/schema/mvc" xmlns : context = "www.springframework/schema/context" xmlns : xsi = "www.w3/2001/XMLSchema-instance" xsi : schemaLocation = " www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc.xsd www.springframework/schema/context 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-7Filed 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 中最好的集成静态资源的方法 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686614058a86303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论