您的位置:首页 > 移动开发

Spring MVC SimpleUrlHandlerMapping example

2015-09-13 14:28 399 查看
In Spring MVC application, the
SimpleUrlHandlerMapping
is the most flexible handler mapping class, which allow developer to specify the mapping of URL pattern and handlers explicitly.

The
SimpleUrlHandlerMapping
can be declared in two ways.

1. Method 1 – prop key

The property keys are the URL patterns while the property values are the handler IDs or names.

<beans ...>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/welcome.htm">welcomeController</prop>
<prop key="/*/welcome.htm">welcomeController</prop>
<prop key="/helloGuest.htm">helloGuestController</prop>
</props>
</property>
</bean>

<bean id="welcomeController"
class="com.mkyong.common.controller.WelcomeController" />

<bean id="helloGuestController"
class="com.mkyong.common.controller.HelloGuestController" />

</beans>


2. Method 1 – value

The left side are the URL patterns while the right side are the handler IDs or names, separate by a equal symbol “
=
”.

<beans ...>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/welcome.htm=welcomeController
/*/welcome.htm=welcomeController
/helloGuest.htm=helloGuestController
</value>
</property>
</bean>

<bean id="welcomeController"
class="com.mkyong.common.controller.WelcomeController" />

<bean id="helloGuestController"
class="com.mkyong.common.controller.HelloGuestController" />

</beans>


3. Demo

Both are defined the same handler mappings.

/welcome.htm –> welcomeController.
/{anything}/welcome.htm –> welcomeController.
/helloGuest.htm –> helloGuestController.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc spring mvc