<a th:href="@{findOne/(id=${tour.id})}" class="btn btn-primary eBtn"
sec:authorize="hasAuthority('ADMIN')">Редактировать тур</a>
$(document).ready(function () {
$('.nBtn, .card-deck .eBtn').on('click', function (event) {
event.preventDefault();
var href = $(this).attr('href');
var text = $(this).text();
if (text=='Редактировать тур') {
$.get(href, function (tour, status) {
$('.myForm #id').val(tour.id);
$('.myForm #title').val(tour.title);
$('.myForm #description').val(tour.description);
$('.myForm #startTime').val(tour.startTime);
$('.myForm #endTime').val(tour.endTime);
$('.myForm #price').val(tour.price);
});
$('.myForm #exampleModal').modal();
} else {
$('.myForm #id').val('');
$('.myForm #title').val('');
$('.myForm #description').val('');
$('.myForm #startTime').val('');
$('.myForm #endTime').val('');
$('.myForm #price').val('');
$('.myForm #exampleModal').modal();
}
});
});
@Controller
public class TourController {
@Autowired
private TourRepository tourRepository;
@PostMapping("/save")
public String save(@ModelAttribute(name = "tour") Tour tour) {
tourRepository.save(tour);
return "redirect:/";
}
@PostMapping("/admin/saveTour")
public String saveTourInModal(@ModelAttribute(name = "tour") Tour tour) {
tourRepository.save(tour);
return "redirect:/admin/tourList";
}
@GetMapping("/")
public String mainPageShowTours(Model model) {
model.addAttribute("tours",tourRepository.findAll());
return "home/main";
}
@GetMapping("/findOne")
@ResponseBody
public Tour findOne(Integer id) {
return tourRepository.findById(id).get();
}
}
<a th:href="@{findOne/(id=${tour.id})}" class="btn btn-primary qBtn">Edit</a>
уже на другое идентичное окно, только с др. id <div class="myForm3" th:fragment="modalForChangeTour">
<form th:action="@{/admin/saveTour}" method="post">
<div class="modal fade" id="exampleModal3" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel3"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel3">Update tour or Create3</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="id3" class="col-form-label">ID:</label>
<input type="text" class="form-control" id="id3" name="id3" value=""/>
</div>
<div class="form-group">
<label for="title3" class="col-form-label">Title:</label>
<input type="text" class="form-control" id="title3" name="title3" value=""/>
</div>
<div class="form-group">
<label for="description3" class="col-form-label">description:</label>
<input type="text" class="form-control" id="description3" name="description3" value=""/>
</div>
<div class="form-group">
<label for="startTime3" class="col-form-label">startTime:</label>
<input type="text" class="form-control" id="startTime3" name="startTime3" value=""/>
</div>
<div class="form-group">
<label for="endTime3" class="col-form-label">endTime:</label>
<input type="text" class="form-control" id="endTime3" name="endTime3" value=""/>
</div>
<div class="form-group">
<label for="price3" class="col-form-label">price:</label>
<input type="text" class="form-control" id="price3" name="price3" 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" value="Save or Create"/>
</div>
</div>
</div>
</div>
</form>
</div>
$(document).ready(function () {
$('table .qBtn').on('click', function (event) {
event.preventDefault();
var href = $(this).attr('href');
var text = $(this).text();
if (text=='Edit') {
$.get(href, function (tour, status) {
$('.myForm3 #id3').val(tour.id);
$('.myForm3 #title3').val(tour.title);
$('.myForm3 #description3').val(tour.description);
$('.myForm3 #startTime3').val(tour.startTime);
$('.myForm3 #endTime3').val(tour.endTime);
$('.myForm3 #price3').val(tour.price);
});
$('.myForm3 #exampleModal3').modal();
} else {
$('.myForm3 #id3').val('');
$('.myForm3 #title3').val('');
$('.myForm3 #description3').val('');
$('.myForm3 #startTime3').val('');
$('.myForm3 #endTime3').val('');
$('.myForm3 #price3').val('');
$('.myForm3 #exampleModal3').modal();
}
});
});
Hibernate: select tour0_.code as code1_3_0_, tour0_.description as descript2_3_0_, tour0_.end_time as end_time3_3_0_, tour0_.image as image4_3_0_, tour0_.price as price5_3_0_, tour0_.start_time as start_ti6_3_0_, tour0_.title as title7_3_0_ from tour tour0_ where tour0_.code=?
Hibernate: select tour0_.code as code1_3_0_, tour0_.description as descript2_3_0_, tour0_.end_time as end_time3_3_0_, tour0_.image as image4_3_0_, tour0_.price as price5_3_0_, tour0_.start_time as start_ti6_3_0_, tour0_.title as title7_3_0_ from tour tour0_ where tour0_.code=?
Hibernate: update tour set description=?, end_time=?, image=?, price=?, start_time=?, title=? where code=?
Hibernate: select tour0_.code as code1_3_, tour0_.description as descript2_3_, tour0_.end_time as end_time3_3_, tour0_.image as image4_3_, tour0_.price as price5_3_, tour0_.start_time as start_ti6_3_, tour0_.title as title7_3_ from tour tour0_
Hibernate: select tour0_.code as code1_3_, tour0_.description as descript2_3_, tour0_.end_time as end_time3_3_, tour0_.image as image4_3_, tour0_.price as price5_3_, tour0_.start_time as start_ti6_3_, tour0_.title as title7_3_ from tour tour0_
Hibernate: select tour0_.code as code1_3_0_, tour0_.description as descript2_3_0_, tour0_.end_time as end_time3_3_0_, tour0_.image as image4_3_0_, tour0_.price as price5_3_0_, tour0_.start_time as start_ti6_3_0_, tour0_.title as title7_3_0_ from tour tour0_ where tour0_.code=?
Hibernate: insert into tour (description, end_time, image, price, start_time, title) values (?, ?, ?, ?, ?, ?)
Hibernate: select tour0_.code as code1_3_, tour0_.description as descript2_3_, tour0_.end_time as end_time3_3_, tour0_.image as image4_3_, tour0_.price as price5_3_, tour0_.start_time as start_ti6_3_, tour0_.title as title7_3_ from tour tour0_
th:href="@{findOne/(id=${tour.id})}"
Converter<S, T>
или же либу MapStruct, ModelMapper и др.BeanUtils.copyProperties()
например, для обновления сущности.@ResponseBody
public Tour findOne(Integer id) {
return tourRepository.findById(id).get();
}
get()
лучше использовать например, orElseThrow()
и выбросить исключение, которое впоследствиее можно при помощи ExceptionHandler обработать и вернуть корректный http ответ. Например, когда объект не найден (404) @Autowired
private TourRepository tourRepository;
@PostMapping("/save")
public String save(@ModelAttribute(name = "tour") Tour tour) {
tourRepository.save(tour);
return "redirect:/";
}
@PostMapping("/admin/saveTour")
public String saveTourInModal(@ModelAttribute(name = "tour") Tour tour) {
tourRepository.save(tour);
return "redirect:/admin/tourList";
}
Само модальное окно вызывается и заполняется данными объекта. После нажатия на кнопку сохранения, у меня стало выводить исключение org.hibernate.exception.ConstraintViolationException: could not execute statement, и что поля бд title и description не могут быть null.
Hibernate: update tour set description=?, end_time=?, image=?, price=?, start_time=?, title=? where code=?
Hibernate: insert into tour (description, end_time, image, price, start_time, title) values (?, ?, ?, ?, ?, ?)