Spring MVC – How To Include JS Or CSS Files In A JSP Page
来源:互联网 发布:win7允许程序访问网络 编辑:程序博客网 时间:2024/06/11 00:38
In this tutorial, we will show you how to include JavaScript and CSS files in a JSP page, with the Spring MVC framework environment.
1. Project Directory
Review the final project directory structure. A standard Maven folder structure, put the JS and CSS files under thewebapp\resources
folder.
2. Resource Mapping
Declares mvc:resources
, to map “url path” to a physical file path location.
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.mkyong.common.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"><value>/WEB-INF/pages/</value> </property> <property name="suffix"><value>.jsp</value> </property></bean> <mvc:resources mapping="/resources/**" location="/resources/mytheme/" /> <mvc:annotation-driven /> </beans>
In above example, any requests from this url pattern /resources/**
, Spring will look for /resources/mytheme/
instead. For example,
- Requested URL = example.com/resources/js/default.js
Spring Converted to this = example.com/resources/mytheme/js/default.js - Requested URL = example.com/resources/css/default.css
Spring Converted to this = example.com/resources/mytheme/css/default.css
3. Include in JSP Page
To include JS and CSS in a JSP page, uses the standard c:url
JSTL tag.
hello.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><html><head><link href="<c:url value="/resources/css/main.css" />" rel="stylesheet"><script src="<c:url value="/resources/js/jquery.1.10.2.min.js" />"></script><script src="<c:url value="/resources/js/main.js" />"></script></head><body><h1>1. Test CSS</h1> <h2>2. Test JS</h2><div id="msg"></div> </body></html>
/webapp/resources/mytheme/js/main.js
jQuery(document).ready(function($) { $('#msg').html("This is updated by jQuery") });
/webapp/resources/mytheme/css/main.css
h1{color:red;}
Alternatively
For those who hates JSTL, you can use the “page context” variable to get the resources like this :
For those who hates JSTL, you can use the “page context” variable to get the resources like this :
<link href="${pageContext.request.contextPath}/resources/css/main.css" rel="stylesheet" >
4. Demo
http://localhost:8080/SpringMVC/
Download Source Code
Download – SpringMVC-Include-JS-CSS-File-Example.zip (66 KB)
- Spring MVC – How To Include JS Or CSS Files In A JSP Page
- Spring MVC – How to include JS or CSS files in a JSP page
- Spring MVC – How to include JS or CSS files in a JSP page
- How to access files in a jar?
- How to save a web page as HTML or MHT
- Show how to print a table in a html page
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- How to include a header on each page when printing a DataGrid
- How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?
- how to install a vncserver or vncviewer in Ubuntu
- How to reimplement (or wrap) a syscall function in linux?
- How to unzip or zip files content
- How to Output a List of Files to a File and Sort Them in Linux
- How to get session information in Spring MVC 3
- How to use Session attributes in Spring-mvc
- How To Print a Form That Is Too Large for the Screen or Page
- How to list files in a dictionary modified after a specific date
- How to send a list of files over a socket in Java
- JasperServer导出PDF报表中文问题解决方案
- 褚橙是如何用互联网营销颠覆橙子的?
- Hibernate.initialize(Obj)用法
- 谷歌浏览器审查元素http头部分析
- 幻世(OurDream)2D图形引擎使用教程6——使用纹理
- Spring MVC – How To Include JS Or CSS Files In A JSP Page
- 批量设置excel条件格式改变整行的填充色
- 操作系统原理——Dekker互斥算法详解
- abstract类和interface的区别(剽窃的)
- Java Socket实战之一 单线程通信
- MySql 动态行转列整理
- jQuery load()方法用法集锦!
- Eclipse Java注释模板设置详解
- DES信息加密解密