当我尝试更新电子商务管理面板中的类别名称时,它没有更新。我的终端中出现以下错误:
org.springframework.dao.InvalidDataAccessApiUsageException:给定的 id 不能为空
我是Spring Boot新手,无法解决这个问题。
我的控制器方法:
@PostMapping("/add-category")
public String add(@ModelAttribute("categoryNew") Category category, RedirectAttributes attributes) {
try {
categoryService.save(category);
attributes.addFlashAttribute("success", "Added successfully");
} catch (DataIntegrityViolationException e) {
e.printStackTrace();
attributes.addFlashAttribute("failed", "Failed to add because duplicate name");
} catch (Exception e) {
e.printStackTrace();
attributes.addFlashAttribute("failed", "Error server");
}
return "redirect:/categories";
}
@GetMapping( "/findById")
@ResponseBody
public Category findById(Long id) {
return categoryService.findById(id);
}
@GetMapping("/update-category")
public String update(Category category, RedirectAttributes redirectAttributes) {
try {
categoryService.update(category);
redirectAttributes.addFlashAttribute("success", "Update successfully!");
} catch (DataIntegrityViolationException e1) {
e1.printStackTrace();
redirectAttributes.addFlashAttribute("error", "Duplicate name of category, please check again!");
} catch (Exception e2) {
e2.printStackTrace();
redirectAttributes.addFlashAttribute("error", "Error from server or duplicate name of category, please check again!");
}
return "redirect:/categories";
}
我的服务实施:
@Override
public Category save(Category category) {
try {
Category categorySave = new Category(category.getName());
return repo.save(categorySave);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
@Override
public Category findById(Long id) {
return repo.findById(id).get();
}
@Override
public Category update(Category category) {
Category updatedCategory = repo.getById(category.getId());
updatedCategory.setName(category.getName());
return updatedCategory;
}
我的 html 与百里香叶:
<table class="table table-bordered" th:if="${size>0}">
<thead>
<tr>
<th scope="col"> Category Index</th>
<th scope="col">Category Name</th>
<th scope="col">Edit Category Name</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr th:each=" category: ${categories}">
<th scope="row" th:text="${category.id}"></th>
<td th:text="${category.name}"></td>
<td><a id="editButton" th:href="@{/findById(id=${category.id})}" class="btn btn-primary" >Update</a></td>
<td>
<a th:if = "${category.is_activated()} == true" th:href="@{/delete-category(id=${category.id})}" class="btn btn-warning">Deleted</a>
<a th:if = "${category.is_activated()} == false" th:href="@{/enable-category(id=${category.id})}" class="btn btn-primary">Enabled</a>
</td>
</tr>
</tbody>
</table>
<!-- End of Main Content -->
<!-- Footer -->
<footer th:replace="~{fragments::footer}"></footer>
<!-- End of Footer -->
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<!-- Scroll to Top Button-->
<a th:replace="~{fragments:: scroll}"></a>
<!-- Logout Modal-->
<!-- Add Modal-->
<!-- MODAL HERE -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<form th:action="@{/add-category}" method="put" th:object="${categoryNew}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New Category</h5>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="recipient-name" class="col-form-label">Name:</label>
<input type="text" class="form-control" th:field="*{name}" id="recipient-name">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</form>
</div>
<!-- End Modal-->
<!-- Change Name category Modal-->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalExample" aria-hidden="true">
<form th:action="@{/update-category}" method="put" th:object="${categoryNew}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editModalExample">Edit Category</h5>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="idEdit" class="col-form-label">Id:</label>
<input type="text" class="form-control" id="idEdit" name="idEdit" readonly>
</div>
<div class="mb-3">
<label for="nameEdit" class="col-form-label">Name:</label>
<input type="text" class="form-control" id="nameEdit" name="nameEdit">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</form>
</div>
我的编辑按钮的js文件:
$(document).ready(function (){
$('table #editButton').on('click', function (event){
event.preventDefault();
var href = $(this).attr('href');
$.get(href, function (category, status){
$('#idEdit').val(category.id);
$('#nameEdit').val(category.name);
});
$('#editModal').modal();
});
});
您可以更改您的href
<a id="editButton" th:href="@{'/findById/' + ${category.id}}" class="btn btn-primary" >Update</a>