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

直接使用方法的参数逐个接收:方法的参数名称必须与用户请求中携带的参数名称(name的值)保持一致,否则就获取不到。
 好处:不需要类型转换

<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>
@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.使用对象接收多个参数

使用对象接收多个参数:要求用户请求中携带的参数名称必须是实体类中的属性保持一致,否则就 获取不到,就是null。

import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
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方法
}
<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>
@RequestMapping("param/test02")
public ModelAndView test02(Team team){
System.out.println("test02-----------------");
System.out.println(team);
return new ModelAndView("ok");
}

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

请求参数和方法名称的参数不一致:使用@RequestParam进行矫正。
value属性表示请求中的参数名称
 required属性表示参数是否是必须的:true:必须赋值,否则报出400错;false:可以不赋值,
结果就是null   required属性默认是false

<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>
@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 对象获取参数

使用HttpServletRequest 对象获取参数:跟原来的javaWeb项目中使用的方式是一样的

这里获取的类型都是String类型,需要自己根据前端野页面的参数类型进行转换。

<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>
@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)
7、请求参数中文乱码
System.out.println(Integer.valueOf(teamId));
System.out.println(teamName);
System.out.println(location);
return new ModelAndView("ok");
}

5.直接使用URL地址传参

借助@PathVariable 注解

例如http://localhost:8080/param/test05/1001/lacker/las  这里采用的是RESTful风格

@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.获取日期类型的参数

不加注解的话会报BindException错误

Spring 入参变量值作为注解值_实体类

在实体类中加上DateTimeFormat注解

@DateTimeFormat(pattern = "yyyy-MM-dd")

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createTime;
<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>
@RequestMapping("test06")
public ModelAndView test06(Team team){
System.out.println("test06-----------------");
System.out.println(team);
return new ModelAndView("ok");
}

7.获取数组类型的参数

两种方式获取

1.通过数组方式  数组的参数名称要和 前端页面的name属性值保持一致,

参数名称不一致时,使用@RequestParam进行矫正。

2.HttpServletRequest 类型对象调用getParameterValues("name属性值")

<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>
@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.获取集合类型的参数

如果是集合类型的 List<泛型> teamName   不在类型前添加@RequestParam("name属性的值")注解的情况下获取集合是获取不到的

Spring 入参变量值作为注解值_spring_02

 

获取集合类型的参数: 简单类型的可以通过@RequestParam注解实现

<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>
@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");
}

对象集合不支持直接获 取,必须封装在类中,作为一个属性操作(这种需求是添加多组对象时候用到)

创建的实体类

import com.kkb.pojo.Team;
import java.util.List;
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/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("test09")
public ModelAndView test09(QueryVO vo){
System.out.println("test09-----------------");
for (Team team : vo.getTeamList()) {
System.out.println(team);
}
return new ModelAndView("ok");
}