- 内容提要
- 作者简介
- 译者简介
- 前言
- HTTP
- Servlet 和 JSP
- 下载 Spring 或使用 STS 与 Maven/Gradle
- 手动下载 Spring
- 使用 STS 和 Maven/Gradle
- 下载 Spring 源码
- 本书内容简介
- 下载示例应用
- 第 1 章Spring 框架
- 第 2 章模型 2 和 MVC 模式
- 第 3 章Spring MVC 介绍
- 第 4 章基于注解的控制器
- 第 5 章数据绑定和表单标签库
- 第 6 章转换器和格式化
- 第 7 章验证器
- 第 8 章表达式语言
- 第 9 章JSTL
- 第 10 章国际化
- 第 11 章上传文件
- 第 12 章下载文件
- 第 13 章应用测试
- 附录 A Tomcat
- 附录 B Spring Tool Suite 和 Maven
- 附录 C Servlet
- 附录 D JavaServer Pages
- 附录 E 部署描述符
4.1 Spring MVC 注解类型
使用基于注解的控制器的几个优点。其一,一个控制器类可以处理多个动作(而实现了 Controller 接口的一个控制器只能处理一个动作)。这就允许将相关的操作写在同一个控制器类中,从而减少应用程序中类的数量。
其二,基于注解的控制器的请求映射不需要存储在配置文件中。使用 RequestMapping 注释类型,可以对一个方法进行请求处理。
Controller 和 RequestMapping 注释类型是 Spring MVC API 最重要的两个注解类型。本章重点介绍这两个,并简要介绍了一些其他不太流行的注解类型。
4.1.1 Controller 注解类型
org.springframework.stereotype.Controller 注解类型用于指示 Spring 类的实例是一个控制器。下面是一个带注解 @Controller 的例子。
package com.example.controller;
import org.springframework.stereotype;
...
@Controller
public class CustomerController {
// request-handling methods here
}Spring 使用扫描机制来找到应用程序中所有基于注解的控制器类。为了保证 Spring 能找到你的控制器,需要完成两件事情。首先,需要在 Spring MVC 的配置文件中声明 spring- context,如下所示:
< beans
...
xmlns:context="http://www.springframework.org/schema/context"
...
>然后,需要应用<component-scan/>元素,如下所示:
< context:component-scan base-package="basePackage"/>
请在<component-scan/>元素中指定控制器类的基本包。例如,若所有的控制器类都在 com.example.controller 及其子包下,则需要写一个如下所示的<component-scan/>元素:
<context:component-scan base-package="com.example.controller"/>现在,整个配置文件看上去如下所示:
< ?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="com.example.controller"/>
< !-- ... -->
< /beans>请确保所有控制器类都在基本包下,并且不要指定一个太广泛的基本包(如指定 com.example,而非 com.example.controller,前者就更广泛),因为这会使得 Spring MVC 扫描了无关的包。
4.1.2 RequestMapping 注解类型
现在,我们需要在控制类的内部为每一个动作开发相应的处理方法。要让 Spring 知道用哪一种方法来处理它的动作,需要使用 org.springframework.web.bind.annotation.Request Mapping 注解类型映射的 URI 与方法。
RequestMapping 注解类型的作用同其名字所暗示的:映射一个请求和一种方法。可以使用 @RequestMapping 注解一种方法或类。
一个采用 @RequestMapping 注解的方法将成为一个请求处理方法,并由调度程序在接收到对应 URL 请求时调用。
下面是一个 RequestMapping 注解方法的控制器类。
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
...
@Controller
public class CustomerController {
@RequestMapping(value = "/input-customer ") public String inputCustomer() { // do something here return "CustomerForm"; } }使用 RequestMapping 注解的 value 属性将 URI 映射到方法。在上面的例子中,我们将 input-customer 映射到 inputCustomer 方法。这样,可以使用如下 URL 访问 inputCustomer 方法。
http://domain/context/input-customer由于 value 属性是 RequestMapping 注释的默认属性,因此,若只有唯一的属性,则可以省略属性名称。换句话说,如下两个标注含义相同。
@RequestMapping(value = "/input-customer ")
@RequestMapping("/input-customer ")但如果有多个属性时,就必须写入 value 属性名称。
请求映射的值可以是一个空字符串,此时该方法被映射到以下网址:
http://domain/contextRequestMapping 除了具有 value 属性外,还有其他属性。例如,method 属性用来指示该方法仅处理哪些 HTTP 方法。
例如,仅当在 HTTP POST 或 PUT 方法时,才访问到下面的 ProcessOrder 方法。
...
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
...
@RequestMapping(value="/process-order", method={RequestMethod.POST, RequestMethod.PUT}) public String processOrder() { // do something here return "OrderForm"; }若 method 属性只有一个 HTTP 方法值,则无需花括号。例如,
@RequestMapping(value="/process-order", method=RequestMethod.POST)如果没有指定 method 属性值,则请求处理方法可以处理任意 HTTP 方法。
此外,RequestMapping 注解类型也可以用来注解一个控制器类,如下所示:
import org.springframework.stereotype.Controller;
...
@Controller
@RequestMapping(value="/customer")
public class CustomerController {在这种情况下,所有的方法都将映射为相对于类级别的请求。例如,下面的 deleteCustomer 方法。
...
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
...
@Controller
@RequestMapping("/customer")
public class CustomerController {
@RequestMapping(value="/delete",
method={RequestMethod.POST, RequestMethod.PUT})
public String deleteCustomer() {
// do something here
return ...;
}由于控制器类的映射使用“/customer”,而 deleteCustomer 方法映射为“/delete”,则如下 URL 会映射到该方法上。
http://domain/context/customer/delete绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论