Java 数据无法保存到数据库中(Spring MVC+;Thymeleaf)
我想把BranchID保存在数据库中。所有值都已插入数据库,但无法保存。有人能帮忙吗 代码如下: 控制器: 概述:Java 数据无法保存到数据库中(Spring MVC+;Thymeleaf),java,spring,model-view-controller,thymeleaf,Java,Spring,Model View Controller,Thymeleaf,我想把BranchID保存在数据库中。所有值都已插入数据库,但无法保存。有人能帮忙吗 代码如下: 控制器: 概述: 公共类GeneralDto{ private List branchenties=new ArrayList(); private UserEntity UserEntity=new UserEntity(); 公共用户实体getUserEntity(){ 返回用户实体; } public void setUserEntity(UserEntity UserEntity){ thi
公共类GeneralDto{
private List branchenties=new ArrayList();
private UserEntity UserEntity=new UserEntity();
公共用户实体getUserEntity(){
返回用户实体;
}
public void setUserEntity(UserEntity UserEntity){
this.userEntity=userEntity;
}
公共列表GetBranchenties(){
回枝性;
}
公共实体(列出分支机构){
this.branchenties=branchenties;
}
}
主题表格:
用户名
全名
电子邮件
密码
确认密码
分支机构
挑选
分支机构
提交
更新
在最佳实践中,当您在任何类旁边添加@Valid注释时,始终会处理错误。例如if(result.hasErrors()){
//做点什么}
。
问题在于您的私有列表branchenties=new ArrayList()代码>
在这里,您正在添加一个列表,但用户不能位于多个分支中。查看分支和用户表的映射。在最佳实践中,在任何类旁边添加@Valid注释时,始终处理错误。例如if(result.hasrerrors()){
//做点什么}
。
问题在于您的私有列表branchenties=new ArrayList()代码>
在这里,您正在添加一个列表,但用户不能位于多个分支中。查看分支和用户表的映射。它的外观。我认为您最好在GeneralDto
中使用branchenties
而不是branchenties
通过model.addAttribute(“userInit”,branchenties)发送branchenties
代码>
并将其显示给下拉列表。
然后在您的GeneralDto
中按其外观接收您的branchId
。我认为您最好在GeneralDto
中使用branchenties
而不是branchenties
通过model.addAttribute(“userInit”,branchenties)发送branchenties
代码>
并将其显示给下拉列表。
并在您的GeneralDto
中接收您的branchId
@RequestMapping(value="/user/create",method=RequestMethod.GET)
public String showCreatePage(Model model,Principal principal){
//model.addAttribute(new UserEntity());
model.addAttribute("body", "user/user-create-temp");
model.addAttribute("userInit", userService.getGeneralInfo(principal));
return "layouts/default";
}
@RequestMapping(value = "/user/create", method = RequestMethod.POST)
public String showCreateUser(Model model, @Valid GeneralDto userEntity,BindingResult result) {
userService.saveUser(userEntity.getUserEntity());
return "redirect:/user/list";
}
public class GeneralDto {
private List<BranchEntity> branchEntities = new ArrayList<BranchEntity>();
private UserEntity userEntity=new UserEntity();
public UserEntity getUserEntity() {
return userEntity;
}
public void setUserEntity(UserEntity userEntity) {
this.userEntity = userEntity;
}
public List<BranchEntity> getBranchEntities() {
return branchEntities;
}
public void setBranchEntities(List<BranchEntity> branchEntities) {
this.branchEntities = branchEntities;
}
}