简体   繁体   English

Spring验证,如何让PropertyEditor生成特定的错误消息

[英]Spring validation, how to have PropertyEditor generate specific error message

I'm using Spring for form input and validation. 我正在使用Spring进行表单输入和验证。 The form controller's command contains the model that's being edited. 表单控制器的命令包含正在编辑的模型。 Some of the model's attributes are a custom type. 某些模型的属性是自定义类型。 For example, Person's social security number is a custom SSN type. 例如,Person的社会安全号码是自定义SSN类型。

public class Person {
    public String getName() {...}
    public void setName(String name) {...}
    public SSN getSocialSecurtyNumber() {...}
    public void setSocialSecurtyNumber(SSN ssn) {...}
}

and wrapping Person in a Spring form edit command: 并在Spring表单编辑命令中包装Person:

public class EditPersonCommand {
    public Person getPerson() {...}
    public void setPerson(Person person) {...}
}

Since Spring doesn't know how to convert text to a SSN, I register a customer editor with the form controller's binder: 由于Spring不知道如何将文本转换为SSN,因此我使用表单控制器的binder注册了一个客户编辑器:

public class EditPersonController extends SimpleFormController {
    protected void initBinder(HttpServletRequest req, ServletRequestDataBinder binder) {
        super.initBinder(req, binder);
        binder.registerCustomEditor(SSN.class, "person.ssn", new SsnEditor());
    }
}

and SsnEditor is just a custom java.beans.PropertyEditor that can convert text to a SSN object: 和SsnEditor只是一个自定义java.beans.PropertyEditor ,可以将文本转换为SSN对象:

public class SsnEditor extends PropertyEditorSupport {
    public String getAsText() {...} // converts SSN to text
    public void setAsText(String str) {
        // converts text to SSN
        // throws IllegalArgumentException for invalid text
    }
}

If setAsText encounters text that is invalid and can't be converted to a SSN, then it throws IllegalArgumentException (per PropertyEditor setAsText 's specification). 如果setAsText遇到无效且无法转换为SSN的文本,则会抛出IllegalArgumentException (根据PropertyEditor setAsText的规范)。 The issue I'm having is that the text to object conversion (via PropertyEditor.setAsText() ) takes place before my Spring validator is called. 我遇到的问题是文本到对象的转换(通过PropertyEditor.setAsText() )发生我的Spring验证器被调用之前。 When setAsText throws IllegalArgumentException , Spring simply displays the generic error message defined in errors.properties . setAsText抛出IllegalArgumentException ,Spring只显示errors.properties定义的一般错误消息。 What I want is a specific error message that depends on the exact reason why the entered SSN is invalid. 我想要的是一个特定的错误消息,取决于输入的SSN无效的确切原因。 PropertyEditor.setAsText() would determine the reason. PropertyEditor.setAsText()将确定原因。 I've tried embedded the error reason text in IllegalArgumentException 's text, but Spring just treats it as a generic error. 我已经尝试在IllegalArgumentException的文本中嵌入错误原因文本,但Spring只是将其视为一般错误。

Is there a solution to this? 这个问题有方法解决吗? To repeat, what I want is the specific error message generated by the PropertyEditor to surface to the error message on the Spring form. 重复一遍,我想要的是PropertyEditor生成的特定错误消息,以表示Spring表单上的错误消息。 The only alternative I can think of is to store the SSN as text in the command and perform validation in the validator. 我能想到的唯一选择是将SSN作为文本存储在命令中,并在验证器中执行验证。 The text to SSN object conversion would take place in the form's onSubmit . SSN对象转换的文本将在表单的onSubmit This is less desirable as my form (and model) has many properties and I don't want to have to create and maintain a command that has each and every model attribute as a text field. 这是不太理想的,因为我的表单(和模型)具有许多属性,我不希望创建和维护一个将每个模型属性作为文本字段的命令。

The above is just an example, my actual code isn't Person/SSN, so there's no need to reply with "why not store SSN as text..." 以上只是一个例子,我的实际代码不是Person / SSN,所以没有必要回复“为什么不将SSN存储为文本......”

