I'm developing a Spring MVC application with RESTFul urls. I'm in trouble with static resources path resolution.
I've in a jsp page a static resource written as:
<link type="text/css" rel="stylesheet" href="resources/css/960_16_col.css">
So when the page is rendered by tomcat 7, I get an error:
No mapping found for HTTP request with URI [/mycoolapp/instances/demo/resources/css/960_16_col.css]
In my servlet-context.xml I have:
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
My web.xml is:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And finally my controller is:
@RequestMapping(value = "/instances/{proj}/{type}", method = RequestMethod.GET)
public ModelAndView instances(Locale locale, Model model,
@PathVariable("proj") String project,
@PathVariable("type") String type) {
. . .
}
Working without the restful urls everything works. After googling and stacking I've found the solution using absolute path for the static resources:
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/960_16_col.css">
as written in this stack overflow question, but it seems a workaround.
Is there an elegant way to resolve static urls and keep them relatives?