Working on a Java EE application using Spring and Maven, the project structure follows the typical hierarchy. Here is a glimpse of it:
MyApplication
src
main
webapp
WEB-INF
layout
header.jsp
styles
main.css
To include the CSS file in the JSP, the following tag is used:
<c:url var="styleSheetUrl" value="/styles/main.css" />
<link rel="stylesheet" href="${styleSheetUrl}">
Upon deployment, the CSS file is not found. The href in the page source points to /MyApplication/styles/main.css
, despite the existence of /styles/main.css
within the WAR file. Trying to access the CSS file directly in the browser results in a 404 error.
The issue was traced back to the Dispatcher Servlet mapping:
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
It seems that the Dispatcher Servlet may not know how to handle the CSS request. What would be the ideal solution for this problem without having to modify all the request mappings?