getParameterMap()返回参数需要对应实体类类型,否则收不到----打卡

2020-10-23 10:39:42 浏览数 (1)

1.在jsp页面传到controller许多参数,使用getParameterMap()获得

1.1register.jsp页面

代码语言:javascript复制
<%--
  Created by IntelliJ IDEA.
  User: 49841
  Date: 2020/9/18
  Time: 11:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册页面</title>
</head>
<body>
    <form method="post" action="usersController?method=register">
        <input type="text" name="uname" placeholder="请输入你的用户名"/>
        <input type="text" name="upassword" placeholder="请输入你的密码"/>
        <input type="text" name="uemail" placeholder="请输入你的邮箱"/>
        <input type="text" name="usex" placeholder="请输入你的性别"/>
        <input type="submit" value="提交"/>
    </form>
<%--    <span style="color:red">${msg}</span>--%>
</body>
</html>

1.2.controller(Servlet),因为后面还写了BaseController,方法就没写在doget、dopost里面

代码语言:javascript复制
//注册
    public String register(HttpServletRequest request, HttpServletResponse response) throws Exception{
        User user = new User();
        //得到参数并封装到user,并为其中为null的属性赋值
        BeanUtils.populate(user, request.getParameterMap());

        user.setUstatus(SysConstant.NOT_ACTIVE);//激活状态为0
        user.setUcode(ActiveCodeUtils.getActiveCode());
        user.setUrole(SysConstant.CUSTOMER);
        //添加进数据库-->邮箱-->(emailActivate)点击激活
        int i = userService.register(user);
        request.setAttribute("url",userService.getUrl());

        //已经激活的状态
        if(i>0){
            //注册成功
            return SysConstant.FORWARD SysConstant.FLAG "registerSuccess.jsp";
        }else{
            //注册失败
            request.setAttribute("registerMsg", "注册失败");
            return SysConstant.FORWARD SysConstant.FLAG "register.jsp";
        }
    }

1.3User实体类

代码语言:javascript复制
public class User {
    private int uid;
    private String uname;
    private String upassword;
    private String uemail;
    private String usex;
    private int ustatus;
    private String ucode;
    private int urole;

    @Override
    public String toString() {
        return "User{"  
                "uid="   uid  
                ", uname='"   uname   '''  
                ", upassword='"   upassword   '''  
                ", uemail='"   uemail   '''  
                ", usex='"   usex   '''  
                ", ustatus="   ustatus  
                ", ucode='"   ucode   '''  
                ", urole="   urole  
                '}';
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpassword() {
        return upassword;
    }

    public void setUpassword(String upassword) {
        this.upassword = upassword;
    }

    public String getUemail() {
        return uemail;
    }

    public void setUemail(String uemail) {
        this.uemail = uemail;
    }

    public String getUsex() {
        return usex;
    }

    public void setUsex(String usex) {
        this.usex = usex;
    }

    public int getUstatus() {
        return ustatus;
    }

    public void setUstatus(int ustatus) {
        this.ustatus = ustatus;
    }

    public String getUcode() {
        return ucode;
    }

    public void setUcode(String ucode) {
        this.ucode = ucode;
    }

    public int getUrole() {
        return urole;
    }

    public void setUrole(int urole) {
        this.urole = urole;
    }
}

2.说明:

&nbus之前图方便,使用getParameterMap()加 BeanUtils.populate(),得到参数就直接封装到user对象里面了,没有使用request.setAttribute(),一个名一个值的这样一个个参数的获取,再new user放进去。
所有采坑了,因为jsp页面input标签里面名称和实体类定义的属性名不一样,所以获取不到值,最后得到了一个空的user对象。呱~

0 人点赞