SpringMVC中的Controller如何获取请求中的参数
转自https://blog.****.net/a909301740/article/details/80411114
一、默认支持的类型
可以从 HttpServletRequest、HttpServletResponse、HttpSession 中获取请求中的参数。
测试类如下:
package com.lyu.qjl.interview.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 类名称:用于测试的控制器
* 全限定性类名: com.lyu.qjl.interview.controller.TestController
* @author 曲健磊
* @date 2018年5月24日下午10:55:54
* @version V1.0
*/
@Controller
public class TestController {
// 通过 HttpServletRequest 和 HttpServletResponse 以及 HTTPSession 来获取请求中的参数
@RequestMapping("/testByParam")
public void getParamByReq(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
发送的请求如下:
控制台打印结果:
二、通过简单的数据类型来接收参数值
例如:int,string,double,float,etc 这些简单的数据类型参数来获取请求中的参数。
代码如下:
package com.lyu.qjl.interview.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 类名称:用于测试的控制器
* 全限定性类名: com.lyu.qjl.interview.controller.TestController
* @author 曲健磊
* @date 2018年5月24日下午10:55:54
* @version V1.0
*/
@Controller
public class TestController {
// 通过简单的数据类型来获取请求中的参数
@RequestMapping("/testBySimple")
public void getParamBySimple(String username, String password) {
System.out.println(username);
System.out.println(password);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
发送的请求如下:
控制台打印结果:
三、通过简单的pojo类来接收参数
也可以通过简单的 pojo 类型来接收参数,代码如下:
package com.lyu.qjl.interview.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lyu.qjl.interview.entity.User;
/**
* 类名称:用于测试的控制器
* 全限定性类名: com.lyu.qjl.interview.controller.TestController
* @author 曲健磊
* @date 2018年5月24日下午10:55:54
* @version V1.0
*/
@Controller
public class TestController {
@RequestMapping("/testByPojo")
public void getParamByPojo(User user) {
System.out.println(user.getUsername());
System.out.println(user.getPassword());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
发送的请求如下:
控制台打印结果:
注意:这里其实是 SpringMVC 通过反射调用的 User 的无参的构造方法 new 的对象,然后调用两个 setter 方法来进行的参数注入,如果 User 类没有无参构造或者没有响应的 setter 方法结果就为空。
四、通过包装类来接收参数
包装类代码如下:
package com.lyu.qjl.interview.vo;
import java.util.List;
import com.lyu.qjl.interview.entity.User;
/**
* 类名称:用户包装类
* 全限定性类名: com.lyu.qjl.interview.vo.UserVO
* @author 曲健磊
* @date 2018年5月25日下午9:27:42
* @version V1.0
*/
public class UserVO {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "UserVO [user=" + user.toString() + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
Controller 代码如下:
package com.lyu.qjl.interview.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.lyu.qjl.interview.entity.User;
import com.lyu.qjl.interview.vo.UserVO;
/**
* 类名称:处理用户请求的handler
* 全限定性类名: com.lyu.qjl.interview.controller.UserController
* @author 曲健磊
* @date 2018年5月21日下午7:24:07
* @version V1.0
*/
@Controller
public class UserController{
// 通过包装类来获取页面信息
@RequestMapping("/saveUserVO")
public String saveUserVO(UserVO userVO) {
System.out.println(userVO.toString());
return "userEdit";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
页面代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
%>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户列表</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<h1>用户新增/修改页面,通过包装类来获取提交的参数</h1>
<form action="<%=path%>/saveUserVO" method="post">
<h2>用户名称:<input type="text" name="user.username" /></h2>
<h2>用户性别:<input type="text" name="user.sex" /></h2>
<h2>用户年龄:<input type="text" name="user.age" /></h2>
<h2><input type="submit" value="保存"/></h2>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
注:name 属性值前要加上外层的对象名。
发送的请求如下:
控制台打印结果如下(只设置了 3 个属性):
五、通过集合类型获取
如果前台传过来的是一组数据该怎么接受呢?(不用 json 格式的字符串)
针对单个属性的话,可以使用数组。(针对批量删除)
针对多个属性的话,仍需要使用包装类。(针对批量修改)
前台页面代码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
%>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户列表</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<h1>批量删除</h1>
<form action="<%=path%>/batchDelUser" method="post">
<table>
<thead>
<tr>
<th>全选</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<c:if test="${userList != null}">
<c:forEach items="${requestScope.userList}" var="user">
<tr>
<td><input type="checkbox" name="userId" value="${user.userId}" /></td>
<td>${user.username}</td>
<td>${user.age}</td>
<td>${user.sex}</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${userList == null}">
<tr>
<td colspan="4">没有查询到用户信息</td>
</tr>
</c:if>
</tbody>
</table>
<input type="submit" value="批量删除用户" />
</form>
<h1>批量修改</h1>
<form action="<%=path%>/batchUpdateUser" method="post">
<table>
<thead>
<tr>
<th>全选</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<c:if test="${userList != null}">
<c:forEach items="${requestScope.userList}" var="user" varStatus="i">
<tr>
<td><input type="checkbox" name="userList[${i.index}].userId" value="${user.userId}" /></td>
<td><input type="text" name="userList[${i.index}].username" value="${user.username}" /></td>
<td><input type="text" name="userList[${i.index}].age" value="${user.age}" /></td>
<td><input type="text" name="userList[${i.index}].sex" value="${user.sex}" /></td>
</tr>
</c:forEach>
</c:if>
<c:if test="${userList == null}">
<tr>
<td colspan="4">没有查询到用户信息</td>
</tr>
</c:if>
</tbody>
</table>
<input type="submit" value="批量修改用户" />
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
Controller 代码:
package com.lyu.qjl.interview.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.lyu.qjl.interview.entity.User;
import com.lyu.qjl.interview.vo.UserVO;
/**
* 类名称:处理用户请求的handler
* 全限定性类名: com.lyu.qjl.interview.controller.UserController
* @author 曲健磊
* @date 2018年5月21日下午7:24:07
* @version V1.0
*/
@Controller
public class UserController{
// 根据用户id数组批量删除用户
@RequestMapping("/batchDelUser")
public String batchDelUser(Integer[] userId) {
for (int i = 0; i < userId.length; i++) {
System.out.println(userId[i]);
}
return "userList";
}
// 根据用户id数组批量修改用户
@RequestMapping("/batchUpdateUser")
public String batchUpdateUser(UserVO userVO) {
System.out.println("得到的用户数:" + userVO.getUserList().size());
for (int i = 0; i < userVO.getUserList().size(); i++) {
System.out.println(userVO.getUserList().get(i));
}
return "userList";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
批量删除:
控制台信息:
批量修改:
控制台信息: