定义切换区域设置并返回到当前视图状态的过渡的最简单方法是什么?



如果有帮助的话,我们确实配置了LocaleChangeInterceptor

(最初发布到http://forum.springsource.org/showthread.php?123391-转换为设置区域设置)

最好使用现有组件。以下是配置为使用"LocaleChangeInterceptor"的SpringWebFlow片段:

@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.propertiesmessages_es.properties文件中定义了属性:

lang.other=es

lang.other=en

对于其他Action类方法,根据需要返回所需的任何内容以指示没有失败,或者再次返回到同一状态,或者转换到新状态。

请参阅https://docs.spring.io/spring-webflow/docs/2.5.1.RELEASE/reference/html/views.html#transition-行动

如果转换操作调用普通Java方法,则被调用的方法可能会返回布尔值,该布尔值的true或false表示转换是否应该发生或阻止执行。方法还可以返回一个字符串,其中文字值"success"、"yes"或"true"表示应该发生转换,而任何其他值都表示相反。

相关内容

最新更新