1

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?

Community
  • 1
  • 1
0x41ndrea
  • 385
  • 5
  • 23
  • `${pageContext.request.contextPath}` is about as simple as you can get it. I don't consider it a work around. Think about it, that variable/tag has to be able to adapt for when the webapp is running as both a root webapp and under development environment with leading context name. So you need some variable in there to adapt to the differences you **might** expect in an absolute URL. FYI, you could use relative URLs but its generally considered a bad idea (Search on SO for pros & cons). – nickdos Nov 28 '12 at 23:49

2 Answers2

2

Try using a <spring:url /> tag, which by default appends the context root to the url.

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<link type="text/css" rel="stylesheet" href='<spring:url value="/resources/css/960_16_col.css" htmlEscape="true"/>'/>

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/spring.tld.html

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

import tag lib

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

then use url tag :

<c:url var="myCss" value="/resources/css/960_16_col.css"/>
<link type="text/css" rel="stylesheet" href="${myCss}"/>

or without creating a variable :

 <link type="text/css" rel="stylesheet" href="<c:url value='/resources/css/960_16_col.css'/>" />
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • that was one of my first attempt, but it won't work because it add the request path. So the problem change, adding the path of the request to the static url. Do you know if is possible to specify to the url ctag to add just the contextPath url? – 0x41ndrea Nov 28 '12 at 11:38
  • thats the way you are supposed to do it ...I think you might have your resources in the wrong location – NimChimpsky Nov 28 '12 at 11:50