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

在Java / Spring中,如何妥善处理缺失的转换值?

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

在Java / Spring中,如何妥善处理缺失的转换值?

为此,您可以创建自己的Custom MessageSource。

就像是:

public class SpecialMessageSource extends ReloadableResourceBundleMessageSource {      @Override      protected MessageFormat resolveCode(String pre, Locale locale) {         MessageFormat result = super.resolveCode(pre, locale);         if (result.getPattern().isEmpty() && locale == Locale.CHINESE) { return super.resolveCode(pre, Locale.ENGLISH);         }         return result;      }      @Override      protected String resolveCodeWithoutArguments(String pre, Locale locale) {         String result= super.resolveCodeWithoutArguments(pre, locale);         if ((result == null || result.isEmpty()) && locale == Locale.CHINESE) { return super.resolveCodeWithoutArguments(pre, Locale.ENGLISH);         }         return result;      }   }

并在spring xml中将此messageSource bean配置为

<bean id="messageSource" >.....</bean>

现在要解决标签问题,您将调用

MessageSource's
以下两种方法之一

String getMessage(String pre, Object[] args, Locale locale);String getMessage(String pre, Object[] args, String defaultMessage, Locale locale);

resolveCode()
当您的消息标签具有参数并且您通过
args
如下所示的参数传递这些参数并调用时,将被
invalid.number= {0} is Invalid

调用
messageSource.getMessage("INVALID_NUMBER", new Object[]{2d}, locale)


resolveCodeWithoutArguments()
当您的消息标签没有参数并且您将
args
参数传递为null
validation.success = Validation Success

并调用时,将被调用
messageSource.getMessage("INVALID_NUMBER", null, locale)



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

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

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