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

Posted

技术标签:

【中文标题】Spring Boot thymeleaf 错误请求 400 而不是显示用户错误【英文标题】:Spring Boot thymeleaf bad request 400 instead of showing user error 【发布时间】:2022-01-22 08:07:09 【问题描述】:

我正在尝试使用发布请求提交表单并首先验证输入。

但是,当我输入错误(例如全部为空)而不是显示错误时,我收到错误请求 (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

如何使用 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;
    

【问题讨论】:

【参考方案1】:

如果您在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;
    

然后,您的 Thymeleaf 实现将选择 modelAttribute 中添加的值,您可以从中选择错误(因为您将填充它)并简单地基于此逻辑。

您可以按照示例from here 更好地理解。请记住,您需要先添加到Model,然后才能根据它在百里香中布置逻辑。

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

【讨论】:

Thx 寻求帮助,但我设法使其工作不是通过添加模型作为参数,但我知道这可能是方式。我的退货“purchaseerror”;这里有点混乱。添加 BindingResult 并返回相同的页面就足够了。

以上是关于Spring Boot thymeleaf 错误请求 400 而不是显示用户错误的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 和 Thymeleaf 在字段错误时返回 400 错误

在 Thymeleaf + Spring Boot 中检索错误时间

thymeleaf + spring-boot 验证错误未显示在 html 上

Spring Boot 和 Thymeleaf - 删除严格的 HTML 错误检查

Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息

Spring Boot Thymeleaf 自定义验证器不显示错误