03_处理器方法的参数

zcq

Team.java

  1. package com.kkb.pojo;
  2. import com.fasterxml.jackson.annotation.JsonFormat;
  3. import org.springframework.format.annotation.DateTimeFormat;
  4. import java.util.Date;
  5. /**
  6. * ClassName: Team
  7. *
  8. * @author wanglina
  9. * @version 1.0
  10. */
  11. public class Team {
  12. private Integer teamId;
  13. private String teamName;
  14. private String location;
  15. @DateTimeFormat(pattern = "yyyy-MM-dd")
  16. @JsonFormat(pattern = "yyyy-MM-dd")
  17. private Date createTime;
  18. @Override
  19. public String toString() {
  20. return "Team{" +
  21. "teamId=" + teamId +
  22. ", teamName='" + teamName + '\'' +
  23. ", location='" + location + '\'' +
  24. ", createTime=" + createTime +
  25. '}';
  26. }
  27. public Date getCreateTime() {
  28. return createTime;
  29. }
  30. public void setCreateTime(Date createTime) {
  31. this.createTime = createTime;
  32. }
  33. public Integer getTeamId() {
  34. return teamId;
  35. }
  36. public void setTeamId(Integer teamId) {
  37. this.teamId = teamId;
  38. }
  39. public String getTeamName() {
  40. return teamName;
  41. }
  42. public void setTeamName(String teamName) {
  43. this.teamName = teamName;
  44. }
  45. public String getLocation() {
  46. return location;
  47. }
  48. public void setLocation(String location) {
  49. this.location = location;
  50. }
  51. }

QueryVO.java

  1. package com.kkb.vo;
  2. import com.kkb.pojo.Team;
  3. import java.util.List;
  4. /**
  5. * ClassName: QueryVO
  6. *
  7. * @author wanglina
  8. * @version 1.0
  9. */
  10. public class QueryVO {
  11. private List<Team> teamList;
  12. public List<Team> getTeamList() {
  13. return teamList;
  14. }
  15. public void setTeamList(List<Team> teamList) {
  16. this.teamList = teamList;
  17. }
  18. }

ParamController.java

  1. package com.kkb.controller;
  2. import com.kkb.pojo.Team;
  3. import com.kkb.vo.QueryVO;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import javax.servlet.http.HttpServletRequest;
  10. import java.util.List;
  11. /**
  12. * ClassName: ParamController
  13. *
  14. * @author wanglina
  15. * @version 1.0
  16. */
  17. @Controller
  18. @RequestMapping("param")
  19. public class ParamController {
  20. //8、获取集合类型的参数: 简单类型的可以通过@RequestParam注解实现;对象集合不支持直接获取,必须封装在类中,作为一个属性操作
  21. @RequestMapping("test08")
  22. public ModelAndView test08(@RequestParam("teamName") List<String> nameList){
  23. System.out.println("test08-----------------");
  24. for (String s : nameList) {
  25. System.out.println(s);
  26. }
  27. return new ModelAndView("ok");
  28. }
  29. @RequestMapping("test09")
  30. public ModelAndView test09(QueryVO vo){
  31. System.out.println("test09-----------------");
  32. for (Team team : vo.getTeamList()) {
  33. System.out.println(team);
  34. }
  35. return new ModelAndView("ok");
  36. }
  37. //7、获取数组类型的参数
  38. @RequestMapping("test07")
  39. public ModelAndView test07(String[] teamName,HttpServletRequest request){
  40. System.out.println("test07-----------------");
  41. //方式1:
  42. for (String s : teamName) {
  43. System.out.println(s);
  44. }
  45. System.out.println("---------------");
  46. //方式2:
  47. String[] teamNames = request.getParameterValues("teamName");
  48. for (String name : teamNames) {
  49. System.out.println(name);
  50. }
  51. return new ModelAndView("ok");
  52. }
  53. //6、获取日期类型的参数:实体类中的对应的属性上必须有注解 @DateTimeFormat(pattern = "yyyy-MM-dd")
  54. @RequestMapping("test06")
  55. public ModelAndView test06(Team team){
  56. System.out.println("test06-----------------");
  57. System.out.println(team);
  58. return new ModelAndView("ok");
  59. }
  60. //5、直接使用URL地址传参: 借助@PathVariable 注解
  61. // 例如http://localhost:8080/param/test05/1001/lacker/las
  62. @RequestMapping("test05/{id}/{name}/{loc}")
  63. public ModelAndView test05(@PathVariable("id") Integer teamId,
  64. @PathVariable("name") String teamName,
  65. @PathVariable("loc") String teamLocation){
  66. System.out.println("test05-----------------");
  67. System.out.println(teamId);
  68. System.out.println(teamName);
  69. System.out.println(teamLocation);
  70. return new ModelAndView("ok");
  71. }
  72. //4、使用HttpServletRequest 对象获取参数:跟原来的javaWeb项目中使用的方式是一样的
  73. @RequestMapping("test04")
  74. public ModelAndView test04(HttpServletRequest request){
  75. System.out.println("test04-----------------");
  76. String teamId = request.getParameter("teamId");
  77. String teamName = request.getParameter("teamName");
  78. String location = request.getParameter("location");
  79. if(teamId!=null)
  80. System.out.println(Integer.valueOf(teamId));
  81. System.out.println(teamName);
  82. System.out.println(location);
  83. return new ModelAndView("ok");
  84. }
  85. //3、请求参数和方法名称的参数不一致:使用@RequestParam进行矫正,
  86. // value属性表示请求中的参数名称
  87. // required属性表示参数是否是必须的:true:必须赋值,否则报出400错;false:可以不赋值,结果就是null
  88. @RequestMapping("test03")
  89. public ModelAndView test03(@RequestParam(value = "teamId",required = false) Integer id,
  90. @RequestParam(value = "teamName",required = true) String name,
  91. @RequestParam("location") String loc){
  92. System.out.println("test03-----------------");
  93. System.out.println(id);
  94. System.out.println(name);
  95. System.out.println(loc);
  96. return new ModelAndView("ok");
  97. }
  98. //2、使用对象接收多个参数:要求用户请求中携带的参数名称必须是实体类中的属性保持一致,否则就获取不到
  99. @RequestMapping("test02")
  100. public ModelAndView test02(Team team){
  101. System.out.println("test02-----------------");
  102. System.out.println(team);
  103. return new ModelAndView("ok");
  104. }
  105. /**
  106. * 1、直接使用方法的参数逐个接收:方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
  107. * 好处:不需要类型转换
  108. */
  109. @RequestMapping("test01")
  110. public ModelAndView test01(Integer teamId,String teamName,String teamLocation){
  111. System.out.println("test01-----------------");
  112. System.out.println(teamId);
  113. System.out.println(teamName);
  114. System.out.println(teamLocation);
  115. return new ModelAndView("ok");
  116. }
  117. @RequestMapping("hello")
  118. public ModelAndView hello(){
  119. return new ModelAndView("hello");
  120. }
  121. }

