会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132441个问题
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 31936楼
Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/随机森林算法 31937楼
Python 全系列/第一阶段:Python入门/函数和内存分析 31938楼
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 31939楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 31940楼
WEB前端全系列/第十九阶段:Vue2知识体系(旧)/Vue基础知识 31941楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 31942楼

Controller

package com.gakki.demo.controller;


import com.gakki.demo.pojo.LocalAuth;
import com.gakki.demo.pojo.PersonInfo;
import com.gakki.demo.service.LocalAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
public class LocalAuthController {
  //声明业务层对象
  @Autowired
  private LocalAuthService localAuthService;

  /**注册本地用户 第一步.*/
  @RequestMapping("/registerUser1")
  public String registerUser1(PersonInfo personInfo, HttpSession session) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date nowTime = new Date();
    personInfo.setCreateTime(nowTime);
    PersonInfo person = this.localAuthService.addPerson(personInfo);
    if (person != null) {
      Long userId = person.getUserId();
      session.setAttribute("userId", userId);
      return "redirect:register2";
    }
    session.setAttribute("msg", "注册失败");
    return "redirect:/error";
  }



  /**注册本地用户 第二步.*/
  @RequestMapping("/registerUser2")
  public String registerUser2(LocalAuth localAuth, Model model) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date nowTime = new Date();
    localAuth.setCreateTime(nowTime);
    //调用业务层对象完成注册操作
    int i = this.localAuthService.addUser(localAuth);
    if (i > 0) {
      return "redirect:login";
    }
    model.addAttribute("msg", "用户不存在");
    return "error";
  }


  /**本地用户登录.*/
}

实体PersonInfo

package com.gakki.demo.pojo;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户个人信息.
 */
public class PersonInfo implements Serializable {
  private static final long serialVersionUID = 8367813726677279369L;

  private Long userId; //用户ID
  private String userName; //名称
//  private String profileImg; //头像地址
  private String email; //邮箱
  private String gender; //性别
  private Integer enableStatus; //用户状态
  private Integer userType; //1.顾客  2.店家  3.超级管理员
  private Date createTime; //创建时间
  private Date lastEditTime; //更新时间

  @Override
  public String toString() {
    return "PersonInfo{" +
            "userId=" + userId +
            ", userName='" + userName + '\'' +
            ", email='" + email + '\'' +
            ", gender='" + gender + '\'' +
            ", enableStatus=" + enableStatus +
            ", userType=" + userType +
            ", createTime=" + createTime +
            ", lastEditTime=" + lastEditTime +
            '}';
  }

  public Long getUserId() {
    return userId;
  }

  public void setUserId(Long userId) {
    this.userId = userId;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gender = gender;
  }

  public Integer getEnableStatus() {
    return enableStatus;
  }

  public void setEnableStatus(Integer enableStatus) {
    this.enableStatus = enableStatus;
  }

  public Integer getUserType() {
    return userType;
  }

  public void setUserType(Integer userType) {
    this.userType = userType;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public Date getLastEditTime() {
    return lastEditTime;
  }

  public void setLastEditTime(Date lastEditTime) {
    this.lastEditTime = lastEditTime;
  }

  public PersonInfo() {
  }

  public PersonInfo(Long userId, String userName, String email, String gender, Integer enableStatus, Integer userType, Date createTime) {
    this.userId = userId;
    this.userName = userName;
    this.email = email;
    this.gender = gender;
    this.enableStatus = enableStatus;
    this.userType = userType;
    this.createTime = createTime;
  }

  public PersonInfo(Long userId, String userName, String email, String gender, Integer enableStatus, Integer userType, Date createTime, Date lastEditTime) {
    this.userId = userId;
    this.userName = userName;
    this.email = email;
    this.gender = gender;
    this.enableStatus = enableStatus;
    this.userType = userType;
    this.createTime = createTime;
    this.lastEditTime = lastEditTime;
  }
}

实体LocalAuth

package com.gakki.demo.pojo;

import java.io.Serializable;
import java.util.Date;

/**
 * 本地认证.
 */
public class LocalAuth implements Serializable {
  private static final long serialVersionUID = 5271646818073814565L;

