开发者

Have Spring DispacherServlet resolve the correct URLmap

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

/pe开发者_如何学Goople/ - 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