SpringMVC参数传递
基本数据类型参数



- 如果请求参数名和方法参数名不对应使用@RequestParam注解中的value值进行匹配赋值(都适用)



- 如果方法参数是基本数据类型(不是封装类)可以通过@RequestParam中的defaultValue值设置参数默认值,可以防止没有参数时 500错误



- 通过@RequestParam注解中的
required
的强制要求必须某个参数值不能为空



对象数据类型
- 请求参数为对象时,前台传递的参数名与对象的属性名一致,且生成get与set方法








集合对象类型参数




多个同名参数
复选框传递的参数就是多个同名参数



restful 传值方式



跳转方式
SpringMVC中默认的跳转方式是请求转发
设置返回值字符串内容
- 重定向:redirect:资源路径
- 请求转发:forward:资源路径(省略不写时默认)
SpringMVC中重定向的路径问题
- 当使用不以/开头的资源路径时,依旧是在相对当前路径进行跳转
- 使用/开头的资源路径时,SringMVC会默认在前加上contextPath即加上当前项目名
@ResponseBody
作用:
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
注意事项:
@ResponseBody这个注解通常使用在控制层(controller)的方法上,其作用是将方法的返回值以特定的格式写入到response的body区域,进而将数据返回给客户端。当方法上面没有写ResponseBody,底层会将方法的返回值封装为ModelAndView对象。也就是说当不写@ResponseBody注解时,单独使用@ResquestMapping都会讲方法的返回值看成是一个视图对象从而进行返回。






1 2 3 4 5 6 7 8 9
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> main.jsp </body> </html>
|
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="demo1" method="get"> <input type="text" name="name" placeholder="demo1"> <input type="text" name="age"> <input type="submit" value="提交"> </form>
<form action="demo2" method="post"> <input type="text" name="name" placeholder="demo2"> <input type="text" name="age"> <input type="submit" value="提交"> </form>
<form action="demo3" method="post"> <input type="text" name="name1" placeholder="demo3"> <input type="text" name="age1"> <input type="submit" value="提交"> </form>
<form action="demo4" method="post"> <input type="text" name="name" placeholder="demo4"> <input type="text" name="age"> <input type="submit" value="提交"> </form>
<form action="demo5" method="post"> <input type="text" name="name" placeholder="demo5"> <input type="text" name="age"> <input type="checkbox" name="hover" value="学习"> <input type="checkbox" name="hover" value="写代码"> <input type="checkbox" name="hover" value="看视频"> <input type="checkbox" name="hover" value="看笔记"> <input type="submit" value="提交"> </form>
<form action="demo6" method="post"> <input type="text" name="peo.name" placeholder="demo6"> <input type="text" name="peo.age" > <input type="submit" value="提交"> </form>
<form action="demo7" method="post"> <input type="text" name="peo[0].name" placeholder="demo7"> <input type="text" name="peo[0].age" > <input type="text" name="peo[1].name" placeholder="demo7"> <input type="text" name="peo[1].age" > <input type="submit" value="提交"> </form>
<form action="page" method="post"> <input type="text" name="pageSize" placeholder="page"> <input type="text" name="pageNumber"> <input type="submit" value="提交"> </form>
<a href="demo8/zs/121">跳转</a>
</body> </html>
|
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| package com.kylin.controller;
import com.kylin.pojo.Demo; import com.kylin.pojo.Demo1; import com.kylin.pojo.People; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller public class DemoController {
@RequestMapping("/demo1") public String demo1(String name, int age) { System.out.println("执行demo1" +" "+ name + " " + age); return "main.jsp"; }
@RequestMapping("/demo2") public String demo2(People people, String name, String age) { System.out.println("执行demo2" + people + " " + name + " " + age); return "main.jsp"; }
@RequestMapping("/demo3") public String demo3(@RequestParam("name1") String name, @RequestParam("age1") String age) { System.out.println(name + " " + age); return "main.jsp"; }
@RequestMapping("/demo4") public String demo4(@RequestParam(required = true) String name,int age) { System.out.println("name是SQL的查询条件,必须要传递name参数" + name); return "main.jsp"; }
@RequestMapping("/demo5") public String demo5(String name, int age, @RequestParam("hover") List<String> hover) { System.out.println(name + " " + age + " " + hover); return "main.jsp"; }
@RequestMapping("/demo6") public String demo6(Demo demo) { System.out.println(demo); return "main.jsp"; }
@RequestMapping("/demo7") public String demo7(Demo1 demo1) { System.out.println(demo1);
return "main.jsp"; }
@RequestMapping("/demo8/{name}/{age}") public String demo8(@PathVariable String name, @PathVariable("age") String age1) { System.out.println(name + " " + age1); return "/main.jsp"; }
@RequestMapping("/demo9") public String demo9() { System.out.println("重定向"); return "redirect:/main"; }
@RequestMapping("/page") public String page(@RequestParam(defaultValue = "2") int pageSize, @RequestParam(defaultValue = "1") int pageNumber) { System.out.println(pageSize + " " + pageNumber); return "main.jsp"; } @RequestMapping("/demo10") @ResponseBody public People demo10() { People people = new People(); people.setName("张三"); people.setAge(18); return people; }
@RequestMapping(value = "/demo11", produces = "text/html;charset=utf-8") @ResponseBody public String demo11() { return "中文"; } }
|