当前位置:   article > 正文

SpringMvc处理器方法参数获取

SpringMvc处理器方法参数获取

 1、直接使用方法的参数逐个接收

参数名称前端和后端一一对应

  1. // 1:直接使用方法的参数逐个接收 要求:参数的名称必须与请求中携带的参数名称一致
  2. // 优势:不需要类型转换,前后端类型直接对应
  3. @RequestMapping("test01")
  4. public ModelAndView test01(Integer teamId, String teamName, String teamLocation) {
  5. System.out.println("teamId: " + teamId + " teamName: " + teamName + " teamLocation: " + teamLocation);
  6. return new ModelAndView("OK");
  7. }
  1. <h3>1、直接使用方法的参数逐个接收</h3>
  2. <form action="/param/test01" method="post">
  3. 球队ID:<input name="teamId"/><br>
  4. 球队name:<input name="teamName"/><br>
  5. 球队location:<input name="teamLocation"/><br>
  6. <button type="submit">提交</button>
  7. </form>

2、用对象接收多个参数

要求:请求中携带的参数必须域对象属性名一致

  1. <h3>2、使用对象接收多个参数</h3>
  2. <form action="/param/test02" method="post">
  3. 球队ID:<input name="teamId"/><br>
  4. 球队name:<input name="teamName"/><br>
  5. 球队location:<input name="location"/><br>
  6. <button type="submit">提交</button>
  7. </form>
  1. public class Team {
  2. private Integer teamId;
  3. private String teamName;
  4. private String location;
  5. }
  6. @RequestMapping("test02")
  7. public ModelAndView test02(Team team) {
  8. System.out.println(team);
  9. return new ModelAndView("OK");
  10. }

3、请求参数和方法参数名称不一致

请求参数和方法名称的参数不一致 使用注解@RequestParam(value"请求中的名称")
 required: 表示参数是否是必须的,true:没有则报错 false;没有该参数则报错
 defaultValue: 表示参数为空时的默认值

  1. <h3>3、请求参数和方法名称的参数不一致</h3>
  2. <form action="/param/test03" method="post">
  3. 球队ID:<input name="teamId"/><br>
  4. 球队name:<input name="teamName"/><br>
  5. 球队location:<input name="location"/><br>
  6. <button type="submit">提交</button>
  7. </form>
  1. @RequestMapping("test03")
  2. public ModelAndView test03(@RequestParam(value = "teamId", required = false, defaultValue = "0") Integer id,
  3. @RequestParam(value = "teamName") String name,
  4. @RequestParam(value = "location") String location) {
  5. System.out.println("teamId: " + id + " teamName: " + name + " location: " + location);
  6. return new ModelAndView("OK");
  7. }

4、使用HttpServletRequest对象获取参数

跟javaweb中是一样的,因为Springmvc框架的底层是由servlet实现的

  1. <h3>4、使用HttpServletRequest获取参数</h3>
  2. <form action="/param/test04" method="post">
  3. 球队ID:<input name="teamId"/><br>
  4. 球队name:<input name="teamName"/><br>
  5. 球队location:<input name="location"/><br>
  6. <button type="submit">提交</button>
  7. </form>
  1. @RequestMapping("test04")
  2. public ModelAndView test04(HttpServletRequest req) {
  3. String teamName = req.getParameter("teamName");
  4. String id = req.getParameter("teamId");
  5. String location = req.getParameter("location");
  6. System.out.println("teamName: " + teamName + " id: " + id + " location: " + location);
  7. return new ModelAndView("OK");
  8. }

5、使用url地址传参

  1. @RequestMapping("test05/{id}/{name}/{loc}")
  2. public ModelAndView test05(@PathVariable("id") Integer id, @PathVariable("name") String name, @PathVariable("loc") String loc) {
  3. System.out.println("teamId: " + id + " teamName: " + name + " location: " + loc);
  4. return new ModelAndView("OK");
  5. }

6、获取日期类型参数

对象类型中的日期属性上需要添加注解@DateFormat(pattern = "yyyy-MM-dd"),通过pattern参数来修改日期的格式

  1. <h3>6、获取日期类型的参数</h3>
  2. <form action="/param/test06" method="post">
  3. 球队ID:<input name="teamId"/><br>
  4. 球队name:<input name="teamName"/><br>
  5. 球队location:<input name="location"/><br>
  6. 球队date:<input name="date"/><br>
  7. <button type="submit">提交</button>
  8. </form>
  1. public class Team {
  2. private Integer teamId;
  3. private String teamName;
  4. private String location;
  5. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  6. private Date date;
  7. }
  8. @RequestMapping("test06")
  9. public ModelAndView test06(Team team) {
  10. System.out.println(team);
  11. return new ModelAndView("OK");
  12. }

7、获取数组类型参数

  1. <h3>7、获取数组类型的参数</h3>
  2. <form action="/param/test07" method="post">
  3. 球队name1:<input name="teamName"/><br>
  4. 球队name2:<input name="teamName"/><br>
  5. 球队name3:<input name="teamName"/><br>
  6. 球队name4:<input name="teamName"/><br>
  7. <button type="submit">提交</button>
  8. </form>
  1. @RequestMapping("test07")
  2. public ModelAndView test07(String[] teamName, HttpServletRequest req) {
  3. // 方式1
  4. for(String i : teamName) {
  5. System.out.println(i);
  6. }
  7. System.out.println("-----");
  8. // 方式2
  9. String[] teamNames = req.getParameterValues("teamName");
  10. for(String i : teamNames) {
  11. System.out.println(i);
  12. }
  13. return new ModelAndView("OK");
  14. }

8、获取集合类型参数

简单类型的可以通过@RequestParam注解实现;对象集合不支持直接获取,必须封装在类中,作为一个属性操作

  1. <form action="/param/test09" method="post">
  2. 球队id1:<input type="text" name="teamList[0].teamId"/><br/>
  3. 球队id2:<input type="text" name="teamList[1].teamId"/><br/>
  4. 球队id3:<input type="text" name="teamList[2].teamId"/><br/>
  5. 球队名称1:<input type="text" name="teamList[0].teamName"/><br/>
  6. 球队名称2:<input type="text" name="teamList[1].teamName"/><br/>
  7. 球队名称3:<input type="text" name="teamList[2].teamName"/><br/>
  8. <button type="submit">提交</button>
  9. </form>
  1. @RequestMapping("test08")
  2. public ModelAndView test08(@RequestParam("teamName") List<String> nameList){
  3. System.out.println("test08-----------------");
  4. for (String s : nameList) {
  5. System.out.println(s);
  6. }
  7. return new ModelAndView("ok");
  8. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/385843
推荐阅读
相关标签
  

闽ICP备14008679号