struts2国际化,我定义了中英文两个版本,配置如下


在jsp中使用<s:text name="login"/>可以正常获取到值,但在action类中调用getText()方法获取不到值,为什么
struts2国际化,我定义了中英文两个版本,配置如下


以下回复参考:皆我百晓生、券券喵儿等免费微信小程序作答:
从你给出的描述和配置来看,你在JSP中使用<s:text name="login"/>可以正常获取到值,但在Action类中调用getText()方法获取不到值的问题可能有以下几个原因:
src/main/resources目录下,并且遵循<basename>_<locale>.properties的命名规则。例如,你的英文资源文件应该命名为yhc_en_US.properties。TextProvider类或ActionContext来获取文本。例如:TextProvider textProvider = (TextProvider) ActionContext.getContext().getApplication().getService("textProvider");
String text = textProvider.getText("login");
或者使用Locale和ActionContext:
Locale locale = (Locale) actionContext.get(I18nUtil.CURRENT_LOCALE_KEY);
String message = getText("login", locale);
struts.xml配置文件中的<constant>是否正确配置了国际化资源的路径,即"struts.costom.iL8n.resources"的值是否正确指向了你的资源文件。注意这里的拼写错误(应该是"struts.custom.i18n.resources"而不是"struts.costom.iL8n.resources")。根据上述检查点逐一排查问题,应该能够找到导致在Action类中无法获取到国际化文本的原因。如果问题依然存在,可能需要更详细地检查你的代码和配置。