@RequestParam注解:
1 2 3 4 5 6 7 8 9
| @Controller @RequestMapping("/vote") public class VoteHandler { @RequestMapping(value = "/test01") public String test01(@RequestParam(value = "name", required = false) String username){ System.out.println("username= " + username); return "success"; } }
|
@RequestParam(value = “name”, required = false)注解说明:
1.获取到超链接传递的数据,请求http://localhost:8080/springmvc/vote/test01?name=xx
2.@RequestParam 表示会接收提交的参数
3.value = “name” 表示提交的参数名为name
4.required = false 表示该参数可以没有,默认是ture,表示必须要传这个参数
5.当我使用@RequestParam(value = “name”, required = false)后就可以让请求的的参数名和方法的形参名可以不一致
@ModelAttribute注解:
1 2 3 4 5 6 7 8
| @Controller @RequestMapping("/vote") public class VoteHandler { @ModelAttribute public void prepareModel(){ System.out.println("——————prepareModel()完成准备工作——————"); } }
|
1.当VoteHandler的方法被标识@ModelAttribute就视为一个前置方法
2.当调用该VoteHandler类的其他的方法时,都会先执行该前置方法
3.类似AOP的前置通知
4.prepareModel前置方法,会切入到其他方法前执行
5.该用法一般用在用户管理
@InitBinder注解:
1 2 3 4
| @InitBinder public void initBinder(WebDataBinder webDataBinder){ webDataBinder.setDisallowedFields("birthday"); }
|
1.方法上需要标注@InitBinder springmvc底层会初始化WebDataBinder
2.调用webDataBinder.setDisallowedFields(“birthday”);表示取消指定属性的绑定即:当表单提交字段为birthday时,
就不会把接收到的birthday值填充到model数据good的birthday属性中
3.机制:springmvc在底层通过反射调用目标方法时,接收到http请求的参数和值,使用反射+注解技术,取消对指定属性的填充
4.setDisallowedFields支持可变参数,可以填写多个字段
5.如果取消某个属性绑定,验证就没意义了,应当把验证的@NotEmpty注解去掉,birthday属性会使用默认值null
参数封装:
1.
1 2 3 4 5
| @PostMapping("/test03") public String test03(Master master){ System.out.println("master=" + master); return "success"; }
|
获取提交的数据 -> 封装成java对象
1.方法的形参用对应的类型来指定即可,SpringMVC会自动的进行封装
2.如果自动的完成封装,要求提交的数据,参数名和对象的字段名保持一致
3.如果属性是对象,这里仍然是通过 字段名.字段名
4.如果提交的数据的参数名和对象的字段名不匹配,则对象的属性值就是null
5.该封装底层是反射+注解..来实现
2.
1 2 3 4 5 6
| @RequestMapping("/test06") public String test06(Master master, Map<String, Object> map){ map.put("address", "beijing"); map.put("master", null); return "success"; }
|
1.通过Map<String, Object>设置数据到request域
2.通过map对象,添加属性到request中
3.原理分析:springmvc会遍历map,然后将map的k-v,存放到request域中
文件上传和下载:
1.文件上传:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| @RequestMapping(value = "/upload") @Controller public class UploadFileHandler { @RequestMapping("/fileUpload") public String fileUpload(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request, String introduceName) throws IOException {
String originalFilename = file.getOriginalFilename(); System.out.println("上传得文件名:" + originalFilename); System.out.println("上传得文件介绍:" + introduceName);
String fileFullPath = request.getServletContext().getRealPath("/img/" + originalFilename);
File saveToFile = new File(fileFullPath);
file.transferTo(saveToFile);
return "success"; } }
|
文件下载:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| @RequestMapping("/down") @Controller public class DownFileHandler {
@RequestMapping("/downFile") public ResponseEntity<byte[]> downFile(HttpSession session) throws IOException {
InputStream resourceAsStream = session.getServletContext().getResourceAsStream("/img/copy.jpg");
byte[] bytes = new byte[resourceAsStream.available()];
resourceAsStream.read(bytes);
HttpStatus httpStatus = HttpStatus.OK;
HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Content-Disposition", "attachment;filename=copy.jpg");
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bytes, httpHeaders, httpStatus); return responseEntity; } }
|