使用thymleaf和hibernate-validation-API

时间:2017-12-20 13:44:03

标签: java spring thymeleaf hibernate-validator

我正在尝试使用带有百里香的hibernate validation api验证我的表单。看来每当我点击两个日期字段为空的生成按钮时,页面中都不会显示错误消息。

 <form th:action="@{/report}" th:object="${salesReportMapping}" method="post">
    <div class="col-md-3">
        <div class="form-group">
            <label for="fromDate">From Date:--</label>
            <input class="form-control" th:field="*{fromDate}" 
            type="date" id="fromDate" name="fromDate"/>
            <p th:if="${#fields.hasErrors('fromDate')}" class="label label-danger" 
            th:errors="*{fromDate}">Please choose valid from date</p>
        </div>
        </div>
    <div class="col-md-3">
        <div class="form-group">
            <label for="toDate">To Date:--</label>
                <input class="form-control" th:field="*{toDate}" 
                type="date" id="toDate" name="toDate"/>
                <p th:if="${#fields.hasErrors('toDate')}" 
                class="label label-danger" th:errors="*{toDate}">
                Please choose valid to date</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="form-group">
            <label for="salesType">Type:-</label>
            <select name="salesType"  id="salesType" class="form-control">
                <option value="product">Product</option>
                <option value="service">Service</option>
            </select>
        </div>
    </div>
    <div class="col-md-3">
        <button style="margin-top: 24px;" id="generate" name="action"
         class="btn btn-primary" value="getReport">Generate</button>
        <button style="margin-top: 24px;" name="action"
         class="btn btn-primary" value="downloadExcel">Download Excel</button>
    </div>
</form>

控制器

控制器看起来像这样

@RequestMapping(value="/report", method=RequestMethod.POST, params="action=getReport")
public String getReport(@ModelAttribute("salesReportMapping")
                        @Valid SalesReportMapping salesReportMapping,
                        BindingResult bindingResult, Model model) throws ParseException {

    if(bindingResult.hasErrors()){
        model.addAttribute("salesReportMapping", new SalesReportMapping());
        return "reports/reports";
    }

    SimpleDateFormat smd = new SimpleDateFormat("yyyy-MM-dd");
    Date fDate = smd.parse(salesReportMapping.getFromDate());
    Date tDate = smd.parse(salesReportMapping.getToDate());
    List<Sales> salesList = reportServices.getSalesReportList(fDate, tDate,
                                    salesReportMapping.getSalesType());

    model.addAttribute("salesReportMapping", new SalesReportMapping());
    return "reports/reports";
}

Pojo课程

包含验证标准的简单pojo类

public class SalesReportMapping {

    @NotNull(message = "from date cannot be empty")
    @NotEmpty(message = "cannot be empty")
    public String fromDate;

    @NotNull(message = "to date cannot be empty")
    @NotEmpty(message = "cannot be empty")
    public String toDate;
    public String salesType;

   /*********getter setters *************/
}

1 个答案:

答案 0 :(得分:0)

我实际上只是通过

解决了这个问题
if(bindingResult.hasErrors()){
        model.addAttribute("salesReportMapping", new SalesReportMapping());
        return "reports/reports";
    }

if(bindingResult.hasErrors()){
        model.addAttribute("salesReportMapping", salesReportMapping);
        return "reports/reports";
    }