gpt4 book ai didi

java - 数据无法保存到数据库(Spring MVC+T​​hymeleaf)

转载 作者:太空宇宙 更新时间:2023-11-04 11:53:38 25 4
gpt4 key购买 nike

我想将 BranchID 保存在数据库中。所有值都已插入数据库,但无法保存branchId。有人可以帮忙吗?

这是代码:

Controller :

@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";
}

常规Dto:

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;
}

}

主题叶形式:

<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>

最佳答案

它看起来的样子。我认为您最好在 GeneralDto 中使用 branchId 而不是 branchEntities 通过 model.addAttribute("userInit",branchEntities); 发送 branchEntities并将其显示在您的下拉列表中。并在您的 GeneralDto 中接收您的 branchId

关于java - 数据无法保存到数据库(Spring MVC+T​​hymeleaf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41561378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com