01-3、RequestMapping注解

RequestMapping注解

  • 作用:用于建立请求URL处理请求方法之间的对应关系。
  • 出现位置:在类、接口、方法上
出现位置说明举例
类上

请求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 {};
}

 

 说明:

  • java中元注解有四个: @Retention @Target @Documented @Inherited;

元注解

用途用法
@Target注解的作用目标

@Target(ElementType.TYPE)   //接口、类、枚举
@Target(ElementType.FIELD)   //字段、枚举的常量
@Target(ElementType.METHOD)   //方法
@Target(ElementType.PARAMETER)   //方法参数
@Target(ElementType.CONSTRUCTOR)   //构造函数
@Target(ElementType.LOCAL_VARIABLE)   //局部变量
@Target(ElementType.ANNOTATION_TYPE)   //注解
@Target(ElementType.PACKAGE)    //包    

@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" %>
<html>
<head>
    <title>入门程序</title>
</head>
<body>
    <h3>SpringMVC入门</h3>
    <a href="/hello">入门程序</a><br>
    <a href="/test/testRequestMapping">testRequestMapping</a>
</body>
</html>

RequestMapping注解的属性:

  • value:用于指定请求的URL,和path属性的作用一样;属性只有一个value时,可以省略value=不写;
  • method:用于指定请求的方法;method属性是RequestMethod类型的数组,可设置多个值,如GET,POST,PUT,DELETE等;
  • params:用于指定限制请求参数的条件,支持简单的表达式,要求请求参数的key和value必须和配置的一模一样;
  • 如:params={"accountName"},表示请求参数必须有accountName;
  • 如:params={"money!100"},表示请求参数中money不能是100;
  • headers:用于指定限制请求消息头的条件;
  • 注意:以上四个属性只要出现2个或以上时,他们的关系是与的关系
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

  • params = {"username"},请求参数中要包含username
  • params = {"username=heihei"},请求参数中要包含username=heihei
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" %>
<html>
<head>
    <title>入门程序</title>
</head>
<body>
    <h3>SpringMVC入门</h3>
    <a href="/hello">入门程序</a><br>
    <a href="/test/testRequestMapping">testRequestMapping</a><br>
    <a href="/test/testRequestMappingParam?username=heihei">testRequestMappingParam</a><br>
</body>
</html>

如果index.jsp中,超链接请求没有带上请求参数username,那么点击testRequestMappingParam超链接会报错;

<a href="/test/testRequestMappingParam">testRequestMappingParam</a><br>

如果index.jsp中,超链接请求username取值与Controller限制的不一样,那么点击testRequestMappingParam超链接会报错;

<a href="/test/testRequestMappingParam?username=hehe">testRequestMappingParam</a><br>

限制请求头:请求头中要包含Accept字符串

@RequestMapping(path = "/testRequestMappingParam",headers = "Accept",params = {"username=heihei"},method = {RequestMethod.GET})

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值