RequestMapping注解有什么作用

发布时间:2022-01-07 19:46:46 作者:iii
来源:亿速云 阅读:387

这篇“RequestMapping注解有什么作用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“RequestMapping注解有什么作用”文章吧。

@RequestMapping注解的作用

@RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一。这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上。并且一个处理请求地址映射的注解,可用在类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

以下为@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;
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 {};
}

方法上:

请求 URL 的第二级访问目录。

属性:

例如:

注意:

以上四个属性只要出现 2 个或以上时,他们的关系是与的关系。

作用:

用于建立请求URL和处理请求方法之间的对应关系。

出现位置:

请求 URL 的第一级访问目录。此处不写的话,就相当于应用的根目录。写的话需要以/开头。 它出现的目的是为了使我们的 URL 可以按照模块化管理:

例如:

账户模块:

订单模块:

案例

普通案例: 将@RequestMapping注解分别注释在类和方法上,所以在前端写链接的时候要写完全的路径(类上标签的路径+方法标签上的路劲)

控制器的代码块:

/**
* RequestMapping 注解出现的位置
* @author 
* @Company http://www.ithiema.com
* @Version 1.0
*/
@Controller("accountController")
@RequestMapping("/account")
public class AccountController {
	@RequestMapping("/findAccount")
	public String findAccount() {
		System.out.println("查询了账户。。。。");
		return "success";
	}
}

JSP中的代码块

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>requestmapping 的使用</title>
</head>
<body>
	<!-- 第一种访问方式 -->
	<a href="${pageContext.request.contextPath}/account/findAccount" rel="external nofollow" >
		查询账户
	</a>


	<!-- 第二种访问方式 -->
	<a href="account/findAccount" rel="external nofollow" >查询账户</a>
</body>
</html>

案例: 用于指定请求的方式,如果要求是get请求,那么在前端写请求方式时的method=get,反之如果要求是post请求,前端的,medthod=post

控制器代码块:

/**
* 保存账户
* @return
*/
@RequestMapping(value="/saveAccount",method=RequestMethod.POST)
public String saveAccount() {
	System.out.println("保存了账户");
	return "success";
}

jsp代码块

<a href="account/saveAccount" rel="external nofollow" >保存账户,get 请求</a>
<form action="account/saveAccount" method="post">
<input type="submit" value="保存账户,post 请求">
</form>

注意: 当使用 get 请求时,提示错误信息是 405,信息是方法不支持 get 方式请求

RequestMapping注解有什么作用

params 属性的示例: 用于指定限制请求参数的条件。它支持简单的表达式。要求请求参数的 key 和 value 必须和 配置的一模一样。 写了这个属性之后就一定要在前端发送请求的时候,前端就要有这个属性,否则就无法访问。控制器代码块:

/**
* 删除账户
* @return
*/
@RequestMapping(value="/removeAccount",params= {"accountName","money>100"})
public String removeAccount() {
	System.out.println("删除了账户");
	return "success";
}

jsp代码块:

<a href="account/removeAccount?accountName=aaa&money>100" rel="external nofollow" >删除账户,金额 100</a>
<a href="account/removeAccount?accountName=aaa&money>150" rel="external nofollow" >删除账户,金额 150</a>

注意:

当我们点击第一个超链接时,可以访问成功。

当我们点击第二个超链接时,无法访问。

如下图:

RequestMapping注解有什么作用

支持REST风格的参数: 代表性状态传输 (REST) 是一种用于分布式超媒体系统(如万维网)的体系结构样式。 (以下测试代码与@PathVariable联用)

控制器代码块:

    @RequestMapping(value = "/testRequestMapping/{Id}")
    public String testRequestMapping(@PathVariable String Id){
        System.out.println("testRequestMapping执行了"+Id);
        return "success";
    }

jsp前端代码块

<a href="account/testRequestMapping/21" rel="external nofollow" >@RequestMapping测试</a>

@RequestMapping注解的六个属性详解

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

RequestMapping注解有六个属性

下面分成三类进行说明

valuemethod

consumesproduces

paramsheaders

以上就是关于“RequestMapping注解有什么作用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. RequestMapping中produces属性作用
  2. 二、Spring MVC的RequestMapping注解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

requestmapping

上一篇:MYSQL插入数据时检查字段值是否重复的方法是什么

下一篇:Linux调度器BFS有哪些作用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》