美文网首页
Spring的Mapping

Spring的Mapping

作者: 匿名wm | 来源:发表于2018-10-06 11:19 被阅读0次

1.有什么用?

根据url找到对应的Controller,这里的url包括访问带有注解的Controller、静态资源、实现Controller接口或接口子类的处理器、实现Servlet接口或者接口子类的请求。

2.有哪些Mapping?

2.1、BeanNameUrlHandlerMapping

全名:org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

DispatchServlet默认的Mapping之一,根据请求的url与spring容器中定义的处理器bean的name属性值进行匹配,找到对应的处理器bean实例。如下:

<bean id = "/index.do" class = "com.company.handlers.IndexController">

且bean的名称必须是“/”开头。

2.2、DefaultAnnotationHandlerMapping

全名:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

DispatchServlet默认的Mapping之一,根据@Controller注解配置找到对应的Controller,常用。但是Spring3.2之后已经废弃,改用:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping。

如果使用,一般和org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter配合使用。RequestMappingHandlerMapping一般与org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter配合使用。

2.3、SimpleUrlHandlerMapping

全名:org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

例如:

<bean id="simpleUrlAction" class="com.plateno.web.action.SimpleUrlAction" />//SimpleUrlAction需要实现Controller或MultiActionController接口

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

<property name="urlMap">

    <map>

        <entry key="/index.html" value-ref = "simpleUrlAction" />

        <entry key="/index2.html" value-ref = "simpleUrlAction" />

        <entry key="/login.html" value-ref = "simpleUrlAction" />

    </map>

</bean>

一般SimpleUrlHandlerMapping是配置defaultHandler为UrlFilenameViewController,将请求的URL路径直接转为逻辑视图名并返回的转换控制器,不需要功能处理,也就不需要再写Controller,直接根据URL解析出逻辑视图名。比如

<bean name="/index.do"  class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>  

<bean name="/index2/**"  class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>  

访问/index.do直接跳转到index.html或其他后缀的index页面,具体index后面的后缀可通过视图解析器配置。

也可以配置ParameterizableViewController,但是这个需要在ParameterizableViewController的property配置viewName属性的值,比如访问index.html页面,需要配置

<bean name="/parameterizableViewController.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">  

    <property name="viewName" value="index" />

</bean>  

则请求的/parameterizableViewController.do会直接跳到index.jsp或其他后缀的index页面。多个请求,则需要配置多个bean。可以根据需要修改该url要跳转的地址,但是UrlFilenameViewController只能根据配置的bean的name值进行跳转到name对应的页面。

比如不想修改请求地址,但是响应改为其他页面了,则用ParameterizableViewController较为合适,而UrlFilenameViewController需要请求方同时要修改请求url。

相关文章

网友评论

      本文标题:Spring的Mapping

      本文链接:https://www.haomeiwen.com/subject/zamewftx.html