spring-mvc - Spring MVC 中带有 Bean 集合的自定义绑定(bind)错误消息

标签 spring-mvc

我们使用的是 Spring MVC 3.0.6,但我们没有使用 JSR 303 验证,只有在处理模型表单 bean 的 Controller 方法中使用 BindingResult 的绑定(bind)错误。我将尝试简化下面的示例,因为问题不在于如何构建事物,因为这些决定是在我到达之前做出的。我只是想让事情在我拥有的参数范围内正常工作。

在我正在处理的这个特定表单中,我有一个表单 bean,它是一个子 bean 列表, View 允许用户添加/删除一堆这些子 bean。

表单 bean 看起来像:

public class FormBean {
    private List<SubBean> subBeans;
    ...
}

和子 bean :
public class SubBean {
    private Integer value1;
    private Date value2;
    private String value3;
}

在 View JSP 中,我们正在执行以下操作:
<form:form modelAttribute="formBean">
    <spring:hasBindErrors name="formBean">
        <div class="error-box">
            <div class="error-txt">
                <form:errors path="*" cssClass="error" />
            </div>
        </div>
    </spring:hasBindErrors>

    <c:forEach items="${formBean.subBeans}" var="subBean" varStatus="subBeanStatus">
        ...
        <form:input path="subBeans[${subBeanStatus.index}].value1" />
        <form:input path="subBeans[${subBeanStatus.index}].value2" />        
        <form:input path="subBeans[${subBeanStatus.index}].value3" />
        ...
    </c:forEach>
    ...
</form:form>

当我使用未通过 Binding-mustard 的值提交表单时,问题就出现了。例如,如果我为 value1 添加一个无效的 int 值,我会收到如下错误消息:
Failed to convert property value of type java.lang.String to required type java.lang.Integer for property subBeans[0].value1; nested exception is java.lang.NumberFormatException: For input string: "sdfs"

我知道使用非嵌套 bean,您可以简单地以以下形式向 Resource Bunder 添加一条消息:
typeMismatch.beanName.fieldName="This is my custom error message!!!"

但是当你有一个列表时,你如何控制错误消息,就像我一样?

最佳答案

我也不喜欢默认消息,并自定义了我自己的 BindingErrorProcessor。

基本上,我想要的通常只是“最后一个字段”的名称——我想说的是 Date 的值无效,或者 Staff 的值无效,等等。我还包括了被拒绝的字段文本,标准 Spring 错误处理器不提供给消息。

public class SimpleMessage_BindingErrorProcessor 
        extends DefaultBindingErrorProcessor 
        {

    @Override
    public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
        // Create field error with the exceptions's code, e.g. "typeMismatch".
        String field = ex.getPropertyName();
        String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);

        Object rejectedValue = ex.getValue();
        if (rejectedValue != null && rejectedValue.getClass().isArray()) {
            rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
        }
        Object[] arguments = getArgumentsForBindError( bindingResult.getObjectName(), field, rejectedValue);

        FieldError fieldError = new FieldError(
                bindingResult.getObjectName(), field, rejectedValue, true,
                codes, arguments, ex.getLocalizedMessage());
        bindingResult.addError( fieldError);
    }

    /**
     * Return FieldError arguments for a binding error on the given field.
     * <p>TW's implementation returns {0} simple field title, {1} rejected value, {2} FQ field resolvable as per Spring DefaultBindingErrorProcessor
     * (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes).
     * @param objectName the name of the target object
     * @param propPath the field that caused the binding error
     * @param rejectedValue the value that was rejected
     * @return the Object array that represents the FieldError arguments
     * @see org.springframework.validation.FieldError#getArguments
     * @see org.springframework.context.support.DefaultMessageSourceResolvable
     */
    protected Object[] getArgumentsForBindError (String objectName, String propPath, Object/*String*/ rejectedValue) {

        // just the Simple Name of Field;
        //      (last field in path).
        //
        String lastField = getLastField_Title( propPath);

        // create Resolvable for "Fully-Qualified" Field;
        //      -- Spring standard,  too specific/ would require defining hundreds of distinct messages;    we don't use these.
        //
        String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + propPath, propPath};
        DefaultMessageSourceResolvable fqField_resolvable = new DefaultMessageSourceResolvable(codes, propPath);

        // return Args;     {0} simple name, {1} rejected text, {2} FQ complex name.
        return new Object[]{ 
                lastField, rejectedValue, fqField_resolvable
        };
    }

    /**
     * Return FieldError arguments for a binding error on the given field.
     * <p>TW's implementation returns {0} simple field title, {1} FQ field resolvable as per Spring DefaultBindingErrorProcessor
     * (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes).
     * @param objectName the name of the target object
     * @param propPath the field that caused the binding error
     * @return the Object array that represents the FieldError arguments
     * @see org.springframework.validation.FieldError#getArguments
     * @see org.springframework.context.support.DefaultMessageSourceResolvable
     */
    @Override
    protected Object[] getArgumentsForBindError (String objectName, String propPath) {

        // just the Simple Name of Field;
        //      (last field in path).
        //
        String lastField = getLastField_Title( propPath);

        // create Resolvable for "Fully-Qualified" Field;
        //      -- Spring standard,  too specific/ would require defining hundreds of distinct messages;    we don't use these.
        //
        String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + propPath, propPath};
        DefaultMessageSourceResolvable fqField_resolvable = new DefaultMessageSourceResolvable(codes, propPath);

        // return Args;     {0} simple name, {2} FQ complex name.
        return new Object[]{ 
                lastField, fqField_resolvable
        };
    }

    protected String getLastField_Title (String propPath) {
        int index = propPath.lastIndexOf('.');
        String title = (index >= 0) ? propPath.substring(index+1) : propPath;
        return StrUtil.capitalize( title);
    }

}

这很好用!现在你所有的 messages.properties 必须说的是:
# Type Mismatch generally;
#   INCOMING 21/8/13 -- use {0} as 'Simple Name' of field,  when using SimpleMessage_BindingErrorProcessor;   {1} is 'resolvable' FQN of field.
#   
typeMismatch=Invalid value for {0}: "{1}"


# Method Invocation/ value conversion; 
#   INCOMING 21/8/13 -- only expected for certain 'Value Converting'/ self-parsing properties;  SPEC.
#   
methodInvocation.machine=Invalid value for {0}: "{1}"

这个区域不是很清楚..整个绑定(bind)->错误处理->消息解析系统相当复杂,并且(据我所见)卡住了消息代码通常过于具体的问题。

这方面的内容很少(我在 Google 上没有找到任何直接相关的内容),所以我希望这对人们有所帮助。

关于spring-mvc - Spring MVC 中带有 Bean 集合的自定义绑定(bind)错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14483223/

相关文章:

java - Hibernate选择查询(员工通过电子邮件ID获取或通过姓名获取)

json - Spring MVC 和 JSON,未找到 Jackson 类异常

java - Spring Boot 通过请求作用域 bean 在单例服务/ Controller 中 Autowiring 字段

java - Spring Boot没有初始化Ehcache

java - 如何发送并行 GET 请求并等待结果响应?

java - @Value 用于在 Spring 3 中初始化 java.util.Date

java - 为什么Spring MVC项目中css、js不连接html?

java - 如何在spring中从jsp文件的选择框中获取值。

java - 得到 JdkVersion classNotFoundException

java - 设置为更新时,Spring Boot mvc java 应用程序无法运行