views:

60

answers:

0

Hi guys,

I want to use RESTful URLs in Spring MVC 3.0.3, e.g. the following in my web.xml:

<servlet>
    <servlet-name>addictedWebServices</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>addictedWebServices</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

I am using FreeMarker and Tiles 2. The problem is that when Tiles inserts a definition, the .FTL's that make up that definition can't be found... the request for the .FTL's gets routed through the DispatcherServlet and Spring can't map a request for it:

 2010-07-09 13:31:44,740  WARN [http-8080-3] org.springframework.web.servlet.PageNotFound (DispatcherServlet.java:965) - No mapping found for HTTP request with URI [/addicted/WEB-INF/freemarker/layouts-commonHead.ftl]

If I change the DispatcherServlet url mapping to "/*.page" then all works fine.. but I want RESTful URLs without extensions.

I have read the following thread and various others on using URL rewriting as a solution to this:

http://stackoverflow.com/questions/2946995/java-jsp-servlet-controller-servlet-throwing-the-famous-stack-overflow/2949077#2949077

I have implemented the solution and it works, to a degree. The .FTLs that make up my Tiles definitions are included in the returned page, however they are no longer being parsed for Freemarker tags and Spring macros (e.g. <@spring.url ... >), I suppose because they are no longer being handled by the DispatcherServlet. Instead the tags and macros appear in the returned page just as they do in the .FTL, which is of course not what I want.

At the moment I have put together a pretty hacky solution whereby a Controller has the following request mapping:

@RequestMapping(value = "/**/{path}.ftl", method = RequestMethod.GET)
public ModelAndView getFtl(@PathVariable String path) {

    ModelAndView mav = new ModelAndView(path);

    return mav;
}

Which then gets resolved by a FreeMarkerViewResolver .. this seems awkward to me and has to be repeated for other resources like CSS files, for which I have the same problem..

Is there a way around this? Any ideas on a solution? Does Spring include its own layout templating solution that might work here instead of Tiles?

Any help much appreciated.