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

In this tutorial, we will show you how to include JavaScript andCSS 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.

spring-mvc-include-js-directory
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,

  1. Requested URL = example.com/resources/js/default.js
    Spring Converted to this = example.com/resources/mytheme/js/default.js
  2. 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 :
<link href="${pageContext.request.contextPath}/resources/css/main.css" rel="stylesheet" >
4. Demo

http://localhost:8080/SpringMVC/

spring-mvc-include-js-css-in-jsp
Download Source Code
Download – SpringMVC-Include-JS-CSS-File-Example.zip (66 KB)

原文:http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值