Reputation: 192
I am new to Spring, I am using spring 3.0 web mvc i am including js and css files but it is not applying on the pages.Firebug showing the 404 error for js and css files. I tried many combination to include the js and css file but nothing worked. please suggest something for this. directory structure is:
SpringApp
--WebContent
--resources
--style.css
--script.js
--WEB-INF
--views
index.jsp
---------
Here is my web.xml file---
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>DojoSpring</display-name>
<servlet-name>SpringDojo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDojo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Here is my Spring configuration file-----
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<import resource="dbconfig.xml"/>
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.abc.controllers" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
</web-app>
And here is my index.jsp file--------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring</title>
<script type="text/javascript" src="/resources/script.js">
</head>
<body onload="testFun()">
Welcome to spring world....
</body>
</html>
Upvotes: 2
Views: 7362
Reputation: 1986
Add the following in web.xml
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
and use the following to include static js files
<script type="text/javascript" src="resources/script.js"> </script>
Upvotes: 1
Reputation: 451
Please check this link. I tried it and it was working for me fine. But it worked for me only when I kept my resources outside the WEB-INF folder.
http://www.stackoverflow.com/questions/11952212/spring-3-1-javascript-file-not-found-404-error
Upvotes: 0
Reputation: 49372
Can you try this?
<display-name>DojoSpring</display-name>
<servlet-name>SpringDojo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDojo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
And add ${pageContext.request.contextPath}/
for each call to the static resource.
Upvotes: 2