从springmvc入门(一)的hello wrold对springmvc有了大致的了解,@Controller与@RequestMapping这两个注解对于web开发至关重要。
@Controller注解:定义一个类作为控制类,看一下这个该注解的源码,详细的了解下,首先,Controller注解标签中还有4个标签
@Target 表示注解的作用对象是什么,ElementType.TYPE代表这注解只能作用于类上
@Retention 表示注解的作用范围,RetentionPolicy.RUNTIME表示运行时使用
@Documented 表示Documented注释的作用及其javadoc文档生成工具的使用
@Component 表示把这个注解注入到spring容器中
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
//控制类的名称
String value() default "";
}
@RequestMapping注解:映射请求的处理器
以@Controller一样,RequestMapping也有@Target标签,但是它比controller多了一个ElementType.METHOD,表示它能作用到方法上
@Mapping表示注解是一个映射的注解
注解里面的属性,即为使用标签时可以用的,在学习一的案例中我们用到了@RequestMapping(value = "/test", method = RequestMethod.GET)
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
//映射的名称
String name() default "";
//请求的路径,可以是多个
@AliasFor("path")
String[] value() default {};
//别名为value,与value一样
@AliasFor("value")
String[] path() default {};
//请求对于的方法,有get,post,delete,put
RequestMethod[] method() default {};
//表示请求必须有该参数,才能让该方法处理请求
String[] params() default {};
//表示请求必须有该header,才能让该方法处理请求
String[] headers() default {};
//处理请求提交内容类型
//jsp页面开头都会有<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
//contentType="text/html";就是响应头
String[] consumes() default {};
//指定返回的内容类型
String[] produces() default {};
}