RequestMapping注解
出现位置 | 说明 | 举例 |
类上 | 请求URL的第一级访问目录; 不写,相当于应用的根目录; 写,要以/开头; 目的是为了使URL可以按照模块化管理。 |
账户模块: /account/add /account/update /account/delete 订单模块: /order/add /order/update /order/delete |
方法上 | 请求URL的第二级访问目录; |
源码:
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;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
说明:
元注解 |
用途 | 用法 |
---|---|---|
@Target | 注解的作用目标 | @Target(ElementType.TYPE) //接口、类、枚举 |
@Retention | 注解的保留位置 | @Retention(RetentionPolicy.RUNTIME) ,注解会在class字节码文件中存在,在运行时可以通过反射获取到; @Retention(RetentionPolicy.SOURCE),注解仅存在于源码中,在class字节码文件中不包含; @Retention(RetentionPolicy.CLASS),默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得; |
@Documented | 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。 | |
@Inherited | 说明子类可以继承父类中的该注解 |
代码:springmvc_day01_01_start
cn.itcast.controller.RequestMappingController
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping(path = "/testRequestMapping")
public String testRequestMapping(){
System.out.println("testRequestMapping执行了...");
return "success";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
入门程序
SpringMVC入门
入门程序
testRequestMapping
RequestMapping注解的属性:
RequestMethod[] method() default {};
package org.springframework.web.bind.annotation;
public enum RequestMethod {
GET,
HEAD,
POST,
PUT,
PATCH,
DELETE,
OPTIONS,
TRACE;
private RequestMethod() {
}
}
限制请求参数的条件:cn.itcast.controller.RequestMappingController
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping(path = "/testRequestMapping",method = {RequestMethod.POST,RequestMethod.GET})
public String testRequestMapping(){
System.out.println("testRequestMapping执行了...");
return "success";
}
@RequestMapping(path = "/testRequestMappingParam",params = {"username=heihei"},method = {RequestMethod.GET})
public String testRequestMappingParam(){
System.out.println("测试testRequestMapping注解的属性,包含参数...");
return "success";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
入门程序
SpringMVC入门
入门程序
testRequestMapping
testRequestMappingParam
如果index.jsp中,超链接请求没有带上请求参数username,那么点击testRequestMappingParam超链接会报错;
testRequestMappingParam
如果index.jsp中,超链接请求username取值与Controller限制的不一样,那么点击testRequestMappingParam超链接会报错;
testRequestMappingParam
限制请求头:请求头中要包含Accept字符串
@RequestMapping(path = "/testRequestMappingParam",headers = "Accept",params = {"username=heihei"},method = {RequestMethod.GET})