【问题标题】:What's the simplest way to define a transition that switches the locale and returns to the current view-state?定义切换语言环境并返回当前视图状态的转换的最简单方法是什么?
【发布时间】:2019-08-12 03:17:48
【问题描述】:

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

(原发于http://forum.springsource.org/showthread.php?123391-Transition-to-set-locale)

【问题讨论】:

    标签: spring-webflow


    【解决方案1】:

    最好使用现有组件。这是配置为使用“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
    

    【讨论】:

      【解决方案2】:

      这不是我想要的,但这是我发现的唯一要做的事情。也就是说,我创建了一个从转换中调用的动作方法:

          <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 类方法,根据需要返回指示没有失败、再次返回相同状态或转换到新状态的任何内容。

      见https://docs.spring.io/spring-webflow/docs/2.5.1.RELEASE/reference/html/views.html#transition-actions

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

      【讨论】:

      • 这里的success()是什么? @dbreaux
      • @Aliy 啊,对不起。那时我的 Action 类扩展了AbstractAction,它有一个success() 方法来返回成功Event。我将编辑答案以说明这一点。
      • 实际上这个解决方案在我的项目中不起作用,我不知道如何在Spring Webflow中实现多语言功能,因为webflow的重定向,我已经苦苦挣扎了一个星期!我认为您的解决方案会有所帮助,但我不理解您的代码))@dbreaux
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多