携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第20天,点击查看活动详情
哈喽,大家好!我是Why,一名在读学生,目前刚刚开始进入自己的编程学习生涯。虽然学习起步较晚,但我坚信做了才有0或1的可能。学了一段时间以后也是选择在掘金上分享自己的日常笔记,也希望能够在众多道友的大家庭中打成一片。 本文主要讲解处理器方法的参数,如果大家读后觉得有用的话,还请大家多多支持博主:欢迎 ❤️点赞👍、收藏⭐、留言💬 ✨✨✨个人主页:JinHuan
4、使用HttpServletRequest 对象获取参数
<h3>4、使用HttpServletRequest 对象获取参数</h3>
<form action="/param/test04" method="post">
球队id:<input type="text" name="teamId"><br/>
球队name:<input type="text" name="teamName"><br/>
球队location:<input type="text" name="teamLocation"><br/>
<button type="submit">提交</button>
</form>
/**
* @param request:
* @return org.springframework.web.servlet.ModelAndView
* decription: 原生方法,使用HttpServletRequest依次获取数据,判空,转换
*/
@RequestMapping("test04")
public ModelAndView test04(HttpServletRequest request){
System.out.println("test04");
String teamId = request.getParameter("teamId");
String teamName = request.getParameter("teamName");
String teamLocation = request.getParameter("teamLocation");
//判空
if (teamId != null) {
System.out.println(Integer.valueOf(teamId)) ;
}
if (teamName != null) {
System.out.println(teamName);
}
if (teamLocation != null) {
System.out.println(teamLocation);
}
return new ModelAndView("ok");
}
5、使用URL获取参数
/**
* @param id:
* @param name:
* @param location:
* @return org.springframework.web.servlet.ModelAndView
* decription: 根据URL直接获取数据:例如:http://localhost:8080/param/test05/1001/jinhuan/china
* 需要借助@PathVariable注解以及 @RequestMapping("test05/{id}/{name}/{location}")参数
*/
@RequestMapping("test05/{id}/{name}/{location}")
public ModelAndView test05(@PathVariable("id") Integer id,
@PathVariable("name")String name,
@PathVariable("location")String location){
System.out.println("test05");
System.out.println(id);
System.out.println(name);
System.out.println(location);
return new ModelAndView("ok");
}
6、获取日期类参数
<h3>6、获取日期类参数</h3>
<form action="/param/test06" method="post">
球队id:<input type="text" name="teamId"><br/>
球队name:<input type="text" name="teamName"><br/>
球队location:<input type="text" name="teamLocation"><br/>
创建日期:<input type="text" name="creatTime"><br/>
<button type="submit">提交</button>
</form>
/**
* @param team:
* @return org.springframework.web.servlet.ModelAndView
* decription: 获取日期类的参数
*/
@RequestMapping("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/>
球队4:<input type="text" name="teamName"><br/>
<button type="submit">提交</button>
</form>
/**
* @param teamName:获取的数组对象,注意参数名和方法中的参数名称必须一致
* @return org.springframework.web.servlet.ModelAndView
* decription: 获取数组类型的参数【第一种】
*/
@RequestMapping("test07")
public ModelAndView test07(String[] teamName){
System.out.println("test07");
for (String team : teamName) {
System.out.println(team);
}
return new ModelAndView("ok");
}
第二种
public ModelAndView test07(HttpServletRequest request){
System.out.println("test07");
String[] teamName = request.getParameterValues("teamName");
for (String team : teamName) {
System.out.println(team);
}
return new ModelAndView("ok");
}