shazinltc
shazinltc

Reputation: 3666

Spring 3.1- javascript file not found- 404 error

I have a js file in the folder WebContent/resources/js/test.js. I am trying to include the same file in a jsp. But the jsp file is unable to find the js file (404 error in browser console). I have gone throw couple of questions in SO:

Can SpringMVC be configured to process all requests, but exclude static content directories?

Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

STS Spring MVC: How to include a JS file in a JSP

But still not helping. Here goes my code:

In the application context, i am using the mvc:resource tag.

<mvc:resources mapping="/resources/**" location="/resources/" />

In my jsp

<script src="${contextPath}/resources/js/test.js" type="text/javascript"></script>

tried giving src value with

 <c:url> 

too.

my web.xml has

<servlet-mapping> 
<servlet-name>TestProject</servlet-name> 
<url-pattern>/</url-pattern>
</servlet-mapping>

Firebug says

The requested resource (/resources/js/test.js) is not available.

Any help would be appreciated.

UPDATE

The GET request URL in firebug is this

http://localhost:8080/TestProject/resources/js/test.js

Is it right??

Upvotes: 3

Views: 12642

Answers (1)

dardo
dardo

Reputation: 4960

You need to do

<script src="<c:url value="/resources/js/test.js" />" type="text/javascript"></script>

Spring is trying to find the resource from the container context root, rather than the app context root.

Upvotes: 5

Related Questions