Reputation: 2177
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-INF| | - jsp/*.jsp
My code to include jquery is:
<script type="text/javascript" src="<c:url value="js/jquery.js" />"></script>
and i cant load the js file into view.
I see the logs with info:
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/pool/js/jquery.js] in DispatcherServlet with name 'appServlet'
what means, that MVC try to map url to js file.
I think that there is something with my configuration, but i don't know what.
my web.xml is:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Upvotes: 10
Views: 35652
Reputation: 2406
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'/>">
Upvotes: 0
Reputation: 535
add mvc:resources in your config file(*-servlet.xml), you can find it works
Upvotes: 0
Reputation: 9407
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'/>" />
Upvotes: 2
Reputation: 1
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">
Upvotes: 0
Reputation: 11
Use spring JSTL tags to include external script files or style sheets. First you should include the the taglib in JSP as follows.
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
Then you can include extenal script file using,
<script type="text/javascript" src="<spring:url value="/js/jquery.js"/>"></script>
Upvotes: 0
Reputation: 1283
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?
Upvotes: 19
Reputation: 340763
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?
Upvotes: 6