我是Spring MVC的新手,试图构建一个示例应用程序,我试图将数据保存到一个名为EmployeeDetails的表中,该表与表用户有一对一的关系。提交表单后,我在字段“user”的对象“employee details”中得到了这个错误字段:rejected value[Users];代码[typemismatch.employeeDetails.user,typemismatch.user,typemismatch.com.sachinmukherjee.spring_hibernate.entity.users,typemismatch];参数[org.springframework.context.support.defaultmessageSourceResolvable:代码[employeeDetails.user,user];参数[];default message[user]];默认消息[无法将类型“java.lang.String”的属性值转换为属性“user”的必需类型“com.sachinmukherjee.spring_hibernate.entity.users”;嵌套异常为java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为属性“user”的必需类型“com.sachinmukherjee.spring_hibernate.entity.users”:找不到匹配的编辑器或转换策略]]
用户表结构
+--------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(20) | NO | UNI | NULL | |
| password | varchar(100) | NO | | NULL | |
| created_at | datetime | YES | | CURRENT_TIMESTAMP | |
| userfullname | varchar(50) | NO | | NULL | |
+--------------+--------------+------+-----+-------------------+----------------+
Users实体(Users.java)
@Entity
@Table(name="users")
public class Users {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name="username",unique = true)
private String username;
@Column(name="password")
private String password;
@Column(name="userfullname")
private String userfullname;
@Column(name = "created_at")
private Timestamp created_at;
@OneToOne(mappedBy = "user",
cascade = CascadeType.PERSIST
)
private EmployeeDetails employee;
public Users() {
}
public Users(String username, String password, String userfullname) {
this.username = username;
this.password = password;
this.userfullname = userfullname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Timestamp getCreated_at() {
return created_at;
}
public String getUserfullname() {
return userfullname;
}
public void setUserfullname(String userfullname) {
this.userfullname = userfullname;
}
public void setCreated_at(Timestamp created_at) {
this.created_at = created_at;
}
public int getId() {
return id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setId(int id) {
this.id = id;
}
public EmployeeDetails getEmployee() {
return employee;
}
public void setEmployee(EmployeeDetails employee) {
this.employee = employee;
}
@Override
public String toString() {
return "Users [id=" + id + ", username=" + username + ", userfullname=" + userfullname + ", created_at="
+ created_at + "]";
}
}
EmployeeDetails表结构
+-------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| first_name | varchar(20) | NO | | NULL | |
| middle_name | varchar(20) | YES | | NULL | |
| last_name | varchar(20) | YES | | NULL | |
| mobile_no | bigint(20) | YES | | NULL | |
| email | varchar(50) | NO | | NULL | |
| created_at | datetime | YES | | CURRENT_TIMESTAMP | |
| users_id | int(11) | NO | MUL | NULL | |
+-------------+-------------+------+-----+-------------------+----------------+
@Entity
@Table(name="employee_details")
public class EmployeeDetails {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name="first_name")
private String first_name;
@Column(name="middle_name")
private String middle_name;
@Column(name="last_name")
private String last_name;
@Column(name="mobile_no")
private long mobile_no;
@Column(name="email")
private String email;
@OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
@JoinColumn(name = "users_id")
private Users user;
public EmployeeDetails() {
}
public EmployeeDetails(String first_name, String middle_name, String last_name, long mobile_no, String email) {
this.first_name = first_name;
this.middle_name = middle_name;
this.last_name = last_name;
this.mobile_no = mobile_no;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getMiddle_name() {
return middle_name;
}
public void setMiddle_name(String middle_name) {
this.middle_name = middle_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public long getMobile_no() {
return mobile_no;
}
public void setMobile_no(long mobile_no) {
this.mobile_no = mobile_no;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
@Override
public String toString() {
return "EmployeeDetails [id=" + id + ", first_name=" + first_name + ", middle_name=" + middle_name
+ ", last_name=" + last_name + ", mobile_no=" + mobile_no + ", email=" + email + ", user=" + user + "]";
}
}
@Controller
@RequestMapping("/employee_details")
public class EmployeeDetailsController {
@Autowired
EmployeeDetailsDAO employeeDetailsDAO;
@Autowired
UsersDAO usersDAO;
@RequestMapping("/")
public String home(Model model) {
List<EmployeeDetails> employees = employeeDetailsDAO.getEmployees();
model.addAttribute("employees", employees);
return "employee_details/index";
}
@GetMapping("/add")
public String add(Model model) {
List<Users> users = usersDAO.getUsers();
EmployeeDetails employee = new EmployeeDetails();
model.addAttribute("userList",users);
model.addAttribute("employee", employee);
return "employee_details/add";
}
@PostMapping("/submit")
public String submit(@ModelAttribute EmployeeDetails employee) {
System.out.println(employee);
employeeDetailsDAO.saveEmployee(employee);
return "redirect:/employee_details/";
}
}
@Repository
public class EmployeeDetailsDAOImp implements EmployeeDetailsDAO {
@Autowired
SessionFactory sessionFactory;
@Transactional
public List<EmployeeDetails> getEmployees() {
Session session = sessionFactory.getCurrentSession();
Query<EmployeeDetails> query = session.createQuery("from EmployeeDetails",EmployeeDetails.class);
List<EmployeeDetails> employees = query.getResultList();
System.out.println(employees);
return employees;
}
@Transactional
public void saveEmployee(EmployeeDetails employee) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(employee);
}
}
<form action="${pageContext.request.contextPath}/employee_details/submit" method="POST" modelAttribute="employee" class="form-control">
<label>Username</label>
<select name="user" required="required">
<option>Select User</option>
<c:forEach var="user" items="${userList}">
<option value=${user}>${user.username}</option>
</c:forEach>
</select>
<br><br>
<label>First Name</label>
<input type="text" name="first_name" />
<br><br>
<label>Middle Name</label>
<input type="text" name="middle_name" />
<br><br>
<label>Last Name</label>
<input type="text" name="last_name" />
<br><br>
<label>Email</label>
<input type="text" name="email" />
<br><br>
<button type="submit" class="btn btn-success">Save</button>
</form>
@Repository
public class UsersDAOImp implements UsersDAO {
//need to inject session factory
@Autowired
private SessionFactory sessionFactory;
@Transactional
public List<Users> getUsers() {
//get the hibernate session
Session session = sessionFactory.getCurrentSession();
//create a query
Query<Users> theQuery =
session.createQuery("from Users", Users.class);
//execute the query and get resultset
List<Users> users = theQuery.getResultList();
//return the result
return users;
}
}
我不明白为什么会出现这个错误,以及应该在submit方法的method参数中传递什么。
我发现的解决方案是将HTML表单替换为spring表单,并将select标记及其路径更改为如下所示。
<form:label path="user.id">Username</form:label>
<form:select path="user.id" required="required">
<form:option value="${user.get(0)}">Select One</form:option>
<c:forEach var="user" items="${userList}">
<form:option value="${user.id}">${user.username}</form:option>
</c:forEach>
</form:select>
我有两个实体,它们使用一个主键互相引用,主键是一个实体的整数。我不确定我做这件事的方式是否正确。 下面是引用主键id为int的实体 下面是我们从上面的实体中将外键设置为Kmichango kandaMchango的实体。 这里是表单的一部分,我在这里提交了用户在jumuiya_michango_form.html中提供的数据 下面是我的控制器中用于链接到表单和发布数据的两个方法 在我提交表单后,我
我正在关注Spring in Action 5,在按下提交按钮后创建Taco模型时遇到问题。这是我的设计Taco控制器类: 以及我捕获的错误消息: 炸玉米饼实体如下所示: 以及我的配料实体: 这是一个html页面,必须使用所选成分创建新的Taco对象: 我该怎么修理它?谢谢你的预付款。
我是SpringMVC的新手。我开发了一个执行选择,插入,更新和删除的示例应用程序。 下面是我的Bean类 下面是我的控制器类 下面是我的JSP页面 现在我面临两个问题。输入值并单击“添加学生”按钮后,收到以下错误。
这是控制器代码部分: 我收到这条消息: 出现错误(类型=错误请求,状态=400)。无法将类型[java.lang.String]的值转换为所需类型[java.util.Date];嵌套异常为org.springframework.core.convert.conversionfailedexception:无法将值“Wed Jun 08 00:00:00 WET 2016”从类型[java.lan
我想在json中以的形式发布上面的字符串日期,但我收到了400个错误请求。有人能帮忙吗。我使用过,但它也没有帮助我。
我正在使用Thymeleaf和SpringBoot构建一个web应用程序,我对这两种技术都是新手。 在我的html文件中,有一个日期字段,如下所示: 我的模型类有一个与issueDate对应的字段,如下所示: 当我从UI输入日期时,我在浏览器中看到以下异常: 根据我的经验,我理解UI将属性读取为字符串,但模型期望出现错误的类型日期。所以我需要将字符串转换为日期。然而,这应该在哪里进行?因为错误甚至