views:

18

answers:

1

On my Tomcat-Spring-Java project I want to have the following URLS

/index - to display entry page
/login - Login page

/cars/ - list the available cars
/cars/{id} - show a particular car
/cars/{id}/action - do action on this particular car

/people/ - list the available people
/people/{id} - show a particular person
/people/{id}/action - do action on this particular person

Ive got my dispacher-servlet map like this in web.xml.

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/login</url-pattern>
    <url-pattern>/logout</url-pattern>
    <url-pattern>/index</url-pattern>
    <url-pattern>/cars/*</url-pattern>
    <url-pattern>/people/*</url-pattern>
</servlet-mapping>

And my dispatcher-servlet.xml map like:

        <bean id="urlMap"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
        <props>
            <prop key="/login">loginController</prop>
            <prop key="/logout">logoutController</prop>
            <prop key="/index">welcomeController</prop>
            <prop key="/cars">listCarsController</prop>
            <prop key="/cars/">listCarsController</prop>
            <prop key="/cars/*">showCarController</prop>
            <prop key="/cars/*/action">actionCarController</prop>
            <prop key="/people">listPeopleController</prop>
            <prop key="/people/">listPeopleController</prop>
            <prop key="/people/*">showPersonController</prop>
            <prop key="/people/*/action">actionPersonController</prop>
        </props>
    </property>
</bean>

This is not working as expected, since when I visit for example /people/1/action, The Dispacher servlet servlet says looking handler for [1/action] and of course it is not as search but with the people/ in front to separate the peoples id to the cars id.

Is like the '*' would be the only URL entered.

By the way I want to keep my URL's extension less, I know that to map the dispatcher to *.htm on the web.xml file, would have taken care of the problem.
But its got to be a way to map the dispatche or to make the dispatcher search for a full url definition.

+1  A: 

You may set a alwaysUseFullPath property:

<bean id="urlMap" 
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 

    <property name = "alwaysUseFullPath" value = "true" />

    ...
</bean>

Alternatively, you may set a single <url-pattern> for the dispatcher servlet. Since it can't be /*, you need to use UrlRewriteFilter.

axtavt
Getting ready to try out alwaysUseFullPath. I already tried UrlRewriteFilter to make extensionless URL .spring but it look awful to fillup my URL maps with .spring at the end besides it doest work with /people/ since there is no file to add the extension...
JorgeO
The UrlRewriteFilter i tried it out using the this post http://blog.ericdaugherty.com/2010/03/web-file-extensions-are-bad.html
JorgeO
Worked! I dont know why is not the default behavior. Or better documented. Thanks
JorgeO