SpringMVC中的Controller如何获取请求中的参数

1、可以通过HttpServletRequest、HttpServletResponse、HttpSession 中获取请求中的参数。

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

这里写图片描述

2、通过int,string,double,float,etc 这些简单的数据类型参数来获取请求中的参数

@Controller
public class TestController {
    // 通过简单的数据类型来获取请求中的参数
    @RequestMapping("/testBySimple")
    public void getParamBySimple(String username, String password) {
        System.out.println(username);
        System.out.println(password);
    }

}

这里写图片描述

3.通过简单的Pojo类来接收参数

@Controller
public class TestController {

    @RequestMapping("/testByPojo")
    public void getParamByPojo(User user) {
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
    }
}

这里写图片描述

把请求中的参数值注入到user对象的对应属性中。

注意:这里其实是 SpringMVC 通过反射调用的 User 的无参的构造方法 new 的对象,然后调用两个 setter 方法来进行的参数注入,如果 User 类没有无参构造或者没有响应的 setter 方法结果就为空。

四、通过包装类来接收参数

Controller如下:

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() + "]";
    }
}

页面代码如下:

<%@ 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>

注:name 属性值前要加上外层的对象名。与上一种方法的不同之处在于这里的请求传递的是对象,上一种方法传递对象中的属性。

这里写图片描述

发送的请求如下:
这里写图片描述

控制台打印结果如下(只设置了 3 个属性):
这里写图片描述

注:password没有在form表单中 被赋值,因此为null。 

五、通过集合类型获取

如果前台传过来的是一组数据该怎么接受呢?(不用 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>

Controller 代码:

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

批量删除:
这里写图片描述
控制台信息:
这里写图片描述

批量修改:
这里写图片描述

控制台信息:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值