You're trying to do validation in a binder. 您正尝试在活页夹中进行验证。 That's not the binder's purpose. 这不是活页夹的目的。 A binder is supposed to bind request parameters to your backing object, nothing more. 绑定器应该将请求参数绑定到您的后备对象,仅此而已。 A property editor converts Strings to objects and vice versa - it is not designed to do anything else. 属性编辑器将字符串转换为对象,反之亦然 - 它不是为了做其他事情而设计的。

In other words, you need to consider separation of concerns - you're trying to shoehorn functionality into an object that was never meant to do anything more than convert a string into an object and vice versa. 换句话说,你需要考虑关注点的分离 - 你试图将功能强加到一个对象中,而这个对象除了将字符串转换为对象外,从不打算做任何事情,反之亦然。

You might consider breaking up your SSN object into multiple, validateable fields that are easily bound (String objects, basic objects like Dates, etc). 您可以考虑将SSN对象分解为多个易于绑定的可验证字段(String对象,日期等基本对象)。 This way you can use a validator after binding to verify that the SSN is correct, or you can set an error directly. 这样,您可以在绑定后使用验证程序来验证SSN是否正确,或者您可以直接设置错误。 With a property editor, you throw an IllegalArgumentException, Spring converts it to a type mismatch error because that's what it is - the string doesn't match the type that is expected. 使用属性编辑器,抛出IllegalArgumentException,Spring将其转换为类型不匹配错误,因为它就是这样 - 字符串与预期的类型不匹配。 That's all that it is. 就是这样。 A validator, on the other hand, can do this. 另一方面,验证器可以做到这一点。 You can use the spring bind tag to bind to nested fields, as long as the SSN instance is populated - it must be initialized with new() first. 只要填充了SSN实例,就可以使用spring绑定标记绑定到嵌套字段 - 必须首先使用new()初始化它。 For instance: 例如:

<spring:bind path="ssn.firstNestedField">...</spring:bind>

If you truly want to persist on this path, however, have your property editor keep a list of errors - if it is to throw an IllegalArgumentException, add it to the list and then throw the IllegalArgumentException (catch and rethrow if needed). 但是,如果你真的想坚持这条路径,那么让你的属性编辑器保留一个错误列表 - 如果要抛出IllegalArgumentException,将它添加到列表然后抛出IllegalArgumentException(如果需要,捕获并重新抛出)。 Because you can construct your property editor in the same thread as the binding, it will be threadsafe if you simply override the property editor default behavior - you need to find the hook it uses to do binding, and override it - do the same property editor registration you're doing now (except in the same method, so that you can keep the reference to your editor) and then at the end of the binding, you can register errors by retrieving the list from your editor if you provide a public accessor. 因为您可以在与绑定相同的线程中构造属性编辑器,所以如果您只是覆盖属性编辑器的默认行为,它将是线程安全的 - 您需要找到它用来进行绑定的钩子,并覆盖它 - 执行相同的属性编辑器您现在正在进行注册(除了使用相同的方法,以便您可以保留对编辑器的引用),然后在绑定结束时,如果您提供公共访问器,则可以通过从编辑器中检索列表来注册错误。 Once the list is retrieved you can process it and add your errors accordingly. 检索到列表后,您可以对其进行处理并相应地添加错误。

As said: 如上所述:

What I want is the specific error message generated by the PropertyEditor to surface to the error message on the Spring form 我想要的是PropertyEditor生成特定错误消息,表示Spring表单上的错误消息

Behind the scenes, Spring MVC uses a BindingErrorProcessor strategy for processing missing field errors, and for translating a PropertyAccessException to a FieldError . 在幕后,Spring MVC使用BindingErrorProcessor策略处理丢失的字段错误,并将PropertyAccessException转换为FieldError So if you want to override default Spring MVC BindingErrorProcessor strategy, you must provide a BindingErrorProcessor strategy according to: 因此,如果要覆盖默认的Spring MVC BindingErrorProcessor策略,则必须根据以下内容提供BindingErrorProcessor策略:

public class CustomBindingErrorProcessor implements DefaultBindingErrorProcessor {

