I have created MVC application.
I want to include js or css file into jsp.
My static files ar under:
- webapp -js/jquery.js -WEB-I
add this to you confing and modify the location according to your need.
<mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>
see this How to handle static content in Spring MVC?
Change your DispatcherServlet
mapping to e.g:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
Or some other not conflicting url-pattern
like *.htm
or /controllers/*
. Remember that from now on all your controllers will be available only through this pattern.
Now it is intercepting everything in your web application, including .js
files, images, etc.
The same thing with hibernateFilter
- you don't really need an open Hibernate session when fetching .js
files, don't you?
add mvc:resources in your config file(*-servlet.xml), you can find it works
I agree with your answer. But in the style.css file declare url that relate to path of image.
--style.css--
.cwt-object0
{
display: block;
left: 2.62%;
margin-left: -1px;
position: absolute;
top: 43px;
width: 64px;
height: 64px;
background-image: url('/resources/images/object0.png');
background-position: 0 0;
background-repeat: no-repeat;
z-index: 0;
}
How to use tag <spring:url></spring:url>
in style.css file to see in browser IE/Firefox
--jsp file ---
<link href="<spring:url value="/resources/style.css"/>" rel="stylesheet" type="text/css" media="screen">
Why not use the simple jsp core ?
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/bootstrap.css'/>" />
I just followed Mkyong Tutorial to place css, js, jquery & image files. Its working for me.
In servlet-context.xml
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/assets/" />
In JSP , import tag library
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
and add like
<link rel="stylesheet" href="<c:url value='/resources/css/custom.css'/>">