【发布时间】: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