会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132808个问题

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 31951楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 31952楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 31954楼
Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/决策树算法 31958楼
Python 全系列/第一阶段:Python入门/控制语句 31959楼

一、问题

        这一集课后作业做完了,想让老师给检查一下,看看有没有问题,是不是达到了这次课后作业的要求

作业要求如图:

image.png


二、作业源码

package com.bjsxt.array;

/**
 * 完成功能:
 * 1. 定义一个类表示上表中的商品。
 * 2. 创建 5 个对象存储上面表的信息。
 * 3. 创建一个数组,存储这 5 个对象。
 * 4. 再创建一个方法,遍历(toString())这个数组,打印表的信息。
 * 5. 创建一个方法:查询最终购买价,大于指定数字的所有商品。
 *
 */
public class Test05zuoye {

    public void print(double m){
        Info info0 = new Info(1,"百战牌鼠标", "BZ_001", 99.21 ,0.9);
        Info info1 = new Info(2,"键盘侠玩偶", "WO_102", 403.00 ,0.7);
        Info info2 = new Info(3,"实战尚学堂", "java", 89.00 ,0.8);
        Info info3 = new Info(4,"高淇牌西装", "GQ_XF_12", 700.00 ,0.5);
        Info info4 = new Info(5,"大米牌手机", "DM_PH_13", 900.00 ,0.3);

        Info[] infos = new Info[5];
        infos[0] = info0;
        infos[1] = info1;
        infos[2] = info2;
        infos[3] = info3;
        infos[4] = info4;

        for (int i = 0;i<infos.length;i++){
            System.out.println(infos[i]);
            for (int j = 0 ;j<infos.length;j++){
                System.out.println("遍历所有:"+infos[j].getName()+"   "+infos[j].getPrice()+"   "+infos[j].getCheap());
                if((infos[j].getPrice())*(infos[j].getCheap())>m){
                    System.out.println("打折后的价格:"+(infos[j].getPrice())*(infos[j].getCheap()));
                    System.out.println("输出结果:"+infos[j].getName());
                    System.out.println("===================================");
                }else{
                    System.out.println("最终购买价小于指定数字,抱歉!");
                }
            }
        }
    }


    public static void main(String[] args) {
        Test05zuoye test05zuoye = new Test05zuoye();
        test05zuoye.print(100);

    }

}
class Info{
    private int id;
    private String name;
    private String type;
    private double price;
    private double cheap;

    @Override
    public String toString() {
        return
                "id=" + id +'\t'+
                "name='" + name + '\t' +
                "type='" + type + '\t' +
                "price=" + price +'\t'+
                "cheap=" + cheap +'\t'
                ;
    }

    public Info(){

    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getCheap() {
        return cheap;
    }

    public void setCheap(double cheap) {
        this.cheap = cheap;
    }

    public Info(int id, String name, String type, double price, double cheap) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.price = price;
        this.cheap = cheap;
    }
}


三、运行结果

GIF3.gif

JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 31960楼

image.png


admin_study.zip


image.png


老师,这个访问http://127.0.0.1:8000/admin/怎么没用

Python 全系列/第十二阶段:Python_Django3框架/Django初级 31961楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask高级 31962楼
JAVA 全系列/第十一阶段:消息中间件与高并发处理/RabbitMQ(旧) 31965楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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