SpringMVC_总结_03_SpringMVC相关注解
一、前言
在前面的小节中,我们配置了注解驱动和自动扫描包,然后就可以使用SpringMVC相关注解了。
二、@Controller
@Controller用来修饰类,源码如下:
package org.springframework.stereotype; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
String value() default "";
}
(1)被@Component修饰,说明此注解用来标识一个bean。
(2)用来说明被修饰的类为控制器类。
三、@RequestMapping
1.属性详解
@RequestMapping注解用来修饰类或方法,源码如下:
package org.springframework.web.bind.annotation; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String[] value() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {};
}
(1) value
value属性用将请求URL(如"/hello") 映射到整个类或特定的处理方法上。
value属性是RequestMapping的默认属性,也就是说 @RequestMapping( value = "/hello") 可以写成 @RequestMapping("/hello") 。
(2)method
method 属性用来指定如下请求方式:GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
2.注解简化
@RequestMapping(value= "/hello",Method=RequestMethod.GET ) 可简写成 @GetMapping("/hello")
类似注解还有:
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping
四、参数Bean
Spring 会为如下类型的参数自动注入其bean对象。
见:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-arguments
五、方法参数
可用如下注解用来修饰请求参数。
1.@RequestParam
可通过RequestParam来接收请求参数
@Controller
@RequestMapping("/pets")
public class EditPetForm { // ... @GetMapping
public String setupForm(@RequestParam("petId") int petId, Model model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
} // ... }
2.@RequestHeader
2.@RequestHeader
通过此注解可以获取请求头相关信息。
例如,一个请求头为:
Host localhost:8080
Accept text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
则可以通过如下方式获取请求头信息
@GetMapping("/demo")
public void handle(
        @RequestHeader("Accept-Encoding") String encoding,
        @RequestHeader("Keep-Alive") long keepAlive) {
    //...
}
3.@CookieValue
获取cookie
@GetMapping("/demo")
public void handle(@CookieValue("JSESSIONID") String cookie) {
    //...
}
4.@PathVariable 
可通过@PathVariable 来接收路径变量
@Controller
@RequestMapping("/owners/{ownerId}")
public class OwnerController { @GetMapping("/pets/{petId}")
public Pet findPet(@PathVariable Long ownerId, @PathVariable Long petId) {
// ...
}
}
5.@ModelAttribute
标注在方法参数上的@ModelAttribute说明了该方法参数的值将由model中取得。如果model中找不到,那么该参数会先被实例化,然后被添加到model中。在model中存在以后,请求中所有名称匹配的参数都会填充到该参数中。
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
public String processSubmit(@ModelAttribute Pet pet) { }
以上面的代码为例,这个Pet类型的实例可能来自哪里呢?有几种可能:
- 它可能因为@SessionAttributes标注的使用已经存在于model中
- 它可能因为在同个控制器中使用了@ModelAttribute方法已经存在于model中——正如上一小节所叙述的
- 它可能是由URI模板变量和类型转换中取得的(下面会详细讲解)
- 它可能是调用了自身的默认构造器被实例化出来的
SpringMVC_总结_03_SpringMVC相关注解的更多相关文章
- Spring bean依赖注入、bean的装配及相关注解
		依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { priv ... 
- springBoot注解大全JPA注解springMVC相关注解全局异常处理
		https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ... 
- spring boot @ConditionalOnxxx相关注解总结
		Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ... 
- springmvc4 相关注解的详细讲解
		首先我是一个初学springmvc,抱着去加深印象的目的去整理相关springmvc4的相关注解,同时也希望给需要相关查阅的读者带来帮助. 1.@ControllerController控制器是通过服 ... 
- spring相关注解
		spring相关注解: 使用之前需要<context:annotation-config/>在配置文件中启用 @Required 应用于类属性的set方法,并且要求该属性必须在xml容器里 ... 
- JPA 入门程序及相关注解
		1. 概述 JPA(Java Persistence API):用于对象持久化的API; JPA本质上是一种ORM规范,不是ORM框架;提供了一些编程的API接口; Hibernate是实现; 1.1 ... 
- Spring注解:Enable相关注解
		@EnableXXX:可以用于取代xml配置中的一些配置,被该注解所标注的类,其中被@Bean标注的方法,一般就用于返回和EnableXXX的XXX相关的Bean,Bean中一般有XXX相关的注解 同 ... 
- Spring AOP 面向切面编程相关注解
		Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ... 
- Springboot 相关注解大全
		1.Spring注解 1.@Autowired 标注在方法,Spring容器创建当前对象,就会调用方法,完成赋值:方法使用的参数,自定义类型的值从ioc容器中获取自动装配; Spring利用依赖注入( ... 
随机推荐
- redis3.2.8安装过程
			1.安装依赖的包yum -y install jemalloc gcc2.解压redis的安装文件tar xf redis-3.2.8.tar.gz3.进入redis-3.2.8目录cd redis- ... 
- 剑指offer 面试49题
			面试49题: 题:丑数 题目:把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N ... 
- GCE 创建一个Linux VM
			sudo yum install wget 安装Java sudo wget --no-check-certificate --no-cookies --header "Cookie: or ... 
- Android开发问题:ActivityNotFoundException: Unable to find explicit activity class
			http://blog.csdn.net/debuglog/article/details/7236013 原因:AndroidManifest.xml未添加对应Activity配置. 解决办法:在A ... 
- Centos小脚本(sftp)
			sftp用户创建,改变属组,家目录 #!/bin/python import os,sys class sftp_user(object): def __init__(self,user,passwd ... 
- iOS 事件响应者链的学习(也有叫 UI连锁链)
			当发生事件响应的时候,必须知道由谁来响应事件.在iOS中,由响应链来对事件进行响应,所有的事件响应的类都是继承于UIResponder的子类,响应链是一个由不同对象组成的层次结构,其中每个对象将依次获 ... 
- Kafka Confluent
			今天我们要讲的大数据公司叫作Confluent,这个公司是前LinkedIn员工出来后联合创办的,而创业的基础是一款叫作Apache Kafka的开源软件. Confluen联合创始人Jun Rao即 ... 
- 【LeetCode】【定制版排序】Sort Colors
			之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ... 
- $GitHub边用边总结
			以下用法是在ubuntu系统下的用法,主要内容整理自'廖雪峰的官方网站'. #1.安装git$ sudo apt-get install git #2.声明git账号$ git config --gl ... 
- AngularJS 视图和路由
			在AngularJS之后引用angular-route 路由 ngRoute模块加载声明 AngularJS提供的when和otherwise两个方法来定义应用的路由 otherwise ... 