hello.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>hello</title>
  5. </head>
  6. <body>
  7. <h3>8、获取集合类型的参数</h3>
  8. <form action="/param/test08" method="post">
  9. 球队名称1:<input type="text" name="teamName"/><br/>
  10. 球队名称2:<input type="text" name="teamName"/><br/>
  11. 球队名称3:<input type="text" name="teamName"/><br/>
  12. <button type="submit">提交</button>
  13. </form>
  14. <form action="/param/test09" method="post">
  15. 球队id1:<input type="text" name="teamList[0].teamId"/><br/>
  16. 球队id2:<input type="text" name="teamList[1].teamId"/><br/>
  17. 球队id3:<input type="text" name="teamList[2].teamId"/><br/>
  18. 球队名称1:<input type="text" name="teamList[0].teamName"/><br/>
  19. 球队名称2:<input type="text" name="teamList[1].teamName"/><br/>
  20. 球队名称3:<input type="text" name="teamList[2].teamName"/><br/>
  21. <button type="submit">提交</button>
  22. </form>
  23. <h3>7、获取数组类型的参数</h3>
  24. <form action="/param/test07" method="post">
  25. 球队名称1:<input type="text" name="teamName"/><br/>
  26. 球队名称2:<input type="text" name="teamName"/><br/>
  27. 球队名称3:<input type="text" name="teamName"/><br/>
  28. <button type="submit">提交</button>
  29. </form>
  30. <h3>6、获取日期类型的参数</h3>
  31. <form action="/param/test06" method="post">
  32. 球队id:<input type="text" name="teamId"/><br/>
  33. 球队名称:<input type="text" name="teamName"/><br/>
  34. 球队位置:<input type="text" name="location"/><br/>
  35. 创建日期:<input type="text" name="createTime"/><br/>
  36. <button type="submit">提交</button>
  37. </form>
  38. <h3>5、直接使用URL地址传参</h3>
  39. <h3>4、使用HttpServletRequest 对象获取参数</h3>
  40. <form action="/param/test04" method="post">
  41. 球队id:<input type="text" name="teamId"/><br/>
  42. 球队名称:<input type="text" name="teamName"/><br/>
  43. 球队位置:<input type="text" name="location"/><br/>
  44. <button type="submit">提交</button>
  45. </form>
  46. <h3>3、请求参数和方法名称的参数不一致</h3>
  47. <form action="/param/test03" method="post">
  48. 球队id:<input type="text" name="teamId"/><br/>
  49. 球队名称:<input type="text" name="teamName"/><br/>
  50. 球队位置:<input type="text" name="location"/><br/>
  51. <button type="submit">提交</button>
  52. </form>
  53. <h3>2、使用对象接收多个参数</h3>
  54. <form action="/param/test02" method="post">
  55. 球队id:<input type="text" name="teamId"/><br/>
  56. 球队名称:<input type="text" name="teamName"/><br/>
  57. 球队位置:<input type="text" name="location"/><br/>
  58. <button type="submit">提交</button>
  59. </form>
  60. <h3>1、直接使用方法的参数逐个接收</h3>
  61. <form action="/param/test01" method="post">
  62. 球队id:<input type="text" name="teamId"/><br/>
  63. 球队名称:<input type="text" name="teamName"/><br/>
  64. 球队位置:<input type="text" name="teamLocation"/><br/>
  65. <button type="submit">提交</button>
  66. </form>
  67. </body>
  68. </html>