【发布时间】:2022-01-22 08:07:09
【问题描述】:
我正在尝试使用发布请求提交表单并首先验证输入。
但是,当我输入错误(例如全部为空)而不是显示错误时,我收到错误请求 (400)。
为了显示错误,我在 HTML 中使用了 th:if 和 th:errors 标签。
如果我提交所有有效输入,则没有问题。
控制器类:
@Controller
@RequestMapping(path = "/order")
public class PurchaseController
{
@GetMapping(path = "/new")
public String newOrder(Model model)
{
model.addAttribute("Purchase", new Purchase());
return "neworder";
}
@PostMapping(path = "/new")
public String createPurchase(@Valid @ModelAttribute(name = "Purchase") Purchase purchase)
{
int purchaseId = 0;
try
{
purchaseId = PurchaseManager.insertPurchase(purchase);
}
catch (SQLException e)
{
return "purchaseerror";
}
if (purchaseId == 0)
return "purchaseerror";
return "redirect:/order/success?id=" + purchaseId;
}
@GetMapping(path = "/success")
public String successPurchase(@RequestParam(required = true, name = "id") int id, Model model)
{
model.addAttribute("id", id);
return "ordersuccess";
}
}
HTML 表单:
<form th:action="@{new}" th:object="${Purchase}" method="post">
<table>
<tr>
<td>First name:</td>
<td><input type="text" th:field="*{firstName}" /></td>
<td th:if="${#fields.hasErrors('firstName')}"
th:errors="*{firstName}">Must be filled</td>
<td>Last name:</td>
<td><input type="text" th:field="*{lastName}" /></td>
<td th:if="${#fields.hasErrors('lastName')}"
th:errors="*{lastName}">Must be filled</td>
</tr>
<tr>
<td>Adresa:</td>
<td><input type="text" th:field="*{address}" /></td>
<td th:if="${#fields.hasErrors('address')}" th:errors="*{address}">Must be filled</td>
</tr>
<tr>
<td>ico:</td>
<td><input type="text" th:field="*{ico}" /></td>
<td th:if="${#fields.hasErrors('ico')}" th:errors="*{ico}">Must be filled</td>
<td>dic:</td>
<td><input type="text" th:field="*{dic}" /></td>
<td th:if="${#fields.hasErrors('dic')}" th:errors="*{dic}">Must be filled</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" th:field="*{email}" /></td>
<td th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Must be filled</td>
<td>phone:</td>
<td><input type="text" th:field="*{phone}" /></td>
<td th:if="${#fields.hasErrors('phone')}" th:errors="*{phone}">Must be filled</td>
</tr>
</table>
<input type="submit" value="Submit"/>
</form>
模型类(购买)
public class Purchase
{
private int id;
@NotBlank
@Size(max = 50)
private String firstName;
@NotBlank
@Size(max = 50)
private String lastName;
@NotBlank
@Size(max = 50)
private String ico;
@NotBlank
@Size(max = 50)
private String dic;
@NotBlank
@Size(max = 400)
private String address;
@NotBlank
@Size(max = 50)
private String email;
@NotBlank
@Size(max = 50)
private String phone;
private LocalDateTime creationDate;
... getters and setters, constructors
如何使用 thymeleaf 显示错误?
编辑: 我设法通过将 BindingResult 参数添加到 Controller 类中的 post 方法并检查是否有任何错误来使其工作。如果是,我返回表单所在的同一页面(/new 映射),即“neworder”。
return "purchaseerror"; 可能会造成一些混乱。
@PostMapping(path = "/new")
public String createPurchase(@Valid @ModelAttribute(name = "Purchase") Purchase purchase, BindingResult result)
{
if (result.hasErrors())
{
return "neworder";
}
int purchaseId = 0;
try
{
purchaseId = PurchaseManager.insertPurchase(purchase);
}
catch (SQLException e)
{
return "redirect:/purchaseerror";
}
if (purchaseId == 0)
return "redirect:/purchaseerror";
return "redirect:/order/success?id=" + purchaseId;
}
【问题讨论】:
标签: java spring spring-boot thymeleaf