views:

374

answers:

4

In Struts 1 you could have, in struts-config.xml, a declaration like:

<action path="/first" forward="/second.do">

Is something similar also possible in Spring, or can I map an URL only to a controller? I am using Spring 2.5.x.

I could off course map the URL to the same controller as:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/first.do">theController</prop>
      <prop key="/second.do">theController</prop>
      ...

Or maybe use the org.springframework.web.servlet.mvc.ParameterizableViewController and have something like:

<bean id="theDummyController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
     <property name="viewName" value="forward:second.do"/>
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/first.do">theDummyController</prop>
      <prop key="/second.do">theController</prop>
      ...

I know I could be complicating things and I should just stick to the simple stuff that gets the job done, but I would like this to be more like a statement of the kind: "this URL is in fact a shortcut (or alias) to this other URL" (don't ask why... long story...) which is somehow visible with the ParameterizableViewController but not completely.

So, is this possible?

Thank you!

A: 

I'm not familiar enough with Spring MVC to know the easiest way to do what you want there. However, the UrlRewriteFilter project makes it easy to setup rules to forward one url to another url.

Jason Gritman
A: 

You could make something easily that allows you to specify "this URL is in fact a shortcut (or alias) to this other URL" by implementing HandlerInterceptor and have your implementation contain a property that is map of URLs that should redirect, or forward, to other URLs.

In your XML you just add a bean for this interceptor, and configure it which URLs should redirect to what, and add a reference to the interceptor to the SimpleUrlHandlerMapping's interceptors property.

Fried Hoeben
I was also thinking to add an interceptor that catches an URL and forwards it to another, then returning false to break the execution chain. But doesn't the handler mapping have to resolve an execution chain for the incoming request? Only then the DispatcherServlet will execute the handler and interceptors found in the returned chain. So I have to map the URL to "something" and then let the interceptor handle the rest. But what do I map it to?
dpb
I'm actually not sure whether the handler needs to have a mapping for each URL before you can get it to an interceptor, but if it does: map * to a 404 page (not found) and you should be good...
Fried Hoeben
A: 

I would recommend managing the URL-based rules outside the Spring config files? Keep the Spring config files clean and rewrite/manage your URLs outside. Have a look at http://tuckey.org/urlrewrite/.

I hope it helps,

-florin

Florin
A: 

I finally ended up creating a separate URL handler mapping where I grouped together the URL aliases. I then resorted to a very detailed description of what the contained mappings are all about, something like:

<bean id="aliasUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <description>
    <![CDATA[
      The following URLs are in fact shortcuts (or aliases) 
      to other URLs etc etc (...I'll spare you the ugly part)
    ]]>
  </description>
  <property name="mappings">
    <props>
      <prop key="/first.do">theController</prop>
      ...

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/second.do">theController</prop>
      ...
dpb