2

This question seems to have been asked a few times, but after reviewing too many of them to count, I am still seeing this problem.

The .jsp's in my Spring MVC project are unable to locate any .css or .js files even though they are in the war and the given file paths are correct.

Depending on my configuration I see two errors. The first being:

WARNING: No mapping found for HTTP request with URI [/waypoint/css/bootstrap.min.css] in DispatcherServlet with name 'waypoint'

Note that my controller is being reached and I am seeing the page, just without any formatting or javascript.

I see this error with the following configuration:

web.xml

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

waypoint-servlet.xml

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

File structure:

WEB-INF
| - css
|   ` - all .css files
| - js
|   ` - you guessed it...
| - pages
|   ` - all .jsp files
| - waypoint-servlet.xml
` - web.xml

I have read a lot of other posts that have resolved this issue using the mvc:resource tag.

When I include the mvc:resource tag, I see 404 errors for my .jsp's. This occurs even when I change the servlet mapping to /*, /**, /<some-name>/, and /<some-name>/*.

I have also tried moving the static files outside of the WEB-INF folder:

| - resources
|  | - css
|  |   ` css...
|  | - js
|      ` js...
| - WEB-INF

As I mentioned, I tried many different mapping combinations for the mvc:resource tag, for both of the above file structures. Some examples include:

<mvc:resources mapping="waypoint/resources/css/**" location="/resources/css/" />
<mvc:resources mapping="resources/css/**" location="/resources/css/" />
<mvc:resources mapping="css/**" location="/resources/css/" />
<mvc:resources mapping="css/**" location="WEB-INF/css/" />
<mvc:resources mapping="css/**" location="css/" />

I tried the above with combinations of the following on the .jsp side as well:

<link href="classpath:/resources/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="<c:url value="../resources/css/bootstrap.min.css"/>" rel="stylesheet" type="text/css" />
<link href="<c:url value="../css/bootstrap.min.css"/>" rel="stylesheet" type="text/css" />

Has anyone experienced this before that might be able to lend a hand? It would be greatly appreciated. If you need any other info please let me know.

Thanks.

SOLUTION

I was able to resolve this issue by adding servlet mappings for *.css and *.js to the fefault servlet:

<servlet-mapping>
    <servlet-name>waypoint</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<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>

This required the removal of the <mvc:resource> tags. I also required the use of the resource/css/ directory outside of the WEB-INF folder. I referenced the js and css files in the JSPs as follows:

<link href="<c:url value="/resources/css/bootstrap.min.css"/>" rel="stylesheet" type="text/css" />

Thanks for reading!

2 Answers 2

1

I am not sure why the <mvc:resources> tag is not working in your case, as it should from the spring 3.2.0. Add the below lines in your web.xml, these are the common problems with the restless urls

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>

You need to place your files outside the web-inf to make it visible to the intercepted requests.

A nice tutorial here

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

Comments

1

A lottery like answer :) the winning combination should be

1) structure, according to your second 'tree', so

| - resources
|  | - css
|  |   ` css...
|  | - js
|      ` js...
| - WEB-INF

2) mapping should be

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

3) the access to static resources from the JSPs, as

<link href="<c:url value="/resources/css/bootstrap.min.css"/>" rel="stylesheet" type="text/css" />

I think that despite trying a lot, your static files failed on account of the wrong configuration, above should fix it

1 Comment

Hey Master Slave, thanks loads for taking a look for me. Sorry to say that I tried your config and it still didn't work out. I am seeing a 404 in the browser and WARNING: No mapping found for HTTP request with URI... in the console (for the JSP, not the CSS file). I have tried playing with the prefix value for the ResourceViewResolver (without success) as well as changing the servlet mapping to /* (no luck). I can say that whenever the resource tag is present, it break's Spring's ability to locate my JSPs. Not sure if that helps at all. Thanks again for your suggestion so far.

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.