【问题标题】:Spring Boot, Thymeleaf Validation for Composition ClassSpring Boot,组合类的 Thymeleaf 验证
【发布时间】:2019-05-03 04:39:51
【问题描述】:

如何在 Thymeleaf/Spring Boot 中验证组合关系。我有一个简单的 FundTrf 类,它“有一个”数据类。问题是当我验证表单输入时,FundTrf 类相关字段得到验证,但 Data 类相关字段没有得到验证。这些类之间是否需要进行额外的投标。以下是我尝试过的。

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>HNB CEFT | Test Bed</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <h1>Form</h1>
        <form action="#" th:action="@{/ceft/fundTrf}" th:object="${fundTrf}" method="post">
            <table>
                <tr><td>Version </td><td><input type="text" th:field="*{version}" /></td>
                    <td th:if="${#fields.hasErrors('version')}" th:errors="*{version}">Version Error</td>
                </tr>
                <tr><td>Bank Code </td><td><input type="text" th:field="*{data.dest_bank_code}" /></td>
                    <td th:if="${#fields.hasErrors('data.dest_bank_code')}" th:errors="*{data.dest_bank_code}">Bank Code Error</td>
                </tr>
                <tr><td>Amount </td><td><input type="text" th:field="*{data.amount}" /></td>
                    <td th:if="${#fields.hasErrors('data.amount')}" th:errors="*{data.amount}">Amount Error</td>
                </tr>
            </table>
            <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
        </form>
    </body>
</html>

下面是我的控制器类。

@Controller
public class Hello  implements WebMvcConfigurer{

    @GetMapping("/ceft/welcome")
    public String welcomeForm(Model model) {
        model.addAttribute("fundTrf", new FundTrf());
        return "welcome";
    }

    @PostMapping("/ceft/fundTrf")
    public String ceftTransaction(@ModelAttribute @Valid FundTrf fundTrf,  BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return "welcome";
        } else {
            return "result";
        }
    }
}

下面是我的 FundTrf 课程

public class FundTrf {

    @NotEmpty
    private String version;
    private Data data;

    ..Getters and Setters
}

这是 Data 类。

public class Data {

    @NotEmpty
    private String reqId;
    @NotEmpty
    private String frm_hnb_account;
    @NotEmpty
    private String dest_bank_account;
    @NotEmpty
    private String benificiary_name;
    @NotEmpty
    private String dest_bank_code;
    @NotEmpty
    @Size(min = 2, max = 30)
    private String amount;

    ..Getters and Setters
}

问题是当我提交带有空值的表单时,出现“版本不能为空”的消息,但金额验证不起作用。我在这里做错了什么?

【问题讨论】:

    标签: java spring spring-boot thymeleaf


    【解决方案1】:

    您必须在对象 Data 上设置 @Valid 才能验证您的 Data 属性。

    public class FundTrf {
    
        @NotEmpty
        private String version;
        @Valid //ADDED VALID HERE
        private Data data;
    
        ..Getters and Setters
    }
    

    javax.validation.Valid 的 javadoc 说:

    标记属性、方法参数或方法返回类型 验证级联。在对象上定义的约束及其 当属性、方法参数或 方法返回类型已验证。此行为以递归方式应用。

    【讨论】:

    • 尝试在控制器中添加@Valid Data 数据,但出现以下错误。字段“版本”上的对象“fundTrf”中的字段错误:拒绝值 []...是否也应在模板中引用数据?
    • 我用你应该放置@Valid的地方编辑了我的答案
    猜你喜欢
    • 2017-07-14
    • 1970-01-01
    • 2020-11-05
    • 2023-03-27
    • 1970-01-01
    • 2017-11-28
    • 2015-07-29
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多