ControllerClassNameHandlerMapping控制器是根据控制器短类名和URL请求匹配,如:
* WelcomeController:对应/welcome*的Url请求
* UserController:对应/user*的url请求
具体来说是将控制器的Controller后缀移除(IndexController=>Index),再将剩余部分转为小写(index)得到的字符串作为请求URL的配置前缀
配置文件代码如下:
# <!-- 根据控制器短类名和URL请求匹配 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean name="indexController" class="cn.wangjicn.web.IndexController">
<property name="greeting" value="忘记的资料库欢迎你!"/>
</bean>
<bean name="userController" class="cn.wangjicn.web.user.UserController"/>
如上面的配置对应的URL为
* IndexController 为 /index*
* UserController 为 /user*
13.11.1. The Controller - ControllerClassNameHandlerMapping
The ControllerClassNameHandlerMapping class is a HandlerMapping implementation that uses a convention to determine the mapping between request URLs and the Controller instances that are to handle those requests.
An example; consider the following (simplistic) Controller implementation. Take especial notice of the name of the class.
public class ViewShoppingCartController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
// the implementation is not hugely important for this example...
}
}
Here is a snippet from the attendent Spring Web MVC configuration file...
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="viewShoppingCart" class="x.y.z.ViewShoppingCartController">
<!-- inject dependencies as required... -->
</bean>
The ControllerClassNameHandlerMapping finds all of the various handler (or Controller) beans defined in its application context and strips 'Controller' off the name to define its handler mappings.
Let's look at some more examples so that the central idea becomes immediately familiar.
*
WelcomeController maps to the '/welcome*' request URL
*
HomeController maps to the '/home*' request URL
*
IndexController maps to the '/index*' request URL
*
RegisterController maps to the '/register*' request URL
*
DisplayShoppingCartController maps to the '/displayshoppingcart*' request URL
(Notice the casing - all lowercase - in the case of camel-cased Controller class names.)
In the case of MultiActionController handler classes, the mappings generated are (ever so slightly) more complex, but hopefully no less understandable. Some examples (all of the Controller names in this next bit are assumed to be MultiActionController implementations).
*
AdminController maps to the '/admin/*' request URL
*
CatalogController maps to the '/catalog/*' request URL
If you follow the pretty standard convention of naming your Controller implementations as xxxController, then the ControllerClassNameHandlerMapping will save you the tedium of having to firstly define and then having to maintain a potentially looooong SimpleUrlHandlerMapping (or suchlike).
The ControllerClassNameHandlerMapping class extends the AbstractHandlerMapping base class so you can define HandlerInterceptor instances and everything else just like you would with many other HandlerMapping implementations.
分享到:
相关推荐
本文将深入探讨NetBeans8.1中如何使用Spring MVC进行开发,以及`ControllerClassNameHandlerMapping`和`SimpleUrlHandlerMapping`这两个关键组件的作用。 首先,让我们了解NetBeans8.1如何生成Spring MVC的基础代码...
3 URL Mapping: ControllerClassNameHandlerMapping 映射控制器 4 使用MultiActionController 5 所有service利用spring注入到controller中,利用注解 6 数据库连接串单独在properties文件配置 7 在spring中配置数据...
3. **ControllerClassNameHandlerMapping**: - 通过控制器类名来映射请求。 - 实现简单但不够灵活。 4. **RequestMappingHandlerMapping**: - 是Spring MVC中最常用的实现。 - 支持使用`@RequestMapping`注解...
控制器类名-处理器映射 ControllerClassNameHandlerMapping 是一种基于控制器类名来映射 URL 请求的方式。 使用 MultipartResolver 与 Commons FileUpload 处理文件上传请求部分详细讲述了如何配置和使用文件上传的...
* ControllerClassNameHandlerMapping:通过使用控制器的类名作为URL基础将控制器映射到URL。 * DefaultAnnotationHandlerMapping:将请求映射给使用@RequestMapping注解的控制器和控制器方法。 * ...
控制器类名-处理器映射ControllerClassNameHandlerMapping允许通过类名直接映射请求到处理器。 在控制器中设置Cache-Control、ETag和Last-Modified响应头可指导客户端如何缓存响应内容,以及如何在后续请求中使用...
Spring MVC提供了“约定优于配置”的Web安全支持,可以通过配置`ControllerClassNameHandlerMapping`来实现基于控制器类名的映射。此外,还可以自定义异常解析器处理各种异常,包括业务异常和HTTP状态码的映射。 ...
- **ControllerClassNameHandlerMapping**:通过URL映射到类名。 ##### 4. HandlerAdapter **HandlerAdapter**用于执行具体的Handler。SpringMVC提供了多种类型的Adapter,比如: - **...
- `ControllerClassNameHandlerMapping`:根据请求URL自动推断处理器的类名。 - `RequestMappingHandlerMapping`:使用@RequestMapping注解自动映射。 ##### 3. HandlerAdapter(处理器适配器) - **定义**:...
- **控制器类名-处理器映射 ControllerClassNameHandlerMapping**:自动将控制器类名映射到处理器。 #### 模型 (Model) - **ModelMap (ModelAndView)**:用于在控制器和视图之间传递数据。 #### HTTP 缓存支持 - *...
13.2.2使用controllerclassnamehandlermapping 13.2.3使用元数据映射控制器 13.2.4使用多映射处理器 13.3用控制器处理请求 13.3.1处理命令 13.3.2处理表单提交 13.3.3用向导处理复杂表单 13.3.4使用一次性...
ControllerClassNameHandlerMapping Conventions CookieGenerator CookieLocaleResolver CookieThemeResolver CosMailSenderImpl CosMultipartHttpServletRequest CosMultipartResolver CronTriggerBean ...
13.11.1. 对控制器的支持: ControllerClassNameHandlerMapping 13.11.2. 对模型的支持:ModelMap (ModelAndView) 13.11.3. 对视图的支持: RequestToViewNameTranslator 13.12. 其它资源 14. 集成视图技术 ...
13.11.1. 对控制器的支持:ControllerClassNameHandlerMapping 13.11.2. 对模型的支持:ModelMap(ModelAndView) 13.11.3. 对视图的支持:RequestToViewNameTranslator 13.12. 基于注解的控制器配置 13.12.1. ...
13.11.1. 对控制器的支持: ControllerClassNameHandlerMapping 13.11.2. 对模型的支持:ModelMap (ModelAndView) 13.11.3. 对视图的支持: RequestToViewNameTranslator 13.12. 其它资源 14. 集成视图技术 ...
13.11.1. 对控制器的支持:ControllerClassNameHandlerMapping 13.11.2. 对模型的支持:ModelMap(ModelAndView) 13.11.3. 对视图的支持:RequestToViewNameTranslator 13.12. 基于注解的控制器配置 13.12.1. ...
13.11.1. 对控制器的支持: ControllerClassNameHandlerMapping 13.11.2. 对模型的支持:ModelMap (ModelAndView) 13.11.3. 对视图的支持: RequestToViewNameTranslator 13.12. 其它资源 14. 集成视图技术 14.1. ...