  private Long localAuthId; //本地id
  private String userName; //名称
  private String password; //密码
  private Date createTime; //创建时间
  private Date lastEditTime; //更新时间
  private PersonInfo userId; //用户信息表

  @Override
  public String toString() {
    return "LoaclAuth{" +
            "localAuthId=" + localAuthId +
            ", userName='" + userName + '\'' +
            ", password='" + password + '\'' +
            ", createTime=" + createTime +
            ", lastEditTime=" + lastEditTime +
            ", userId=" + userId +
            '}';
  }

  public LocalAuth() {
  }

  public LocalAuth(Long localAuthId, String userName, String password, Date createTime, Date lastEditTime) {
    this.localAuthId = localAuthId;
    this.userName = userName;
    this.password = password;
    this.createTime = createTime;
    this.lastEditTime = lastEditTime;
  }

  public LocalAuth(Long localAuthId, String userName, String password, Date createTime, Date lastEditTime, PersonInfo personInfo) {
    this.localAuthId = localAuthId;
    this.userName = userName;
    this.password = password;
    this.createTime = createTime;
    this.lastEditTime = lastEditTime;
    this.userId = personInfo;
  }

  public Long getLocalAuthId() {
    return localAuthId;
  }

  public void setLocalAuthId(Long localAuthId) {
    this.localAuthId = localAuthId;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public Date getLastEditTime() {
    return lastEditTime;
  }

  public void setLastEditTime(Date lastEditTime) {
    this.lastEditTime = lastEditTime;
  }

  public PersonInfo getUserId() {
    return userId;
  }

  public void setUserId(PersonInfo userId) {
    this.userId = userId;
  }
}

前端页面 注册1

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/6/12
  Time: 13:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>Gakki网站购物注册</h3>
<hr>
    <form action="/registerUser1" method="post">
        <p>
            用户名称:<input type="text" name="userName">
        </p>
        <p>
            邮箱:<input type="text" name="email">
        </p>
        <p>
            性别: 男<input type="radio" name="gender" value="男" checked="checked">
            女<input type="radio" name="gender" value="女">
        </p>
        <%--用户状态,仅管理员可以更改--%>
        <p>
            <input type="hidden" value="1" name="enableStatus">
        </p>
        <p>
            角色: 顾客<input type="radio" name="userType" value="1" checked="checked">
            店家<input type="radio" name="userType" value="2">
        </p>
        <p>
            <input type="submit" value="继续">
        </p>
    </form>
</body>
</html>

前端页面 注册2

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/6/15
  Time: 19:00
  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 action="/registerUser2" method="post">
        <p>
            登录名:<input type="text" name="userName">
        </p>
        <p>
            密码:<input type="password" name="password">
        </p>
        <p>
            <input type="hidden" name="userId" value="${userId}">
        </p>
        <p>
            <input type="submit" value="注册">
        </p>

    </form>
</body>
</html>

问题:注册一 成功(数据库也插入了数据)后跳转注册2    这里提交表单报错。

o.s.w.s.m.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'localAuth' on field 'userId': rejected value [19]; codes [typeMismatch.localAuth.userId,typeMismatch.userId,typeMismatch.com.gakki.demo.pojo.PersonInfo,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [localAuth.userId,userId]; arguments []; default message [userId]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.gakki.demo.pojo.PersonInfo' for property 'userId'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.gakki.demo.pojo.PersonInfo' for property 'userId': no matching editors or conversion strategy found]]

是什么原因呀?是因为表单中的userId 是String  无法转成 实体类对象属性吗?  该如何解决呢?很急 卡了一上午了。

JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 31944楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 31945楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 31947楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637