admin管理员组

文章数量:1415139

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 bination 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="" xmlns="" xmlns:web=".xsd" xsi:schemaLocation=" .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=""
xmlns:context=""
xmlns:mvc="" 
xmlns:xsi=""


xsi:schemaLocation="  
.0.xsd
 
.0.xsd 
 
.0.xsd">

<import resource="dbconfig.xml"/>

<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<context:ponent-scan base-package=".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" ".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()">
    Wele to spring world....
</body>
</html>

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 bination 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/2001/XMLSchema-instance" xmlns="http://java.sun./xml/ns/javaee" xmlns:web="http://java.sun./xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun./xml/ns/javaee http://java.sun./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/schema/beans"
xmlns:context="http://www.springframework/schema/context"
xmlns:mvc="http://www.springframework/schema/mvc" 
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"


xsi:schemaLocation="http://www.springframework/schema/beans  
http://www.springframework/schema/beans/spring-beans-3.0.xsd
http://www.springframework/schema/context 
http://www.springframework/schema/context/spring-context-3.0.xsd 
http://www.springframework/schema/mvc 
http://www.springframework/schema/mvc/spring-mvc-3.0.xsd">

<import resource="dbconfig.xml"/>

<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<context:ponent-scan base-package=".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/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()">
    Wele to spring world....
</body>
</html>
Share Improve this question asked Apr 19, 2013 at 8:59 Mahesh PatidarMahesh Patidar 1923 silver badges15 bronze badges 2
  • try this... <script type="text/javascript" src="<%=request.getContextPath()%>/resources/script.js"> – Brijesh Eshwar 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" – Mahesh Patidar Commented Apr 19, 2013 at 9:16
Add a ment  | 

3 Answers 3

Reset to default 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.

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>

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./questions/11952212/spring-3-1-javascript-file-not-found-404-error

本文标签: javascriptJS and CSS file not found error in spring 30 web mvcStack Overflow