综合百科

Spring MVC的常用注解有哪些及怎么用

Spring Boot 默认集成了Spring MVC,下面为Spring MVC一些常用注解。

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

一、Controller注解

Controller注解用于修饰Java类,被修饰的类充当MVC中的控制器角色。
Controller注解使用了@Component修饰,使用Controller注解修饰的类,会被@ComponentScan检测,并且会作为Spring的bean被放到容器
中。

packagecom.example.demo;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;@ControllerpublicclassDemoController{@RequestMapping("/index")@ResponseBodypublicStringindex(){return"index";}}

运行项目后,浏览器访问:http://localhost:8080/index,页面显示:
index

二、RestController注解

RestController注解是为了更方便使用@Controller和@ResponseBody。
@ResponseBody修饰控制器方法,方法的返回值将会被写到HTTP的响应体中,所返回的内容不放到模型中,也不会被解释为视图的名称。
下面例子等同于上面例子。

packagecom.example.demo;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassDemoController{@RequestMapping("/index")publicStringindex(){return"index";}}

三、RequestMapping注解

RequestMapping注解可修饰类或方法,主要用于映射请求与处理方法。
当用于修饰类并设置了URL时,表示为各个请求设置了URL前缀。
RequestMapping注解主要有以下属性:
(1)path与value:用于配置映射的url;
(2)method:映射的HTTP方法,如GET、POST、PUT、DELETE;
也可以使用默认配置了@RequestMapping的method属性的几个注解:
@GetMapping等同于RequestMapping(method="RequestMethod.GET")
@PostMapping、@PutMapping、@DeleteMapping类似。
(3)params:为映射的请求配置参数标识;
(4)consumes:配置请求的数据类型,如XML或JSON等;
(5)produces:配置响应的数据类型,如“application/json”返回json数据;

packagecom.example.demo;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/oa")publicclassDemoController{@RequestMapping(value="/index1")publicStringindex1(){return"index1";}@RequestMapping(value="/index2",method=RequestMethod.GET)publicStringindex2(){return"index2";}@GetMapping(value="/index3")publicStringindex3(){return"index3";}}

浏览器分别访问:
http://localhost:8080/oa/index1
http://localhost:8080/oa/index2
http://localhost:8080/oa/index3
页面分别显示:
index1
index2
index3

四、PathVariable注解

PathVariable注解主要用于修饰方法参数,表示该方法参数是请求URL的变量。

packagecom.example.demo;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassDemoController{@GetMapping("/index1/{name}")publicStringindex1(@PathVariableStringname){return"index1:"+name;}//可以为@PathVariable配置属性值,显式绑定方法参数与URL变量的值@GetMapping("/index2/{name}")publicStringindex2(@PathVariable("name")Stringlc){return"index2:"+lc;}}

浏览器访问http://localhost:8080/index1/a
页面显示:
a
访问http://localhost:8080/index1/b
页面显示:
b

五、RequestParam注解

RequestParam注解用于获取请求体中的请求参数,如表单提交后获取页面控件name值。

packagecom.example.demo;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importjava.util.Map;@RestControllerpublicclassDemoController{@PostMapping("/index1")publicStringindex1(@RequestParamStringuserName){returnuserName;}//map存放所有请求参数@PostMapping("/index2")publicStringindex2(@RequestParamMap<String,String>map){Stringage=map.get("age");Stringsex=map.get("sex");returnage+","+sex;}}

随便在电脑中如桌面新建一个html文件:

<html><body><formmethod="post"action="http://localhost:8080/index1"><inputtype="text"name="userName"value="abc"/><inputtype="submit"value="提交1"/></form><formmethod="post"action="http://localhost:8080/index2"><inputtype="text"name="age"value="22"/><inputtype="password"name="sex"value="male"/><inputtype="submit"value="提交2"/></form></body></html>

浏览器打开后,如果点击“提交1”按钮后,页面跳到http://localhost:8080/index1,显示abc。
如果点击“提交2”按钮后,页面跳到http://localhost:8080/index2,显示22,male。

六、文件上传

使用RequestParam注解可以实现文件上传。

packagecom.example.demo;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.multipart.MultipartFile;importjava.io.File;importjava.io.IOException;@RestControllerpublicclassDemoController{@PostMapping("/upload")publicStringupload(@RequestParam("file")MultipartFilefile)throwsIOException{StringfileName=file.getOriginalFilename();StringfilePath="D:/";Filedest=newFile(filePath+fileName);file.transferTo(dest);return"上传成功";}}

随便新建一个html文件

<html><body><formmethod="post"action="http://localhost:8080/upload"enctype="multipart/form-data"><inputtype="file"name="file"/><inputtype="submit"value="提交"/></form></body></html>

浏览器打开后,选择一个文件,点击提交后,文件保存到了D盘。

关于“Spring MVC的常用注解有哪些及怎么用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Spring MVC的常用注解有哪些及怎么用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

上一篇:budgie
下一篇:Twitter是什么