目录
1.直接使用方法的参数逐个接收
@Controller
@RequestMapping("param")
public class ParamController {
@RequestMapping("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");
}
}
<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>
直接使用方法的参数逐个接收
:
方法的参数名称必须与用户请求中携带的参数名称保持一致,否则
就获取不到
好处:不需要类型转换
2.使用对象接收多个参数
@RequestMapping("test02")
public ModelAndView test02(Team team){
System.out.println("test02-----------------");
System.out.println(team);
return new ModelAndView("ok");
}
public class Team {
private Integer teamId;
private String teamName;
private String location;
//省略getter和setter
public String toString() {
return "Team{" +
"teamId=" + teamId +
", teamName='" + teamName + '\'' +
", location='" + location'}';
}
}
<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>
使用对象接收多个参数:要求
用户请求中携带的参数名称必须是实体类中的属性保持一致
,否则就
获取不到
3.请求参数和方法名称的参数不一致
@RequestMapping("test03")
public ModelAndView test03(@RequestParam(value = "id",required =
false,defaultValue = "1") Integer teamId,
@RequestParam(value = "name") String teamName,
@RequestParam(value = "location") String
teamLocation){
System.out.println("test03--------------");
System.out.println(teamId);
System.out.println(teamName);
System.out.println(teamLocation);
return new ModelAndView("OK");
}
<form action="/param/test03" method="post">
球队ID:<input name="id" /><br/>
球队name:<input name="name" /><br/>
球队location:<input name="location" /><br/>
<button type="submit">提交</button>
</form>
请求参数和方法名称的参数不一致
:
使用@RequestParam进行矫正
value属性表示请求中的参数名
required
属性表示参数是否是必须的:
true:必须赋值,否则报出400错;false:可以不赋值,
结果就是null
defaultValue=
自定义默认值 如果没有从用户请求中获取到值,使用默认值
4.使用HttpServletRequest 对象获取参数
@RequestMapping("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");
}
<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>
使用HttpServletRequest 对象获取参数:跟原来的javaWeb项目中使用的方式是一样的
5.直接使用URL地址传参
@RequestMapping("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");
}
<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>
直接使用URL地址传参: 借助@PathVariable 注解
按照顺序接受,{id}接收teamId,{name}接收teamName,{loc}接收location
例如直接在地址栏输入:http://localhost:8080/param/test05/1001/lacker/las
6.获取日期类型的参数
@RequestMapping("test06")
public ModelAndView test06(Team team){
System.out.println("test06-----------------");
System.out.println(team);
return new ModelAndView("ok");
}
public class Team {
private Integer teamId;
private String teamName;
private String location;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createDate;
//省略setter和getter
public String toString() {
return "Team{" +
"teamId=" + teamId +
", teamName='" + teamName + '\'' +
", location='" + location + '\'' +
", createDate=" + createDate +
'}';
}
}
<form action="/param/test06" method="post">
球队ID:<input name="teamId" /><br/>
球队name:<input name="teamName" /><br/>
球队location:<input name="location" /><br/>
日期:<input name="createDate" /><br/>
<button type="submit">提交</button>
</form>
通过注释DateTimeFormat定义格式来接收日期
7.获取数组类型的参数
@RequestMapping("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:通过HttpServletRequest对象获取参数
String[] teamNames = request.getParameterValues("teamName");
for (String name : teamNames) {
System.out.println(name);
}
return new ModelAndView("ok");
}
<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>
有多中方法来获取数组,按自己需求决定
8.获取集合类型的参数
@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");
}
@RequestMapping("test09")
public ModelAndView test09(QueryVO vo){
System.out.println("test09-----------------");
for (Team team : vo.getTeamList()) {
System.out.println(team);
}
return new ModelAndView("ok");
}
public class QueryVO {
private List<Team> teamList;
public List<Team> getTeamList() {
return teamList;
}
public void setTeamList(List<Team> teamList) {
this.teamList = teamList;
}
}
<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>
求简单类型的参数可以直接使用注解@RequestParam获取,但是对于对象的集合不支持,对象的集合必须封装在类中,作为一个属性操作