springmvc处理器方法的参数
处理器方法可以包含以下四类参数,这些参数会在系统调用时由系统自动赋值.所以我们可以在方法内直 接使用。以下是这四类参数:
-
HttpServletRequest
-
HttpServletResponse
-
HttpSession
-
请求中所携带的请求参数
准备工作:创建新的控制器ParamController.java和前端页面hello.jsp页面
package com.daw.controller;
import com.daw.pojo.Team;
import com.daw.vo.QueryVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* DAW
* ParamController
* 又不是不能用!
*/
@Controller
@RequestMapping("param")
public class ParamController {
@RequestMapping("hello")
public ModelAndView hello(){
return new ModelAndView("hello");
}
}
1、直接使用方法的参数逐个接收
前端页面
<%--
Created by IntelliJ IDEA.
User: Mr-DUAN
Date: 2021-08-18
Time: 14:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head>
<body>
<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>
</body>
</html>
处理器代码
package com.daw.controller;
import com.daw.pojo.Team;
import com.daw.vo.QueryVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* DAW
* ParamController
* 又不是不能用!
*/
@Controller
@RequestMapping("param")
public class ParamController {
/**
* 直接使用方法的参数逐个接收 方法的参数名称必须与用户请求中的名称相同
* @param teamId
* @param teamName
* @param teamLocation
* @return
*/
@RequestMapping("test01")
public ModelAndView test01(Integer teamId,String teamName,String teamLocation){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("teamId",teamId);
modelAndView.addObject("teamName",teamName);
modelAndView.addObject("teamLocation",teamLocation);
modelAndView.setViewName("ok");
System.out.println("test01---------------");
System.out.println(teamId);
System.out.println(teamName);
System.out.println(teamLocation);
return modelAndView;
}
@RequestMapping("hello")
public ModelAndView hello(){
return new ModelAndView("hello");
}
}
2 、使用对象接收多个参数
前端页面
<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("test02")
public ModelAndView test02(Team team){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("teamId",team.getTeamId());
modelAndView.addObject("teamName",team.getTeamName());
modelAndView.addObject("teamLocation",team.getLocation());
modelAndView.setViewName("ok");
System.out.println("test01---------------");
System.out.println(team);
return modelAndView;
}
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>
处理器代码
/3、请求参数和方法名称不一样
//value属性表示请求中的参数名称
//required属性表示参数是否是必须的 如果是ture 参数必须赋值 否则报错400 如果是false可以不赋值
@RequestMapping("test03")
public ModelAndView test03(@RequestParam(value = "teamId",required = false) Integer id,@RequestParam("teamName") String name,@RequestParam("location") String loc){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("teamId",id);
modelAndView.addObject("teamName",name);
modelAndView.addObject("teamLocation",loc);
modelAndView.setViewName("ok");
System.out.println("test01---------------");
return modelAndView;
}
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>
处理器代码
@RequestMapping("test04")
public ModelAndView test04(HttpServletRequest request){
Integer teamId = Integer.valueOf(request.getParameter("teamId"));
String teamName = request.getParameter("teamName");
String location = request.getParameter("location");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("teamId",teamId);
modelAndView.addObject("teamName",teamName);
modelAndView.addObject("teamLocation",location);
modelAndView.setViewName("ok");
System.out.println("test01---------------");
return modelAndView;
}
5、 直接使用URL地址传参
url地址:http://localhost:8080/param/test05/1001/lacker/las
处理器代码
//5、直接使用url地址进行传参:借助@PathVariable注解
// http://localhost:8080/param/test05/1001/lacker/las
@RequestMapping("test05/{id}/{name}/{loc}")
public ModelAndView test05(@PathVariable("id") Integer teamId,@PathVariable("name") String teamName,@PathVariable("loc") String teamLocation){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("teamId",teamId);
modelAndView.addObject("teamName",teamName);
modelAndView.addObject("teamLocation",teamLocation);
modelAndView.setViewName("ok");
System.out.println("test05---------------");
System.out.println(teamId);
System.out.println(teamName);
System.out.println(teamLocation);
return modelAndView;
}
6 获取日期类型的参数
前端页面
<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("test06")
public ModelAndView test06(Team team){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("teamId",team.getTeamId());
modelAndView.addObject("teamName",team.getTeamName());
modelAndView.addObject("teamLocation",team.getLocation());
modelAndView.addObject("createTime",team.getCreatetime());
modelAndView.setViewName("ok");
System.out.println("test01---------------");
System.out.println(team);
return modelAndView;
}
实体类Team
package com.daw.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* DAW
* Team
* 又不是不能用!
*/
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 +
'}';
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Integer getTeamId() {
return teamId;
}
public void setTeamId(Integer teamId) {
this.teamId = teamId;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
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("test07")
public ModelAndView test07(String[] teamName,HttpServletRequest request){
System.out.println("test07-------------------");
//1、方式1
for (String s : teamName) {
System.out.print(s+"\t");
}
//2、方式2
String[] teamNames = request.getParameterValues("teamName");
System.out.println("-----------------------------");
for (String name : teamNames) {
System.out.println(name+"\t");
}
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>
处理器代码
//8.获取集合类型的参数:简单类型的可以通过@RequestParam注解实现,对象集合不支持直接获取,必须封装在类中,作为一个属性操作
@RequestMapping("test08")
public ModelAndView test08(@RequestParam("teamName") List<String> teamName){
System.out.println("test08-------------------");
for (String s : teamName) {
System.out.println(s);
}
return new ModelAndView("ok");
}
SpringMVC不支持直接从参数中获取对象集合类型,需要将对象集合封装到实体类中。
案例源码:
修改实体类Team.java
package com.daw.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* DAW
* Team
* 又不是不能用!
*/
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 +
'}';
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Integer getTeamId() {
return teamId;
}
public void setTeamId(Integer teamId) {
this.teamId = teamId;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
创建实体类QueryVO.java
package com.daw.vo;
import com.daw.pojo.Team;
import java.util.List;
/**
* DAW
* QueryVO
* 又不是不能用!
*/
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");
}