3

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??

Community
  • 1
  • 1
shazinltc
  • 3,616
  • 7
  • 34
  • 49

1 Answers1

5

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.

dardo
  • 4,870
  • 4
  • 35
  • 52
  • As i have mentioned in the question, i have tried this. it doesn't help. – shazinltc Aug 15 '12 at 11:10
  • Where is your javascript file located within the project, what's the full path? – dardo Aug 15 '12 at 13:20
  • TestProject/WebContent/resources/js/test.js. TestProject is my project name. – shazinltc Aug 16 '12 at 09:14
  • 1
    I'm guessing your mvc:resources bean is having issues with the WebContent folder, try either pointing the location to classpath:/resources/ or moving the resources folder to within the WEB-INF and pointing the location to /WEB-INF/resources/ – dardo Aug 16 '12 at 13:29
  • Hey it is working fine now. I think it was some problem with contextpath variable declaration!! Sorry for that – shazinltc Aug 17 '12 at 09:40
  • no problem, either accept my answer or post your own and accept it, otherwise this question is going to sit open – dardo Aug 17 '12 at 13:17