【发布时间】:2019-08-12 03:17:48
【问题描述】:
如果有帮助,我们确实配置了 LocaleChangeInterceptor。
(原发于http://forum.springsource.org/showthread.php?123391-Transition-to-set-locale)
【问题讨论】:
标签: spring-webflow
如果有帮助,我们确实配置了 LocaleChangeInterceptor。
(原发于http://forum.springsource.org/showthread.php?123391-Transition-to-set-locale)
【问题讨论】:
标签: spring-webflow
最好使用现有组件。这是配置为使用“LocaleChangeInterceptor”的 Spring WebFlow 的 sn-p:
@Bean
public FlowHandlerMapping flowHandlerMapping(FlowDefinitionRegistry flowDefinitionRegistry) {
FlowHandlerMapping handlerMapping = new FlowHandlerMapping();
handlerMapping.setInterceptors(new Object[] { localeChangeInterceptor() });
// ... other configuration
return handlerMapping;
}
/**
* @category locale_switcher
*/
@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.ENGLISH);
return localeResolver;
}
/**
* @category locale_switcher
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
它是如何工作的:
http://localhost:8080/myflow?execution=e1s1&lang=en
【讨论】:
这不是我想要的,但这是我发现的唯一要做的事情。也就是说,我创建了一个从转换中调用的动作方法:
<transition on="switchLanguage" validate="false">
<evaluate expression="myAction.switchLanguage"/>
</transition>
并且,特别是在这种情况下,对于扩展 MultiAction 的 Action 类:
public Event switchLanguage(RequestContext context)
{
// get the "other" locale string itself from the current locale's resource bundle
Locale locale = context.getExternalContext().getLocale();
MessageSource ms = context.getActiveFlow().getApplicationContext();
String newLocaleString = ms.getMessage("lang.other", null, locale);
HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getNativeRequest();
HttpServletResponse res = (HttpServletResponse) context.getExternalContext().getNativeResponse();
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(req);
localeResolver.setLocale(req, res, StringUtils.parseLocaleString(newLocaleString));
return success();
}
在我的例子中,我只需要支持两种语言,所以我在我的两个 messages.properties 和 messages_es.properties 文件中定义了属性:
lang.other=es
或
lang.other=en
对于其他 Action 类方法,根据需要返回指示没有失败、再次返回相同状态或转换到新状态的任何内容。
如果转换操作调用一个普通的 Java 方法,被调用的方法可能会返回一个布尔值,其值是 true 或 false,指示转换是应该发生还是被阻止执行。一个方法也可以返回一个字符串,其中文字值“success”、“yes”或“true”表示应该发生转换,任何其他值表示相反。
【讨论】:
AbstractAction,它有一个success() 方法来返回成功Event。我将编辑答案以说明这一点。