在Spring MVC 中,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。
@RequestMapping 既可修饰类,也可以修饰方法。当修饰类和方法时,访问的地址是类 + 方法。
浅看一下源码:
// Target 中这两个属性表示 @RequestMapping 注解可以用在方法和类上
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
String[] value() default {};
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
@RequestMapping注解中除了 name() 返回的字符串外,有七个属性。
| 属性名 | 属性说明 |
|---|---|
| value、path | 指定请求的url地址。value和path的作用相同,可以互相替换。value是默认属性,所以@RequestMapping(value=“/example”)和@RequestMapping(“/example”)是等价的。 |
| method | 指定请求的method类型, GET(查)、POST(增)、PUT(改)、DELETE(删)等 |
| params | 指定request中必须包含某些参数值是,才让该方法处理。 |
| headers | 指定request中必须包含某些指定的header值,才能让该方法处理请求。 |
| consumes | 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html; |
| produces | 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回; |
value属性通过请求的请求地址匹配请求映射。valuel属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求,value属性必须设置。
@Controller // 必须用 Controller 注解
public class Demo1 {
// 路由规则注册:访问页面的地址
@RequestMapping("/test1")
public String sayTest1() {
// 跳转到 templates 下的 demo2.html
return "demo2";
}
}
// 访问地址: http://localhost:8080/test1
或者
@Controller // 必须用 Controller 注解
@ResponseBody // 返回一个text/html 信息(非视图文件)
public class Demo2 {
// 路由规则注册:访问页面的地址
@RequestMapping("/test2")
public String sayTest2() {
return "hello,this is Test
";
}
}
// 访问地址: http://localhost:8080/test2
1.首先你得创建一个数据实体类
import lombok.Data;
// @Data注解在lombok下,可以节省代码空间
// 有了@Data注解,我们就不需要手动写get、set、toString和构造方法了,它内置这些方法
@Data
public class Student {
private final String name;
private final String brief;
private final String birthday;
}
2.创建一个视图文件(just-a-view.html),内部自定义数据对象
DOCTYPE html>
<html lang="zh-hans">
<head>
<meta charset="UTF-8">
<title>仅仅是一个视图文件 | 实际上是一个模板文件title>
head>
<body>
<h1 th:text="${person.name}">这里的内容会被替换h1>
<p th:text="${person.brief}">这里的内容会被替换p>
<p th:text="${person.birthday}">这里的内容会被替换p>
body>
html>
3.经过Controller进行Model数据绑定
@Controller
public class DemoController {
// 支持所有的 HTTP 方法,@RequestMapping 默认是 get方式的请求
@RequestMapping("/hello")
public String hello(Model model) {
Student student = new Student("张三", "我是张三,很高兴认识大家", "2000-07-26");
//往前台传数据,可以传对象,通过表达式 ${}可以获取到
model.addAttribute("person", student);
return "just-a-view";
}
}
// 访问地址: http://localhost:8080/hello
@RequestMapping 中的 method 主要用来定义接收浏览器发来的何种请求。
Http规范定义了多种请求资源的方式,最基本的有四种,分别为:GET(查)、POST(增)、PUT(改)、DELETE(删),而URL则用于定位网络上的资源相当于地址的作用,配合四种请求方式,可以实现对URL对应的资源的增删改查操作。
@Controller
public class UserController1 {
// 只支持 GET 方法
@RequestMapping(value = "/login1", method=RequestMethod.GET)
public String login1() {
return "success";
}
// 可简写为
@GetMapping("/login2")
public String login2() {
return "success";
}
}
// 访问地址: http://localhost:8080/login1
这时,如果浏览器发来的请求不是GET的话,将收到浏览器返回的错误提示,也就是得通过链接的方式。
@Controller
public class UserController2 {
// 只支持 POST 方法
@PostMapping("/login3")
public String login3() {
return "success";
}
}
这时,必须通过表单的方式发送请求,否则将收到浏览器返回的错误提示
<form action="user/login" method="post">
<input type="submit" value="使用Post发送请求"/>
form>
@Controller
public class UserController3 {
// 支持 HTTP GET 和 POST 方法
@RequestMapping(value = "/login4", method = {RequestMethod.GET, RequestMethod.POST})
public String login4() {
return "success";
}
}
params 属性表示请求参数,也就是追加在URL上的键值对,多个请求参数以&隔开。
定义实体类:
@Data
public class Cat {
public final String name;
public final int age;
}
不需要params属性的情况:
@Controller
public class ParamsController {
@GetMapping("/cat1")
@ResponseBody
public Cat createCat() {
// 返回对象会被 JSON 序列后输出
return new Cat("小猫", 3);
}
}
// 访问地址: http://localhost:8080/cat1
// 网页结果:{"name":"小猫","age":3}
@Controller
public class ParamsController {
@GetMapping("/cat2")
@ResponseBody
public Cat createCat2(String name, String age) {
return new Cat(name, age);
}
// 键入地址: http://localhost:8080/cat3?name=小米&age=6
@GetMapping(value = "cat3", params={"name=小米","age=4"})
@ResponseBody
public Cat createCat3(String name, String age) {
return new Cat(name, age);
}
// 固定地址: http://localhost:8080/cat3?name=小米&age=6
}
@Controller
public class ParamsController {
@GetMapping("/cat4")
@ResponseBody
public Cat createCat4(Cat cat) {
// 可以直接用对象请求。只要能正确传参,Spring内部会构造对象
return cat;
}
}
// 键入地址: http://localhost:8080/cat3?name=小米&age=6
通过 @RequestMapping 中的 headers 属性,可以限制客户端发来的请求
@Controller
public class HeadersController {
// 表示只接收本机发来的请求
@RequestMapping(path = "/login", headers="Host=localhost:8080")
public String login() {
return "success";
}
}
produces属性指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。
public class DemoController {
// 指定输出格式为 text, 字库为 utf-8
@RequestMapping(value = "/hello", produces = "text/plain; charset=utf-8")
@ResponseBody
public String hello(@RequestParam(value = "target", defaultValue = "世界") String target) {
// 设置 target 默认值为“世界”,如果请求时指定了target 的值,则输出指定值
return "你好, " + target;
}
}
// 键入地址: http://localhost:8080/hello
// 输出结果: 你好,世界
// 键入地址: http://localhost:8080/hello?target=小米
// 输出结果: 你好,小米
开始学习Spring已经有一周了,给我的感觉就是起步比较难。框架这种东西你不需要知道原理就可以使用,这对于开发来说确实省事儿了不少。但是对于初学者来说,确实有不小的难度,因为这里面的东西太多了,如果没有时间的积累,你很难在短期内认识那么多的工具,更别说搞懂原理了。
不过也会正常,“万事开头难”。我们能做的只有慢慢努力了。这篇文章仅仅是初学的总结,后续还需要不断的补充和改进!!!
总结:
提示:这里对文章进行总结:
以上就是今天的学习内容,本文是SpringMVC的学习,又认识了一个注解@RequestMapping,并且学习了它的各种用法。之后的学习内容将持续更新!!!