我正在用springboot和thymeleaf做一个简单的任务,但是当我尝试使用save函数进行更新或创建时,我丢失了对象的数据。我单击“新建”按钮,然后更新,并用带电的对象正确打开了数据的模态,但单击“保存”后,对象丢失了。
我相信我的问题在于表格,我不知道这是什么。我已经尝试过邮递员,但是单击“保存或更新”时没有收到请求
索引:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script th:src="@{js/main.js}" src="../static/js/main.js"></script>
</body>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Paginación con SpringBoot</h1>
</div>
<button class="btn btn-success nBtn">New</button>
<div class="card">
<div class="card-block">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>COUNTRY</th>
<th>CAPITAL</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<tr th:each="country :${data.content}">
<td th:text="${country.id}"></td>
<td th:text="${country.name}"></td>
<td th:text="${country.capital}"></td>
<td>
<a th:href="@{/delete/(id=${country.id})}" class="btn btn-danger dBtn">Delete</a>
<a th:href="@{/findOne/(id=${country.id})}" class="btn btn-primary eBtn">Edit</a>
</td>
</tr>
</tbody>
</table>
<hr>
</div>
<div>
<ul class="nav nav-pills nav-justified">
<li class="nav-item" th:each="i :${#numbers.sequence(0,data.totalPages-1)}">
<a th:href="@{/(page=${i})}" th:text="${i}" class="nav-link" th:classappend="${currentPage}==${i}?'active':''"></a>
</li>
</ul>
</div>
</div>
<div class="myForm">
<form th:action="@{/save}" th:object="${country}" method="post">
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Update or Create</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="id" class="col-form-label">Id:</label>
<input type="text" class="form-control" id="id" name="id" th:value="*{''}" disabled/>
</div>
<div class="form-group">
<label for="name" class="col-form-label">Name:</label>
<input type="text" class="form-control" id="name" name="name" th:value="*{''}"/>
</div>
<div class="form-group">
<label for="capital" class="col-form-label">Capital:</label>
<input type="text" class="form-control" id="capital" name="capital" th:value="*{''}"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" data-dismiss="modal" value="Save"/>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p class="alert alert-danger">Are you sure you want to delete this?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<a href="" class="btn btn-danger" id="delRef">Delete</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
控制器:
@Controller
public class CountryController {
@Autowired
private CountryRepository countryRepository;
@SuppressWarnings("deprecation")
@GetMapping("/")
public String showPage(Model model, @RequestParam(defaultValue="0") int page) {
model.addAttribute("data", countryRepository.findAll(new PageRequest(page,4)));
model.addAttribute("currentPage", page);
return "index";
}
@PostMapping("/save")
public String save(Country country) {
countryRepository.save(country);
return "redirect:/";
}
@GetMapping("/delete")
public String deleteCountry(Integer id) {
countryRepository.deleteById(id);;
return "redirect:/";
}
@GetMapping("/findOne")
@ResponseBody
public Optional<Country> FindOne(Integer id) {
return countryRepository.findById(id);
}
}
js:
$(document).ready(function(){
$('.nBtn, .table .eBtn').on('click', function(event){
event.preventDefault();
var href = $(this).attr('href');
var text = $(this).text();
if(text=='Edit'){
$.get(href,function(country,status){
$('.myForm #id').val(country.id);
$('.myForm #name').val(country.name);
$('.myForm #capital').val(country.capital);
});
$('.myForm #exampleModal').modal();
}else{
$('.myForm #id').val('');
$('.myForm #name').val('');
$('.myForm #capital').val('');
$('.myForm #exampleModal').modal();
}
});
$('.table .dBtn').on('click', function(event){
event.preventDefault();
var href = $(this).attr('href');
$('#myModal #delRef').attr('href',href);
$('#myModal').modal();
});});
班级:
@Entity
public class Country {
@Id
@GeneratedValue
private Integer id;
private String name;
private String capital;
public Country(String name, String capital) {
this.name = name;
this.capital = capital;
}
public Country() {
}
@Override
public String toString() {
return "Country [id=" + id + ", name=" + name + ", capital=" + capital + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
}
存储库:
public interface CountryRepository extends JpaRepository<Country,Integer>{
}
答案 0 :(得分:1)
在保存方法上可能缺少@RequestBody注释
@PostMapping("/save")
public String save(@RequestBody Country country) {
countryRepository.save(country);
return "redirect:/";
}}
答案 1 :(得分:1)
顺便说一句,您不应从控制器访问存储库,因为没有事务管理。
答案 2 :(得分:1)
使用@RequestBody
和repository.saveAndFlush(entity)
通过使用@RequestBody
批注,您的值将与您在系统中创建的用于处理任何特定呼叫的模型进行映射
在saveAndFlush
上,此命令中的更改将刷新到DB immediately
。使用save时,不一定是正确的,而是可能一直保留在内存中,直到发出flush或commit命令为止。
@PostMapping("/save")
public String save(@RequestBody Country country) {
countryRepository.saveAndFlush(country);
return "redirect:/";
}
答案 3 :(得分:1)
@mantamusica,如果您仍然有兴趣,我会为您提供解决方案。
在这种情况下,请添加注释
@RequestBody
返回错误代码415。
此问题的解决方案在于HTML端,或者说是Bootstrap。
只需删除
data-dismiss="modal"
来自输入
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" data-dismiss="modal" value="save"/>
</div>
...它对我有用:)