controller和RequestMapping

一、控制器定义
控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现。 控制器解析用户的请求并将其转换为一个模型。在Spring MVC中一个控制器可以包含多个Action(动作、方法)。

使用注解@Controller定义控制器
org.springframework.stereotype.Controller注解类型用于声明Spring类的实例是一个控制器;

Spring可以使用扫描机制来找到应用程序中所有基于注解的控制器类,为了保证Spring能找到你的控制器,需要在配置文件中声明组件扫描。

@Controller
@RequestMapping("/foo")
public class FooController {

// @RequestMapping("/action1")
@RequestMapping({"/","action1","111"})
public String action1(Model model){
model.addAttribute("message","测试action1");
return "foo/index";
}
}
二、@RequestMapping详解
@RequestMapping注释用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。该注解共有8个属性,注解源码如下:

/**
* 用于映射url到控制器类或一个特定的处理程序方法.
*/
//该注解只能用于方法或类型上
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {

/**
* 指定映射的名称
*/
String name() default "";

/**
* 指定请求的路径映射,指定的地址可以是uri模板,别名为path
*/
@AliasFor("path")
String[] value() default {};

/** 别名为value,使用path更加形象
* 只有用在一个Servlet环境:路径映射URI(例如“/myPath.do”)。
* Ant风格的路径模式,同时也支持(例如,“/myPath/*.do”)。在方法层面,在主要的映射在类型级别表示相对路径(例如,“edit.do”)
* 的支持。路径映射的URI可能包含占位符(例如“/$ {}连接”)
*/
@AliasFor("value")
String[] path() default {};

/**
* 指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE. 收窄请求范围 The
* HTTP request methods to map to, narrowing the primary mapping: GET, POST,
* HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
*/
RequestMethod[] method() default {};

/**
* 映射请求的参数,收窄请求范围 The parameters of the mapped request, narrowing the
* primary mapping.
*/
String[]params() default {};

/**
* 映射请求头部,收窄请求范围 The headers of the mapped request, narrowing the primary
* mapping. RequestMapping(value = "/something", headers =
* "content-type=text/*")
*/
String[] headers() default {};

/**
* 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html,收窄请求范围 The
* consumable media types of the mapped request, narrowing the primary
* mapping.
*/
String[] consumes() default {};

/**
* 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回 The producible media types
* of the mapped request, narrowing the primary mapping. produces =
* "text/plain" produces = {"text/plain", "application/*"} produces =
* "application/json; charset=UTF-8"
*/
String[] produces() default {};
}

value:指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);

@RequestMapping({"/","action1","111"})
//仅有value元素可省略
public String action1(Model model){
model.addAttribute("message","测试action1");
return "foo/index";
}
method:指定请求的method类型, GET、POST、PUT、DELETE等;

@GetMapping("/action5") //这是一个组合注解等同下面这个注解
// @RequestMapping(value = "/action5",method = RequestMethod.GET)
public String action5(Model model){
model.addAttribute("message","请求谓语动词是GET");
return "foo/index";
}
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

// 请求内容类型必须为text/html
@RequestMapping(value = "/action6",consumes = "!text/html")
public String action6(Model model){
model.addAttribute("message","请求的提交内容类型(Content-Type)是text/html");
return "foo/index";
}
produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

//客户端接收json且编码为utf-8,多数浏览器Accept设置的为*/*,接收任意类型
@RequestMapping(value = "/action7",produces="application/json; charset=UTF-8")
public String action7(Model model) {
model.addAttribute("message", "客户端可以接收的类型是application/json; charset=UTF-8");
return "foo/index";
}
params: 指定request中必须包含某些参数值是,才让该方法处理。

//请求的参数必须包含id=215与name不等于abc
@RequestMapping(value = "/action8",params={"id=215","name!=abc"})
public String action8(Model model) {
model.addAttribute("message", "请求的参数必须包含id=215与name不等于abc");
return "foo/index";
}
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

//请求头部信息中必须包含Host=localhost:8080
@RequestMapping(value = "/action9",headers="Host=localhost:8080")
public String action9(Model model) {
model.addAttribute("message", "请求头部信息中必须包含Host=localhost:8080");
return "foo/index";
}
————————————————
版权声明:本文为CSDN博主「qq_泥瓦工」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42218123/article/details/80526520

原文地址:https://www.cnblogs.com/ysd139856/p/12685177.html