Spring Boot thymelaf错误请求400,而不是显示用户错误



我正在尝试使用post-request提交表单,并首先验证输入。

然而,当我做出错误的输入(例如全部为空(而不是显示错误时,我得到了错误的请求(400(。

为了显示错误,我使用HTML中的th:ifth: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

如何使显示错误使用thymelaf工作?

EDIT:我通过在Controller类中的post方法中添加BindingResult参数并检查是否有任何错误,成功地使其工作。如果是,我返回表单所在的同一页(/new映射(,即";neworder";。

返回"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;
}

我认为如果您在createPurchase方法中使用Model作为第二个参数,您的问题就可以解决。然后在您的方法中,您可以执行以下操作来添加消息:

@PostMapping("/add")
public String addUser(@Valid User user, BindingResult result, Model model) {
if (result.hasErrors()) {
return "errors/addUser";
}
repository.save(user);
model.addAttribute("users", repository.findAll()); //this is what you could do.
return "errors/home";
}

这将导致你的方法如下(请自行修改——我写这篇文章只是为了示范(:

@PostMapping(path = "/new")
public String createPurchase(@Valid @ModelAttribute(name = "Purchase") Purchase purchase, Model model)
{
int purchaseId = 0;
try
{
purchaseId = PurchaseManager.insertPurchase(purchase);
}
catch (SQLException e)
{
// todo: don't return right away. Add `model.addAttribute` first.
return "purchaseerror";
}
if (purchaseId == 0) {
// todo: don't return right away. Add `model.addAttribute` first.
return "purchaseerror";
}

return "redirect:/order/success?id=" + purchaseId;
}

modelAttribute中添加的值将由Thymelaf实现从中挑选,您可以从中挑选错误(正如您已经填充的那样(,并简单地将逻辑建立在这个基础上。

为了更好地理解,你可以从这里学习。只需记住,您需要添加到Model中,然后才能在此基础上在thymelaf中布局逻辑。

我希望我的回答能解决你的疑问。如果没有,请道歉。

相关内容

最新更新