Spring MVC 4.2.4.RELEASE 中文文档
  • Introduction
  • Part I: 目录与翻译注记(Table of contents and Notes)
    • 目录
    • 翻译注记
  • Part II: 文档内容(Documentation contents)
    • Spring Web MVC框架简介
      • Spring Web MVC的新特性
      • 允许其他MVC实现
    • DispatcherServlet
      • WebApplicationContext中特殊的Bean类型
      • 默认的DispatcherServlet配置
      • DispatcherServlet的处理流程
    • 控制器的实现
      • 使用@Controller注解定义一个控制器
      • 使用@RequestMapping注解映射请求路径
      • 定义@RequestMapping注解的处理方法
      • 异步请求的处理
      • 对控制器测试
    • 处理器映射
      • 使用HandlerInterceptor拦截请求
    • 视图解析
      • 使用ViewResolver接口解析视图
      • 视图链
      • 视图重定向
      • 内容协商解析器ContentNegotiatingViewResolver
    • 使用闪存属性FlashAttributes
    • URI构造
      • 为控制器和方法指定URI
      • 在视图中为控制器和方法指定URI
    • 地区信息
      • 获取时区信息
      • Accept请求头解析器AcceptHeaderLocaleResolver
      • Cookie解析器CookieLocaleResolver
      • Session解析器SessionLocaleResolver
      • 地区更改拦截器LocaleChangeInterceptor
    • 主题 themes
      • 关于主题:概览
      • 定义主题
      • 主题解析器
    • Spring的multipart(文件上传)支持
      • 概述
      • 使用MultipartResolver与Commons FileUpload传输文件
      • Servlet 3.0下的MultipartResolver
      • 处理表单中的文件上传
      • 处理客户端发起的文件上传请求
    • 异常处理
      • 处理器异常解析器HandlerExceptionHandler
      • @ExceptionHandler注解
      • 处理一般的Spring MVC异常
      • 使用@ResponseStatus注解业务异常
      • Servlet默认容器错误页面的定制化
    • Web安全
    • "约定优于配置"的支持
      • 控制器类名-处理器映射ControllerClassNameHandlerMapping
      • 模型ModelMap(ModelAndView)
      • 视图-请求与视图名的映射
    • HTTP缓存支持
      • HTTP请求头Cache-Control
      • 对静态资源的HTTP缓存支持
      • 在控制器中设置Cache-Control、ETag和Last-Modified响应头
      • 弱ETag
    • 基于代码的Servlet容器初始化
    • 配置Spring MVC
      • 启用MVC Java编程配置或MVC命名空间
      • 默认配置的定制化
      • 转换与格式化
      • 验证
      • 拦截器
      • 内容协商
      • 视图控制器
      • 视图解析器
      • 资源的服务
      • 回到默认的Servlet来进行资源服务
      • 路径匹配
      • 消息转换器
      • 使用MVC Java编程进行高级定制
      • 使用MVC命名空间进行高级定制
Powered by GitBook
On this page

Was this helpful?

  1. Part II: 文档内容(Documentation contents)
  2. 控制器的实现

使用@Controller注解定义一个控制器

[Original] The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API. However, you can still reference Servlet-specific features if you need to.

@Controller注解表明了一个类是作为控制器的角色而存在的。Spring不要求你去继承任何控制器基类,也不要求你去实现Servlet的那套API。当然,如果你需要的话也可以去使用任何与Servlet相关的特性和设施。

[Original] The @Controller annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations (see the next section).

@Controller注解可以认为是被标注类的原型(stereotype),表明了这个类所承担的角色。分派器(DispatcherServlet)会扫描所有注解了@Controller的类,检测其中通过@RequestMapping注解配置的方法(详见下一小节)。

[Original] You can define annotated controller beans explicitly, using a standard Spring bean definition in the dispatcher’s context. However, the @Controller stereotype also allows for autodetection, aligned with Spring general support for detecting component classes in the classpath and auto-registering bean definitions for them.

当然,你也可以不使用@Controller注解而显式地去定义被注解的bean,这点通过标准的Spring bean的定义方式,在dispather的上下文属性下配置即可做到。但是@Controller原型是可以被框架自动检测的,Spring支持classpath路径下组件类的自动检测,以及对已定义bean的自动注册。

[Original] To enable autodetection of such annotated controllers, you add component scanning to your configuration. Use the spring-context schema as shown in the following XML snippet:

你需要在配置中加入组件扫描的配置代码来开启框架对注解控制器的自动检测。请使用下面XML代码所示的spring-context schema:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.springframework.samples.petclinic.web"/>

    <!-- ... -->

</beans>
Previous控制器的实现Next使用@RequestMapping注解映射请求路径

Last updated 5 years ago

Was this helpful?