SpringMVC_[2]处理器参数的获取

  • 参数的获取
  • 1 直接使用方法的参数逐个接收
  • 2 使用对象接收多个参数
  • 3 请求参数和方法名称的参数不一致
  • 4 使用HttpServletRequest 对象获取参数
  • 5 直接使用URL地址传参
  • 6 获取日期类型的参数
  • 7 获取数组类型的参数
  • 8 获取集合类型的参数


参数的获取

注意:SpringMVC不支持直接从参数中获取对象集合类型,需要将对象集合封装到实体类中。
先创建一个bean实体类

public class Team {
    private Integer teamId;
    private String teamName;
    private String location;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;

    @Override
    public String toString() {
        return "Team{" + "teamId=" + teamId + ", teamName='" + teamName + '\'' + ", location='" + location + '\'' + ", createTime=" + createTime + '}';
    } 
    //省略set get方法 
}

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

<h3>1、直接使用方法的参数逐个接收</h3>
<form action="/param/test01" method="post"> 
    球队id:<input type="text" name="teamId"/><br/> 
    球队名称:<input type="text" name="teamName"/><br/>
    球队位置:<input type="text" name="teamLocation"/><br/>
    <button type="submit">提交</button>
</form>
/*** 
     * 1、直接使用方法的参数逐个接收:方法的参数名称必须与用户请求中携带的参数名称保持一致,否则 就获取不到
     * 好处:不需要类型转换
     */
    @RequestMapping("/param/test01")
    public ModelAndView test01(Integer teamId, String teamName, String teamLocation) {
        System.out.println("test01-----------------");
        System.out.println(teamId);
        System.out.println(teamName);
        System.out.println(teamLocation);
        return new ModelAndView("ok");
    }

2 使用对象接收多个参数

SpringMVC不支持直接从参数中获取对象集合类型,需要将对象集合封装到实体类中。

public class Team {
    private Integer teamId;
    private String teamName;
    private String location;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;

    @Override
    public String toString() {
        return "Team{" + "teamId=" + teamId + ", teamName='" + teamName + '\'' + ", location='" + location + '\'' + ", createTime=" + createTime + '}';
    } 
    //省略set get方法 
}
<h3>2、使用对象接收多个参数</h3>
<form action="/param/test02" method="post">
    球队id:<input type="text" name="teamId"/><br/>
    球队名称:<input type="text"  name="teamName"/><br/>
    球队位置:<input type="text" name="location"/><br/>
    <button type="submit">提交</button>
</form>
//2、使用对象接收多个参数:要求用户请求中携带的参数名称必须是实体类中的属性保持一致,否则就 获取不到    
	@RequestMapping("/param/test02")
    public ModelAndView test02(Team team) {
        System.out.println("test02-----------------");
        System.out.println(team);
        return new ModelAndView("ok");
    }

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

<h3>3、请求参数和方法名称的参数不一致</h3>
<form action="/param/test03" method="post">
    球队id:<input type="text" name="teamId"/><br/>
    球队名称:<input type="text" name="teamName"/><br/>
    球队位置:<input type="text" name="location"/><br/>
    <button type="submit">提交</button>
</form>
/**
     * 请求参数和方法名称的参数不一致:使用@RequestParam进行矫正,
     * value属性表示请求中的参数名称
     * required属性表示参数是否是必须的:true:必须赋值,否则报出400错;false:可以不赋值, 结果就是null
     */
    @RequestMapping("/param/test03")
    public ModelAndView test03(@RequestParam(value = "teamId", required = false) Integer id,
                               @RequestParam(value = "teamName", required = true) String name,
                               @RequestParam("location") String loc) {
        System.out.println("test03-----------------");
        System.out.println(id);
        System.out.println(name);
        System.out.println(loc);
        return new ModelAndView("ok");
    }

4 使用HttpServletRequest 对象获取参数

<h3>4、使用HttpServletRequest 对象获取参数</h3>
<form action="/param/test04" method="post">
    球队id:<input type="text" name="teamId"/><br/>
    球队名称:<input type="text" name="teamName"/><br/>
    球队位置:<input type="text" name="location"/><br/>
    <button type="submit">提交</button>
