簡體   English   中英

未顯示Spring驗證錯誤

[英]Spring validation errors not displayed

我有以下情況。 我有一個驗證器來驗證我的命令對象,並設置Errors對象上的錯誤,以便在我的表單中顯示。 驗證器按預期調用並且工作正常,但是當我因驗證錯誤而被發送回我的表單時,我不會顯示我在Errors對象上設置的錯誤。

驗證器:

public void validate(Object obj, Errors err) {   
    MyCommand myCommand = (MyCommand) obj;   
    int index = 0;   
    for (Field field : myCommand.getFields()) {   
        if (field.isChecked()) {   
            if ((field.getValue() == null) || (field.getValue().equals(""))) {   
                err.rejectValue("fields[" + index + "].value", "errors.missing");   
            }   
        }   
        index++;   
    }   
    if (myCommand.getLimit() < 0) {   
        err.rejectValue("limit", "errors.invalid");   
    }   
}

命令:

public class MyCommand {   
    private List<Field> fields;   
    private int limit;   

    //getters and setters   
}   

public class Field {   
    private boolean checked;   
    private String name;   
    private String value;   

    //getters and setters   
}

形成:

    <form:form id="myForm" method="POST" action="${url}" commandName="myCommand">   
    <c:forEach items="${myCommand.fields}" var="field" varStatus="status">   
        <form:checkbox path="fields[${status.index}].checked" value="${field.checked}" />   
        <c:out value="${field.name}" />   
        <form:input path="fields[${status.index}].value" />   
        <form:errors path="fields[${status.index}].value" cssClass="error" /></td>   
        <form:hidden path="fields[${status.index}].name" />   
    </c:forEach>   
    <fmt:message key="label.limit" />    
    <form:input path="limit" />   
    <form:errors path="limit" cssClass="error" />   
</form:form>

控制器:

    @RequestMapping(value = REQ_MAPPING, method = RequestMethod.POST)   
    public String onSubmit(Model model, MyCommand myCommand, BindingResult result) {   
    // validate   
    myCommandValidator.validate(myCommand, result);   
    if (result.hasErrors()) {   
        model.addAttribute("myCommand", myCommand);   
        return VIEW;   
    }   

    // form is okay, do stuff and redirect   
}

可能是我在驗證器和標簽中給出的路徑不正確嗎? 驗證器驗證包含對象列表的命令對象,這就是為什么我在注冊錯誤消息時在命令對象的列表上給出索引(例如:“fields [”+ index +“]”。value)。 或者是否包含錯誤的Errors對象不可用於我的視圖?

歡迎和贊賞任何幫助,它可能會給我一個提示或指出我正確的方向。

我發現你的問題是什么。 對象myCommand屬性fields始終為null。 您需要創建類mycommand的構造函數中,你需要使用LazyList從Apache的共享或AutoPopulatingList從Spring構建的汽車越來越多的Field

在示例中(使用AutoPopulatingList):

public class MyCommand {   
    private List<Field> fields;   
    private int limit;   

    //getters and setters
    //...
    //Constructor
    public MyCommand() {
        fields = new AutoPopulatingList<Field>(Field.class);
    } 
}

err.rejectValue("fields[" + index + "].value", "errors.missing"); 不會做你想要達到的目標。 第一個參數必須是Command bean的屬性名稱,在您的case字段中。

要向用戶發送消息,您必須在屬性文件中使用可參數化的消息,例如。 myform.field.error = you have an error in field {0}

所以,使用Errors對象的這個方法:

void rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage) 

index傳遞給errorArgs的位置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM