@有效的打印信息到控制台,但不到页面
@Controller
public class RoomsController {
private final HotelService hotelService;
private final ComfortService comfortService;
private final RoomService roomService;
@Autowired
public RoomsController(HotelService hotelService, ComfortService comfortService, RoomService roomService) {
this.hotelService = hotelService;
this.comfortService = comfortService;
this.roomService = roomService;
}
@GetMapping("/admin/deleteRoom")
String showDeleteRoomPage(@ModelAttribute("formData") DeleteRoomFormData formData){
return "deleteRoom";
}
@PostMapping("/admin/deleteRoom")
String doDeleteRoom(@Valid @ModelAttribute("formData")DeleteRoomFormData formData, Model model, BindingResult bindingResult){
if (bindingResult.hasErrors()) {
return "deleteRoom";
}
roomService.deleteRoomByNumberAndHotelName(formData.toParameters());
return "redirect:/";
}
}
public class DeleteRoomFormData {
@NotNull
@Range(min = 1, max = 300)
private int number;
@NotNull
@Size(min = 4, max = 400, message = "Minimum number of characters = 4" )
private String hotelName;
public RoomDeleteParameters toParameters(){
return new RoomDeleteParameters(number,hotelName);
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getHotelName() {
return hotelName;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
}
package ua.forms;
import ua.models.Hotel;
public class RoomDeleteParameters {
private final int number;
private final String hotelName;
public RoomDeleteParameters(int number, String hotelName) {
this.number = number;
this.hotelName = hotelName;
}
public int getNumber() {
return number;
}
public String getHotelName() {
return hotelName;
}
}
<div class="card-body">
<form class="form-horizontal" data-th-action="@{/admin/deleteRoom}" data-th-object="${formData}" method="post">
<div class="errors">
<div th:utext="${errorMessage}" data-th-if="${error}" class="alert alert-danger" role="alert">
</div>
</div>
<div class="form-group">
<label for="hotelName" class="cols-sm-2 control-label">Hotel Name</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="hotelName" id="hotelName" placeholder="Enter hotel name" data-th-field="*{hotelName}"/>
</div>
<p style="color:red" th:if="${#fields.hasErrors('hotelName')}" th:text="${#strings.listJoin(#fields.errors('hotelName'), ', ')}"></p>
</div>
</div>
<div class="form-group">
<label for="number" class="cols-sm-2 control-label">Hotel Name</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="number" id="number" placeholder="Enter number" data-th-field="*{number}"/>
</div>
<p style="color:red" th:if="${#fields.hasErrors('number')}" th:text="${#strings.listJoin(#fields.errors('number'), ', ')}"></p>
</div>
</div>
<div class="form-group ">
<input type="submit" value="Delete">
</div>
</form>
</div>
当我的字段有错误时@有效的打印信息到控制台,但为什么不在页面上?
字段"hotelName"上的对象"formData"中出现字段错误:拒绝的值[];codes[Size.formData.hotelName,Size.hotelName,Sizes.java.lang.String,Size];arguments[org.springframework.context.support.DefaultMessageSourceResolvable:codes[formData.hotelName,hotelName];论点[];默认消息[hotelName],400,4];默认消息[最小字符数=4]字段"number"上的对象"formData"中存在字段错误:拒绝的值[0];codes[Range.formData.number,Range.number、Range.int、Range];arguments[org.springframework.context.support.DefaultMessageSourceResolvable:codes[formData.number,number];论点[];默认消息[编号],300,1];默认消息
BindingResult
参数需要跟随@ModelAttribute
参数才能工作。因此,将您的@PostMapping
更改为:
@PostMapping("/admin/deleteRoom")
String doDeleteRoom(@Valid @ModelAttribute("formData")DeleteRoomFormData formData, BindingResult bindingResult, Model model){
if (bindingResult.hasErrors()) {
return "deleteRoom";
}
roomService.deleteRoomByNumberAndHotelName(formData.toParameters());
return "redirect:/";
}