【问题标题】:Using multiple Resources Bundles with Thymeleaf将多个资源包与 Thymeleaf 一起使用
【发布时间】:2014-03-31 15:55:05
【问题描述】:

我想使用 Thymeleaf 在 Spring MVC 应用程序中使用多个资源包。我无法访问

项目结构(EAR)

  • MyProject(通过部署程序集包括以下两个项目)
  • MyProjectEJB
  • 我的项目网站

    • src

      • 基础项目
        • 配置
          • ThymeleafConfig
          • 网络配置
    • 网页内容

      • WEB-INF

          • 我的库...
        • 消息

          • 全球
            • GlobalResources(获得 GlobalResources_fr.properties 和 GlobalResources_en.properties)。
          • 用户
            • UserResources(同时获得 UserResources_fr.properties 和 UserResources_en.properties)。
        • 观看次数

          • 用户
            • createOrUpdateUserForm.html

WebConfig.java

package baseproject.configuration;

import java.util.Locale;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "baseproject.controller")
public class WebConfig extends WebMvcConfigurerAdapter {

    public ResourceBundleMessageSource messageSource() {

        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

        String[] strBaseNames = {
                "/WEB-INF/messages/global/GlobalResources",
                "/WEB-INF/messages/user/UserResources",
        };

        messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setDefaultEncoding("UTF-8");

        // # -1 : never reload, 0 always reload
        messageSource.setCacheSeconds(0);
        messageSource.setBasenames(strBaseNames);

        return messageSource;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {

        LocaleChangeInterceptor result = new LocaleChangeInterceptor();
        result.setParamName("lang");

        return result;
    }       

    @Bean
    public LocaleResolver localeResolver() {

        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        sessionLocaleResolver.setDefaultLocale(Locale.ENGLISH);

        return sessionLocaleResolver;
    }

    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry) {

        interceptorRegistry.addInterceptor(localeChangeInterceptor());
    }

}

ThymeleafConfig.java

package baseproject.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
public class ThymeleafConfig {

    @Bean
    public ServletContextTemplateResolver templateResolver() {

        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();

        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");

        //NB, selecting HTML5 as the template mode.
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCacheable(false);

        return templateResolver;
    }

    public SpringTemplateEngine templateEngine() {

      SpringTemplateEngine templateEngine = new SpringTemplateEngine();
      templateEngine.setTemplateResolver(templateResolver());

      return templateEngine;
    }

    @Bean
    public ViewResolver viewResolver() {

      ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();

      viewResolver.setTemplateEngine(templateEngine());
      viewResolver.setOrder(1);
      viewResolver.setViewNames(new String[]{"*"});
      viewResolver.setCache(false);

      return viewResolver;
    }
}

应用程序上下文 XML 文件

<context:annotation-config />
<context:component-scan base-package="baseproject.controller" />

HTML 文件代码

<label for="strFirstName" th:text="#{first.name} + #{:}">First Name</label>
<input type="text" id="strFirstName" name="strFirstName" th:value="*{strFirstName}" />

说到#{first.name},我总是看到??first.name_en??。我希望能够使用多个捆绑包,例如名字 (#{first.name}) 来自 UserResources,${:} 来自 GlobalResources(因为它在整个应用程序中使用)。我来自 Struts 1.3.5,我使用的是以下标签:

<bean:message bundle="Bundle name from the struts-config.xml file)" key="first.name" />

我正在寻找使用 Spring 和 Thymeleaf 的等效项。

非常感谢您的帮助。

【问题讨论】:

  • 使用 Thymeleaf 和 Spring MVC 进行国际化是否需要任何配置?比如,我需要在我正在使用的模板上设置我的资源包吗?

标签: spring spring-mvc thymeleaf


【解决方案1】:

问题已解决。

两件事:

  1. 我必须将资源包放在我的类路径中,因此我需要更改以下代码以指向正确的位置:

    String[] strBaseNames = {
            "ca.gc.baseproject.messages.global.GlobalResources",
            "ca.gc.baseproject.messages.user.UserResources",
    };
    
  2. 以下方法缺少@Bean 注解:

    @Bean
    public SpringTemplateEngine templateEngine()
    

我也尝试过让我的资源包放在 WEB-INF 文件夹中,但没有成功。我很乐意将我的包放在类路径中,因此它们也可以在 Java 应用程序中使用。

【讨论】:

    【解决方案2】:

    将您的资源文件放入“WebContent/WEB-INF/messages/global or user”而不是“WebContent/messages/global or user”。

    希望这会有所帮助。

    【讨论】:

    • 他们已经在这里了。请参阅上面的 WebConfig.java.. 谢谢!
    【解决方案3】:

    如果仍然是实际问题: 解决方法是:

    只需删除 @EnableWebMvc 和 LocaleChangeInterceptor 就可以正常工作!

    【讨论】:

    • 这不是实际问题,原发帖人已经接受了自己的答案。
    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    相关资源
    最近更新 更多