当前位置:网站首页>@requestmapping注解的作用及用法
@requestmapping注解的作用及用法
2022-07-30 23:33:00 【是庸医啊】
目录
@requestmapping注解的作用及用法
注解 @RequestMapping 可以用在类定义处和方法定义处,就是来映射服务器访问的路径。
类定义处:规定初步的请求映射,相对于web应用的根目录;
方法定义处:进一步细分请求映射,相对于类定义处的URL。如果类定义处没有使用该注解,则方法标记的URL相对于 根目录而言;
user服务器下的访问
zar服务器的访问
1)此注解可加在方法上,是为此方法注册一个可以访问的名称(路径)
@RequestMapping (" /demo ")
public String demo () {
system.out.println("服务器被访问到了");
return "main";/可以直接跳到/admin/main.jsp页面上
}
<a href="$ (pagecontext. request.contextPath}/demo .action">访问服务器</ a>
2)此注解可以加在类上,相当于是包名(虚拟路径),区分不同类中相同的action的名称
@RequestMapping ("/user")
public class DemoActionl {..}
<a href="${pagecontext. request . contextPathi/user/demo.action">访问服务器</ a>
搭建Spring MVC项目
创建对象,然后写请求
@Controller
public class RegAction{
@RequestMapping(value="/req",method = RequestMethod.GET)
public String req() {
system.out.println(“我是处理get请求的);
return"main";
@RequestMapping (value = "/reg", method= RequestMethod. POST)
public String reql(){
system.out.println("我是处理post请求的... ") ;
return"main";
}
}
提交get请求
换一下,提交get请求
@RequestMapping 域简介
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
RequestMapping注解有六个属性,下面我们把她分成三类进行说明。
1、 value, method;
value: 指定请求的实际地址,指定的地址可以是URI Template 模式;
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3、 params,headers;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
@RequestMapping 域用法
value的url值为以下三类:
A) 可以指定为普通的具体值;
B) 可以指定为含有某变量的一类值(URI Template Patterns with Path Variables):/owners/{ownerId}
C) 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions):/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}
method:
Http规范定义了多种请求资源的方式,最基本的有四种,分别为:GET(查)、POST(增)、PUT(改)、DELETE(删),而URL则用于定位网络上的资源相当于地址的作用,配合四种请求方式,可以实现对URL对应的资源的增删改查操作。
consumes:
consumes="application/json"方法仅处理request Content-Type为“application/json”类型的请求。
produces:
produces="application/json"方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;
params:
params="myParam=myValue"仅处理请求中包含了名为“myParam”,值为“myValue”的请求;
headers:
headers="Referer=http://www.ifeng.com/" 仅处理request的header中包含了指定“Refer”请求头和对应值为“http://www.ifeng.com/”的请求;
四、@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型)
A、处理requet uri 部分(这里指uri template中variable,不含queryString部分)的注解: @PathVariable;
B、处理request header部分的注解: @RequestHeader, @CookieValue;
C、处理request body部分的注解:@RequestParam, @RequestBody;
D、处理attribute类型是注解: @SessionAttributes, @ModelAttribute;
边栏推荐
- "Wei cup" school more than 2022 cattle summer camp 4 Nancy (polocy) pelosi article variance law of Arts
- 反转链表-头插反转法
- 游戏商城表建立
- [动态规划] 0-1背包问题和完全背包问题
- 2021GDCPC Guangdong University Student Programming Competition H.History
- PS Basic Learning (1)
- "Code execution cannot continue because MSVCP140.dll was not found, reinstalling the program may resolve the problem, etc." Solutions
- 2D转换模块&&媒体查询
- "NIO Cup" 2022 Nioke Summer Multi-School Training Camp 2 H.Take the Elevator
- pytorch的安装注意事项
猜你喜欢
随机推荐
flex布局父项常见属性flex-wrap
机器学习1一回归模型(二)
通过对抗性知识蒸馏压缩深度图神经网络
【VisDrone数据集】YOLOV3训练VisDrone数据集步骤与结果
微软商店出现【0x800706D9】解决方法
测试人面试 常被问到的计算机网络题,高薪回答模板来了
Day016 类和对象
reindex win10
Shell编程条件语句 test命令 整数值,字符串比较 逻辑测试 文件测试
Computer shortcut icon whitening solution
uniapp folding box secondary loop
JS中? ?和??=和?.和 ||的区别
Manually set transaction commit in mysql
Achievements of Science and Technology (31)
PyTorch model export to ONNX file example (LeNet-5)
uniapp develops WeChat applet - soft exam brushing applet
2022 Nioke Summer Multi-School Training Camp 1 J Serval and Essay
Lambda表达式
uni-ui安装
45.【list链表的应用】