栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

基于注解的Spring bean验证

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于注解的Spring bean验证

我快速浏览了BeanValidator
API
,看起来您可能想尝试
errorCodeConverter 属性。

您是否需要实现自己的ErrorCodeConverter或使用提供的实现之一?

....<bean id="validator"     p:configurationLoader-ref="configurationLoader"    p:errorCodeConverter-ref="errorCodeConverter" /><bean id="errorCodeConverter"  />....

注意:configurationLoader是本教程中使用的config XML中定义的另一个bean。

转换器示例:

package contact;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springmodules.validation.bean.converter.ErrorCodeConverter;public class MyErrorCodeConverter implements ErrorCodeConverter {    private Log log = LogFactory.getLog(MyErrorCodeConverter.class);    @Override    public String convertPropertyErrorCode(String errorCode, Class clazz, String property) {        log.error(String.format("Property %s %s %s", errorCode, clazz.getClass().getName(), property));        return errorCode;  // <------ use the errorCode only    }    @Override    public String convertGlobalErrorCode(String errorCode, Class clazz) {        log.error(String.format("Global %s %s", errorCode, clazz.getClass().getName()));        return errorCode;    }}

现在,属性应该可以工作了:

MyEmailErrorCode=Bad emailclass Foo {    @Email(errorCode="MyEmailErrorCode")    String email}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429024.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号