我想在数据库中保存BranchID。所有值都插入到数据库中,但无法保存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;
}
}
ThemeLeaf表格:
<form class="form-horizontal" th:action="@{/user/create}"
method="post" th:object="${userInit}" onsubmit="return validatePassword()">
<div class="box-body">
<div class="form-group form-group-code">
<label for="code" class="col-sm-2 control-label">User Name</label>
<div class="col-sm-10">
<input type="hidden" th:field="*{userEntity.userId}"></input>
<input type="text" th:field="*{userEntity.username}" class="form-control"
placeholder="Username"></input>
<div th:if="${#fields.hasErrors('userEntity.username')}">
<label th:errors="*{userEntity.username}" style="color: red"></label>
</div>
</div>
</div>
<div class="form-group">
<label for="fullName" class="col-sm-2 control-label">Full name</label>
<div class="col-sm-10">
<input type="text" th:field="*{userEntity.fullName}" class="form-control"
placeholder="Fullname"></input>
<div th:if="${#fields.hasErrors('userEntity.fullName')}">
<label th:errors="*{userEntity.fullName}"></label>
</div>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" th:field="*{userEntity.email}" class="form-control" placeholder="Email"></input>
<div th:if="${#fields.hasErrors('userEntity.email')}">
<label th:errors="*{userEntity.email}"></label>
</div>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" th:field="*{userEntity.password}" th:id="password" class="form-control"
placeholder="Password"></input>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Confirm Password</label>
<div class="col-sm-10">
<input type="password" class="form-control"
placeholder="Confirm Password" id="confirm_password"></input>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Branch </label>
<div class="col-sm-4">
<select class="form-control"
th:field="*{branchEntities}">
<option selected="selected" value="">Select
Branch</option>
<option th:each="branch : *{branchEntities}"
th:value="${branch.branchId}"
th:text="${branch.branchName}"></option>
</select>
</div>
</div>
<!-- <div class="form-group">
<label class="col-sm-2 control-label">Status</label>
<div class="col-sm-10">
<label>Active
<input type="radio" th:field="*{enabled}" value="" name="enabled" class="flat-red" checked="checked"></input>
</label>
<label>Inactive
<input type="radio" th:field="*{enabled}" th:value="${userEntity.userId}" name="enabled" class="flat-red"></input>
</label>
</div>
</div> -->
<div></div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button th:if="${userInit.userEntity.userId == null}"
type="submit" class="btn btn-info pull-left">Submit</button>
<button th:if="${userInit.userEntity.userId != null}"
type="submit" class="btn btn-info pull-left">Update</button>
<a th:href="@{/users}" type="submit"
class="btn btn-danger pull-right">Cancel</a>
</div>
<!-- /.box-footer -->
</form>
答案 0 :(得分:0)
在最佳实践中,当您在任何类旁边添加@Valid注释时,始终会处理errors.like if(result.hasErrors()){
//do someething}
。
问题发生在private List<BranchEntity> branchEntities = new ArrayList<BranchEntity>();
在这里,您要添加一个列表,但用户不能在许多分支中。查看分支和用户表的映射。
答案 1 :(得分:0)
它的样子。我认为最好在branchId
中使用GeneralDto
而不是branchEntities
通过branchEntities
发送model.addAttribute("userInit",branchEntities);
并显示给你的下拉菜单。
并在branchId
中收到GeneralDto
。