提交新增用户信息如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+
request.getServerName()+":"+
request.getServerPort()+ path +"/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增用户</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/add.do" method="post">
<center>
<label>账号:</label>
<input type="text" id="txtUsername" name="userName" placeholder="请输入账号" required/><br/>
<label>密码:</label>
<input type="password" id="txtPassword" name="password" placeholder="请输入密码" required/><br/>
<label>性别:</label>
<input type="radio" name="gender" value="1" checked>女</input><br/>
<input type="radio" name="gender" value="2">男</input><br/>
<label>出生日期:</label>
<input type="date" name="birthday" min="1900-01-01"><br/>
<input type="text" name="birthday" value="1900-01-01"><br/>
<label>用户角色:</label>
<select name="userRole">
<option value="">--请选择--</option>
<option value="1" selected>系统管理员</option>
<option value="2">经理</option>
<option value="3">普通员工</option>
</select>
<input type="submit" value="注册" />
<input type="reset" value="重置" />
</center>
</form>
</body>
</html>
controller代码如下
@RequestMapping("/add.do")
public String add(User user){
System.out.println("要插入的用户信息:");
System.out.println(user.getUserName());
System.out.println(user.getPassword());
System.out.println(user.getGender());
System.out.println(user.getBirthday());
System.out.println(user.getUserRole());
user.setCreateDate(new Date());
user.setCreateBy(1);
user.setModifyDate(new Date());
user.setModifyBy(1);
boolean save = userServiceByMybatisPlus.save(user);
if (save){
return "redirect:/user/list.do";
}else {
return "404";
}
}
运行后,单击增加按钮后,出现
经过百度,分析可能是提交数据不满足要求。后来发现是birthday数据的问题。无论使用下面哪种类型,均出现上面的错误。
<input type="date" name="birthday" min="1900-01-01"><br/>
<input type="text" name="birthday" value="1900-01-01"><br/>
最后发现就是在User.java中birthday属性前增加类型转换注解即可解决问题
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;