Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 450
shazinltc Avatar asked Dec 05 '25 09:12

shazinltc


1 Answers

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.

like image 167
dardo Avatar answered Dec 08 '25 11:12

dardo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!