</form>
//4、使用HttpServletRequest 对象获取参数:跟原来的javaWeb项目中使用的方式是一样的
    @RequestMapping("/param/test04")
    public ModelAndView test04(HttpServletRequest request) {
        System.out.println("test04-----------------");
        String teamId = request.getParameter("teamId");
        String teamName = request.getParameter("teamName");
        String location = request.getParameter("location");
        if (teamId != null) {
            System.out.println(Integer.valueOf(teamId));
            System.out.println(teamName);
            System.out.println(location);
        }
        return new ModelAndView("ok");
    }

5 直接使用URL地址传参

<h3>5、直接使用URL地址传参(例如http://localhost:8080/param/test05/1001/lacker/las)</h3>
/**
     * 直接使用URL地址传参: 借助@PathVariable 注解
     * 例如http://localhost:8080/param/test05/1001/lacker/las
     * @return
     */
    @RequestMapping("/param/test05/{id}/{name}/{loc}")
    public ModelAndView test05(@PathVariable("id") Integer teamId,
                               @PathVariable("name") String teamName,
                               @PathVariable("loc") String teamLocation) {
        System.out.println("test05-----------------");
        System.out.println(teamId);
        System.out.println(teamName);
        System.out.println(teamLocation);
        return new ModelAndView("ok");
    }

6 获取日期类型的参数

<h3>5、直接使用URL地址传参(例如http://localhost:8080/param/test05/1001/lacker/las)</h3>
<h3>6、获取日期类型的参数</h3>
<form action="/param/test06" method="post"> 
    球队id:<input type="text" name="teamId"/><br/> 
    球队名称:<input type="text" name="teamName"/><br/>
    球队位置:<input type="text" name="location"/><br/> 
    创建日期:<input type="text" name="createTime"/><br/>
    <button type="submit">提交</button>
</form>
//6、获取日期类型的参数
    @RequestMapping("/param/test06")
    public ModelAndView test06(Team team) {
        System.out.println("test06-----------------");
        System.out.println(team);
        return new ModelAndView("ok");
    }

7 获取数组类型的参数

<h3>7、获取数组类型的参数</h3>
<form action="/param/test07" method="post">
    球队名称1:<input type="text" name="teamName"/><br/>
    球队名称2:<input type="text" name="teamName"/><br/>
    球队名称3:<input type="text" name="teamName"/><br/>
    <button type="submit">提交</button>
</form>
//7、获取数组类型的参数
    @RequestMapping("/param/test07")
    public ModelAndView test07(String[] teamName, HttpServletRequest request) {
        System.out.println("test07-----------------");
        //方式1:
        for (String s : teamName) {
            System.out.println(s);
        }
        System.out.println("---------------");
        //方式2:
        String[] teamNames = request.getParameterValues("teamName");
        for (String name : teamNames) {
            System.out.println(name);
        }
        return new ModelAndView("ok");
    }

8 获取集合类型的参数

<h3>8、获取集合类型的参数</h3>
<form action="/param/test08" method="post">
    球队名称1:<input type="text" name="teamName"/><br/>
    球队名称2:<input type="text" name="teamName"/><br/>
    球队名称3:<input type="text" name="teamName"/><br/>
    <button type="submit">提交</button>
</form>

<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>

把集合类型的参数封装为实体类

public class QueryVO {
        private List<Team> teamList;

        public List<Team> getTeamList() {
            return teamList;
        }

        public void setTeamList(List<Team> teamList) {
            this.teamList = teamList;
        }
    }
//8、获取集合类型的参数: 简单类型的可以通过@RequestParam注解实现;对象集合不支持直接获 取,必须封装在类中,作为一个属性操作   
    @RequestMapping("/param/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");
    }

    @RequestMapping("/param/test09")
    public ModelAndView test09(QueryVO vo) {
        System.out.println("test09-----------------");
        for (Team team : vo.getTeamList()) {
            System.out.println(team);
        }
        return new ModelAndView("ok");
    }

最后,如果有问题,希望指正,一起进步。