• Spring MVC SimpleUrlHandlerMapping example


    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.

  • 相关阅读:
    git使用
    onethink常用标签的使用示例
    thinkphp中 select() 和find() 方法的区别
    CSS3Ps -Photoshop图层特效转CSS3代码
    普通公司网站代码片段合辑
    IE hack大全
    PHP四种基础算法详解:冒泡,选择,插入和快速排序法
    PHP编程效率的20个要点
    php实现AES/CBC/PKCS5Padding加密解密(又叫:对称加密)
    浏览器桌面提醒,适用于网站“新消息提醒”
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4755313.html
Copyright © 2020-2023  润新知