赞
踩
参数名称前端和后端一一对应
- // 1:直接使用方法的参数逐个接收 要求:参数的名称必须与请求中携带的参数名称一致
- // 优势:不需要类型转换,前后端类型直接对应
- @RequestMapping("test01")
- public ModelAndView test01(Integer teamId, String teamName, String teamLocation) {
- System.out.println("teamId: " + teamId + " teamName: " + teamName + " teamLocation: " + teamLocation);
- return new ModelAndView("OK");
- }
- <h3>1、直接使用方法的参数逐个接收</h3>
- <form action="/param/test01" method="post">
- 球队ID:<input name="teamId"/><br>
- 球队name:<input name="teamName"/><br>
- 球队location:<input name="teamLocation"/><br>
- <button type="submit">提交</button>
- </form>
要求:请求中携带的参数必须域对象属性名一致
- <h3>2、使用对象接收多个参数</h3>
- <form action="/param/test02" method="post">
- 球队ID:<input name="teamId"/><br>
- 球队name:<input name="teamName"/><br>
- 球队location:<input name="location"/><br>
- <button type="submit">提交</button>
- </form>
- public class Team {
- private Integer teamId;
- private String teamName;
- private String location;
- }
-
- @RequestMapping("test02")
- public ModelAndView test02(Team team) {
- System.out.println(team);
- return new ModelAndView("OK");
- }
请求参数和方法名称的参数不一致 使用注解@RequestParam(value"请求中的名称")
required: 表示参数是否是必须的,true:没有则报错 false;没有该参数则报错
defaultValue: 表示参数为空时的默认值
- <h3>3、请求参数和方法名称的参数不一致</h3>
- <form action="/param/test03" method="post">
- 球队ID:<input name="teamId"/><br>
- 球队name:<input name="teamName"/><br>
- 球队location:<input name="location"/><br>
- <button type="submit">提交</button>
- </form>
- @RequestMapping("test03")
- public ModelAndView test03(@RequestParam(value = "teamId", required = false, defaultValue = "0") Integer id,
- @RequestParam(value = "teamName") String name,
- @RequestParam(value = "location") String location) {
- System.out.println("teamId: " + id + " teamName: " + name + " location: " + location);
- return new ModelAndView("OK");
- }
跟javaweb中是一样的,因为Springmvc框架的底层是由servlet实现的
- <h3>4、使用HttpServletRequest获取参数</h3>
- <form action="/param/test04" method="post">
- 球队ID:<input name="teamId"/><br>
- 球队name:<input name="teamName"/><br>
- 球队location:<input name="location"/><br>
- <button type="submit">提交</button>
- </form>
- @RequestMapping("test04")
- public ModelAndView test04(HttpServletRequest req) {
- String teamName = req.getParameter("teamName");
- String id = req.getParameter("teamId");
- String location = req.getParameter("location");
- System.out.println("teamName: " + teamName + " id: " + id + " location: " + location);
- return new ModelAndView("OK");
- }
- @RequestMapping("test05/{id}/{name}/{loc}")
- public ModelAndView test05(@PathVariable("id") Integer id, @PathVariable("name") String name, @PathVariable("loc") String loc) {
- System.out.println("teamId: " + id + " teamName: " + name + " location: " + loc);
- return new ModelAndView("OK");
- }
对象类型中的日期属性上需要添加注解@DateFormat(pattern = "yyyy-MM-dd"),通过pattern参数来修改日期的格式
- <h3>6、获取日期类型的参数</h3>
- <form action="/param/test06" method="post">
- 球队ID:<input name="teamId"/><br>
- 球队name:<input name="teamName"/><br>
- 球队location:<input name="location"/><br>
- 球队date:<input name="date"/><br>
- <button type="submit">提交</button>
- </form>
- public class Team {
- private Integer teamId;
- private String teamName;
- private String location;
- @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
- private Date date;
- }
- @RequestMapping("test06")
- public ModelAndView test06(Team team) {
- System.out.println(team);
- return new ModelAndView("OK");
- }
- <h3>7、获取数组类型的参数</h3>
- <form action="/param/test07" method="post">
- 球队name1:<input name="teamName"/><br>
- 球队name2:<input name="teamName"/><br>
- 球队name3:<input name="teamName"/><br>
- 球队name4:<input name="teamName"/><br>
- <button type="submit">提交</button>
- </form>
- @RequestMapping("test07")
- public ModelAndView test07(String[] teamName, HttpServletRequest req) {
- // 方式1
- for(String i : teamName) {
- System.out.println(i);
- }
- System.out.println("-----");
- // 方式2
- String[] teamNames = req.getParameterValues("teamName");
- for(String i : teamNames) {
- System.out.println(i);
- }
- return new ModelAndView("OK");
- }
简单类型的可以通过@RequestParam注解实现;对象集合不支持直接获取,必须封装在类中,作为一个属性操作
- <form action="/param/test09" method="post">
- 球队id1:<input type="text" name="teamList[0].teamId"/><br/>
- 球队id2:<input type="text" name="teamList[1].teamId"/><br/>
- 球队id3:<input type="text" name="teamList[2].teamId"/><br/>
- 球队名称1:<input type="text" name="teamList[0].teamName"/><br/>
- 球队名称2:<input type="text" name="teamList[1].teamName"/><br/>
- 球队名称3:<input type="text" name="teamList[2].teamName"/><br/>
- <button type="submit">提交</button>
- </form>
- @RequestMapping("test08")
- public ModelAndView test08(@RequestParam("teamName") List<String> nameList){
- System.out.println("test08-----------------");
- for (String s : nameList) {
- System.out.println(s);
- }
- return new ModelAndView("ok");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。