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!