0

When using custom validation under this scenario:

if(officer.getBadgeId() == 0){

        errors.rejectValue("badgeId", "badgeId.required");
}

if the POJO/Model officer has a datatype of int for the badgeId can a string be set as the error message for the attribute? Or the datatype of the attribute does not matter when doing validation and returning error messages to the view.

If it is an issue how do you handle this scenario in terms or returning an error message to the user from custom validation

4

2 回答 2

3

错误消息将String仅为类型。

void rejectValue(String field, String errorCode, String defaultMessage)
使用给定的错误描述拒绝当前对象的给定字段。

春季文档链接。

于 2012-09-02T04:49:58.677 回答
0

这是一个示例:http ://alasdoo.com/2011/07/data-validation-conversion-basics-custom-error-messages-in-spring-mvc/

if (user.getBirthYear()!=null)
            if ( user.getBirthYear() <MINI_YEAR || user.getBirthYear() >MAX_YEAR)
                e.rejectValue( "birthYear", "user.birthYear.outOfInterval");
    }

在这里,您可以看到作为日期类型的birthYear 返回了一条存储在 message.properties 文件中的 String 类型的错误消息。

于 2012-09-02T04:53:08.333 回答