简体   繁体   中英

Validation and Message Joining Spring Framework

I've tried to implement a validation service to my project. My Validator Code looks like this:

public class UserValidator implements Validator{

    public boolean supports(Class<?> clazz) {
        return Ort.class.isAssignableFrom(clazz);
    }

    public void validate(Object target, Errors errors) {
        Ort ort = (Ort) target;
        if((ort.getcountryname().length() == 0) && (ort.getzip().length() == 0))
        {
        errors.reject("error.zip");
        errors.reject("error.countryname");
        }

    }

}

I have an message xml with he following entries: Code:

......
error.zip=There is an Error at Zip-Code
error.countryname=There is an Error at Countryname

Everything works fine. I make an outprint on my jsp like this: Code:

<form:errors path="*" cssClass="error message" element="div"/>

My HTML Code looks like this if I get an error: Code:

There is an Error at Zip-Code<br>There is an Error at Countryname

My Question: How can I join the message so that i get an outprint on my jsp with looks so: Code:

There is an Error at Zip-Code, There is an Error at Countryname

Can i do somethink like that in my validation class?

Code:

......
        errors.reject("error.zip"+"error.countryname");
......

I guess you mixed two concepts:

  • how the error messages are stored in the errors object ( errors.reject("error.zip"+"error.countryname") )
  • how they are printed There is an Error at Zip-Code, There is an Error at Countryname

If you want to change the way they are printed, then you have to change the form:errors tag, but NOT the way you add the messages to the error object! (In contrast: if you want to change the messages itself, then changing the output would be the wrong way.)

So remove the <br/> from the output and replace it by , you just need to specify the delimiter attribute of the form:errors tag:

<form:errors path="*" cssClass="error message" element="div" delimiter",&nbsp;"/>

@see Spring Reference Appendix G.4 The errors tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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