2

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>
2
  • try this... <script type="text/javascript" src="<%=request.getContextPath()%>/resources/script.js"> Commented Apr 19, 2013 at 9:03
  • I try this but Fire bug gives me this error NetworkError: 404 Not Found - localhost:8090/DojoSpring/resources/script.js" Commented Apr 19, 2013 at 9:16

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for reply.As I mentioned above that I am using spring 3.0 and it does not support this.
I try this but giving the error-NetworkError: 404 Servlet Resource Servlet is not available - localhost:8090/DojoSpring/resources/script.js by Firebug (Firefox)
1

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.