`
coreoak
  • 浏览: 65297 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring MVC2

阅读更多
4) Handler Mappings

When the Client Request reaches the Dispatcher Servlet, the Dispatcher Servlet tries to find the appropriate Handler Mapping Object to map between the Request and the Handling Object. A Handler Mapping provides an abstract way that tell how the Client's Url has to be mapped to the Handlers. Four concrete variation of Handler Mapping are available. They are defined as follows

    * BeanNameUrl HandlerMapping
    * CommonsPathMap HandlerMapping
    * ControllerClassName HandlerMapping
    * SimpleUrl HandlerMapping

All the above Handler Mapping objects are represented as BeanNameUrlHandlerMapping, CommonsPathMapHandlerMapping, ControllerClassNameHandlerMapping and SimpleUrlHandlerMapping in the org.springframework.web.servlet package respectively. Let us see the functionalities and the differences in usage one by one.
4.1) BeanNameUrl HandlerMapping

This is the simplest of the Handler Mapping and it is used to map the Url that comes from the Clients directly to the Bean Object. In the later section, we will see that the Bean is nothing but a Controller object. For example, consider that the following are the valid Url in a Web Application that a Client Application can request for.


http://myserver.com/eMail/showAllMails
http://myserver.com/eMail/composeMail
http://myserver.com/eMail/deleteMail


Note that the Url (excluding the Application Context) in the above cases are 'showAllMails', 'composeMail' and 'deleteMail'. This means that the Framework will look for Bean Definitions with Identifiers 'showAllMails', 'composeMail' and 'deleteMail'. Consider the following Xml code snippet in the Configuration file,


<beans>

    <bean name="/showAllMails.jsp"
    class="com.javabeat.net.ShowAllMailsController">
    </bean>

    <bean name="/composeMail.jsp"
    class="com.javabeat.net.ComposeMailController">
    </bean>

    <bean name="/ deleteMail.jsp"
    class="com.javabeat.net.DeleteMailController">
    </bean>

</beans>


So, in BeanNameUrl Handler Mapping, the Url of the Client is directly mapped to the Controller. To enable this kind of Handler Mapping in the Application, the Configuration file should have a similar kind of definition like the following,


<beans>

    �
   <bean id="beanNameUrl"
   class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    �

</beans>


4.2) CommonsPathMap HandlerMapping

This is a rarely used Handler Mapping in which case, the name of the Url to which the Controller has to be mapped is specified directly in the Source file of the Controller. Considering the previous example, if we want to map 'showAllMails', 'composeMail' and 'deleteMail' to Controllers namely ShowAllMailsController, ComposeMailController and DeleteMailController, then the mapping information must be specified in the form of meta-data in the source files inside the Javadoc comments. Consider the following Controller Definitions,


/**
*@@ org.springframework.web.servlet.handler.commonsattributes.
*PathMap("/showAllMails.jsp")
*/
public class ShowAllMailsController{
}

/**
*@@ org.springframework.web.servlet.handler.commonsattributes.
*PathMap("/composeMail.jsp")
*/
public class ComposeMailController{
}

/**
*@@ org.springframework.web.servlet.handler.commonsattributes.
*PathMap("/deleteMail.jsp")
*/
public class DeleteMailController {
}


The attribute must point to org.springframework.web.servlet.handler.commonsattributes.PathMap. By defining Controllers in this way, one more additional compilation step is needed. That is to make the availability of this attribute in the Class files, this Java Source has to be compiled with the Commons Attribute Compiler which comes along with the Spring Distribution. As before, to enable this kind of mapping , the Configuration File should have an entry similar to this,


<beans>

    <bean id="metaHandlerMapping" class="org.springframework.web.servlet.handler.
    metadata.CommonsPathMapHandlerMapping"/>

</beans>


4.3) ControllerClassName HandlerMapping

In this kind of Handler Mapping, the name of the Controller is taking directly from the Url itself with slight modifications. For example, let us assume that the Client request ends with Url as shown below,


http://myserver.com/emailApp/showInbox.jsp
http://myserver.com/emailApp/showDeletedItems.jsp


And as such, we have a Controller definition by name ShowController as follows,

ShowController.java


public class ShowController{
}


Also the Configuration file is made to activate this kind of Handler Mapping by making the following definition,


<beans>

    <bean id="controllerClassName" class="org.springframework.web.servlet.handler.
    metadata.ControllerClassNameHandlerMapping"/>

</beans>


The first thing the Framework does it, it will traverse through the List of Controllers defined in the Configuration File and perform these actions. For the Controller ShowController, then Framework will remove the Controller String and then lowercase the first letter. In our case the string now becomes show. Now whatever Client Request matches the pattern /show*, then the ShowController will be invoked.
4.4) SimpleUrl HandlerMapping

This is the Simplest of all the Handler Mappings as it directly maps the Client Request to some Controller object. Consider the following Configuration File,


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

    <property name="mappings">
        <props>
            <prop key="/showAllMails.jsp">showController</prop>
            <prop key="/composeMail.jsp">composeController</prop>
            <prop key="/deleteMail.jsp">deleteController</prop>
        </props>
    </property>

</bean>


The set of mappings is encapsulated in the 'property' tag with each defined in a 'prop' element with the 'key' attribute being the Url, the value being the Identifier of the Controller Objects. Note that the Beans for the above Identifiers should be defined somewhere in the Configuration File.
5) Handler Adapters

It is important to understand that the Spring Framework is so flexible enough to define what Components should be delegated the Request once the Dispatcher Servlet finds the appropriate Handler Mapping. This is achieved in the form of Handler Adapters. If you remember in the Spring Work flow section, that it is mentioned once the Dispatcher Servlet chooses the appropriate Handler Mapping, the Request is then forwarded to the Controller object that is defined in the Configuration File. This is the default case. And this so happens because the Default Handler Adapter is Simple Controller Handler Adapter (represented by org.springframework.web.servlet.SimpleControllerHandlerAdapter), which will do the job of the Forwarding the Request from the Dispatcher to the Controller object.

Other types of Handler Adapters are Throwaway Controller HandlerAdapter (org.springframework.web.servlet.ThrowawayControllerHandlerAdapter) and SimpleServlet HandlerAdapter (org.springframework.web.servlet.SimpleServletHandlerAdapter). The Throwaway Controller HandlerAdapter, for example, carries the Request from the Dispatcher Servlet to the Throwaway Controller (discussed later in the section on Controllers) and Simple Servlet Handler Adapter will carry forward the Request from the Dispatcher Servlet to a Servlet thereby making the Servlet.service() method to be invoked.

If, for example, you don't want the default Simple Controller Handler Adapter, then you have to redefine the Configuration file with the similar kind of information as shown below,


<bean id="throwawayHandler" class = "org.springframework.web.servlet.mvc.throwaway.
    ThrowawayControllerHandlerAdapter"/>


or


<bean id="throwawayHandler" class="org.springframework.web.servlet.mvc.throwaway.
    SimpleServletHandlerAdapter"/>


Even, it is possible to write a Custom Handler Adapter by implementing the HandlerAdapter interface available in the org.springframework.web.servlet package.
分享到:
评论

相关推荐

    spring mvc2-2

    spring mvc2-1spring mvc2-1spring mvc2-1spring mvc2-1spring mvc2-1spring mvc2-1

    精通Spring MVC 4

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...

    spring mvc2-1

    spring mvc2-1

    Spring MVC 基础实例源码01

    2. **DispatcherServlet**:Spring MVC的入口点,它是一个前端控制器,接收所有HTTP请求,并根据配置的HandlerMapping转发到相应的Controller。 3. **Controller**:Controller是处理用户请求的组件,通常使用注解...

    Spring MVC jar包

    Spring MVC 是一个基于Java的轻量级Web应用框架,它为开发者提供了模型-视图-控制器(MVC)架构,使开发人员能够更好地组织和分离应用程序的业务逻辑、数据处理和用户界面。Spring MVC是Spring框架的一个核心组件,...

    最全最经典spring-mvc教程

    Spring MVC 是一款强大的Java Web开发框架,用于构建高效、可维护和模块化的Web应用程序。它作为Spring框架的一部分,提供了一种优雅的方式来处理HTTP请求和响应,使得开发者可以专注于业务逻辑而不是底层实现。在这...

    Spring MVC 4.2.3

    2. **Java配置增强**:Spring 4.2.x系列进一步提升了Java配置的支持,使得在没有XML配置的情况下也能轻松地配置Spring MVC。 3. **RESTful支持**:Spring MVC提供了对RESTful风格的HTTP方法(如GET、POST、PUT、...

    Spring.MVC-A.Tutorial-Spring.MVC学习指南 高清可复制版PDF

    Spring MVC 是一个基于Java的轻量级Web应用框架,它是Spring框架的重要组成部分,主要用于构建Web应用程序的后端控制器。这个教程“Spring MVC - A Tutorial”旨在帮助开发者深入理解和掌握Spring MVC的核心概念和...

    spring mvc 4.0

    Spring MVC是Spring框架的一个核心模块,专为构建Web应用程序提供模型-视图-控制器(MVC)架构。在Spring MVC 4.0版本中,它引入了许多改进和新特性,以提升开发效率和应用程序的性能。 1. **依赖注入**:Spring ...

    Mastering Spring MVC 4(2015.09)源码

    Spring MVC 是一个强大的Java Web开发框架,它是Spring框架的一部分,专为构建高度可扩展和模块化的Web应用程序而设计。在2015年的版本中,Spring MVC 4已经相当成熟,提供了许多特性来简化开发流程并提高开发效率。...

    Spring MVC + Mybatis+Spring实现的个人博客系统

    这是一个基于Spring MVC、Mybatis和Spring框架实现的个人博客系统,涵盖了Web开发中的后端架构设计、数据库管理和前端展示等多个方面。以下将详细介绍这个系统的关键知识点: **1. Spring MVC** Spring MVC是Spring...

    Spring MVC所需jar包

    2. **Spring MVC 模块**:`spring-webmvc.jar` 是 Spring MVC 的核心组件,它实现了 MVC 设计模式,提供请求处理、视图解析等功能。这个 jar 包是构建基于 Spring 的 Web 应用必不可少的。 3. **Spring Web 模块**...

    spring mvc框架依赖全面jar

    Spring MVC 是一个基于 Java 的轻量级Web应用框架,它为构建模型-视图-控制器(MVC)架构的应用程序提供了强大的支持。在本压缩包中包含了一系列与Spring MVC相关的jar文件,这些文件是构建和运行Spring MVC项目所...

    Spring MVC 教程快速入门 深入分析

    相较于Struts2,Spring MVC避免了一些可能导致性能下降的特性,如值栈、OGNL表达式等。 二、Spring MVC核心类与接口:Spring MVC架构中包含许多核心组件,如DispatcherServlet、HandlerMapping、Controller、...

    Spring MVC使用Demo

    Spring MVC是Spring框架的一个核心模块,专用于构建Web应用程序。这个"Spring MVC使用Demo"提供了实践操作,帮助开发者深入理解Spring MVC的开发环境配置、注解的使用以及工作原理。 首先,Spring MVC的设计模式...

    spring MVC .docx

    Spring MVC 是一个基于Java的轻量级Web应用框架,它是Spring框架的重要组成部分,主要用于构建Web应用程序的后端控制器。Spring MVC的设计目标是提供一个清晰的组件化架构,使得开发者可以独立地开发和测试控制器、...

    基本的spring mvc + spring security实现的登录(无数据库)

    2. **Spring Security**: - **认证与授权**:Spring Security提供了一套完整的认证和授权机制。在这个无数据库的登录示例中,可能使用内存中的UserDetails和UserDetailsService实现认证。 - **配置**:可以使用...

Global site tag (gtag.js) - Google Analytics