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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 【姓名】焦杨 【派别】玉印派 【导师】王玉印 袁文魁 【分舵】图龙宝刀 【舵主】焦典 品甘苦 每个有理想的人,都会...
    我是骄阳似火阅读 408评论 2 1
  • 盼望你回家的声音,见到你失落无比,在一起无聊至极。我为什么不能像个小女人?被你宠爱?我以为你会为我改变什么?结果只...
    A攀澜阅读 169评论 0 0
  • “呃,咳咳……”少女一股脑从床上爬起来,刚刚那股似乎要使她窒息的感觉却依旧存在……“你竟然还能活着,真是不容易呢!...
    USSR大本营阅读 2,967评论 1 7
  • 昨晚看了《从你的全世界路过》,看完之后,没有一点感觉。不禁开始怀疑,是不是我有点铁石心肠。看朋友圈在刷这部电影已经...
    菜菜小仙女阅读 309评论 0 0