    public void processMissingFieldError(String missingField, BindException errors) {
        super.processMissingFieldError(missingField, errors);
    }

    public void processPropertyAccessException(PropertyAccessException accessException, BindException errors) {
        if(accessException.getCause() instanceof IllegalArgumentException)
            errors.rejectValue(accessException.getPropertyChangeEvent().getPropertyName(), "<SOME_SPECIFIC_CODE_IF_YOU_WANT>", accessException.getCause().getMessage());
        else
            defaultSpringBindingErrorProcessor.processPropertyAccessException(accessException, errors);
    }

}

In order to test, Let's do the following 为了测试,让我们做以下几点

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    binder.registerCustomEditor(SSN.class, new PropertyEditorSupport() {

        public String getAsText() {
            if(getValue() == null)
                return null;

            return ((SSN) getValue()).toString();
        }

        public void setAsText(String value) throws IllegalArgumentException {
            if(StringUtils.isBlank(value))
                return;

            boolean somethingGoesWrong = true;
            if(somethingGoesWrong)
                throw new IllegalArgumentException("Something goes wrong!");
        }

    });
}

Now our Test class 现在我们的Test类

public class PersonControllerTest {

    private PersonController personController;
    private MockHttpServletRequest request;

    @BeforeMethod
    public void setUp() {
        personController = new PersonController();
        personController.setCommandName("command");
        personController.setCommandClass(Person.class);
        personController.setBindingErrorProcessor(new CustomBindingErrorProcessor());

        request = new MockHttpServletRequest();
        request.setMethod("POST");
        request.addParameter("ssn", "somethingGoesWrong");
    }

    @Test
    public void done() {
        ModelAndView mav = personController.handleRequest(request, new MockHttpServletResponse());

        BindingResult bindingResult = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command");

        FieldError fieldError = bindingResult.getFieldError("ssn");

        Assert.assertEquals(fieldError.getMessage(), "Something goes wrong!");
    }

}

regards, 问候,

As a follow up to @Arthur Ronald's answer, this is how I ended up implementing this: 作为@Arthur Ronald回答的后续内容,这就是我最终实现这一点的方式:

On the controller: 在控制器上:

setBindingErrorProcessor(new CustomBindingErrorProcessor());

And then the binding error processor class: 然后绑定错误处理器类:

public class CustomBindingErrorProcessor extends DefaultBindingErrorProcessor {

    public void processPropertyAccessException(PropertyAccessException accessException, 
                                               BindingResult bindingResult) {

        if(accessException.getCause() instanceof IllegalArgumentException){

            String fieldName = accessException.getPropertyChangeEvent().getPropertyName();
            String exceptionError = accessException.getCause().getMessage();

            FieldError fieldError = new FieldError(fieldName,
                                                   "BINDING_ERROR", 
                                                   fieldName + ": " + exceptionError);

            bindingResult.addError(fieldError);
        }else{
            super.processPropertyAccessException(accessException, bindingResult);
        }

    }

}        

So the processor method's signature takes a BindingResult instead of a BindException on this version. 因此处理器方法的签名在此版本上采用BindingResult而不是BindException。

I believe you could just try to put this in your message source: 我相信你可以尝试将它放在你的消息来源中:

typeMismatch.person.ssn=Wrong SSN format typeMismatch.person.ssn =错误的SSN格式

This sounds similar to an issue I had with NumberFormatExceptions when the value for an integer property could not be bound if, say, a String was entered in the form. 这听起来类似于我在使用NumberFormatExceptions时遇到的问题,如果在表单中输入了String,则无法绑定整数属性的值。 The error message on the form was a generic message for that exception. 表单上的错误消息是该异常的通用消息。

The solution was to add my own message resource bundle to my application context and add my own error message for type mismatches on that property. 解决方案是将我自己的消息资源包添加到我的应用程序上下文中,并为该属性上的类型不匹配添加我自己的错误消息。 Perhaps you can do something similar for IllegalArgumentExceptions on a specific field. 也许您可以对特定字段上的IllegalArgumentExceptions执行类似的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM