我想使用Thymeleaf在Spring MVC应用程序中使用多个资源包。我无法访问
项目结构(EAR)
MyProjectWeb
的src
的WebContent
WEB-INF
消息
视图
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 ??。我希望能够使用多个bundle,比如名字(#{first.name})来自UserResources,而$ {:}来自GlobalResources(因为它在整个应用程序中使用)。我来自Struts 1.3.5,我使用以下标签:
<bean:message bundle="Bundle name from the struts-config.xml file)" key="first.name" />
我正在寻找使用Spring和Thymeleaf的等效产品。
非常感谢您的帮助。
答案 0 :(得分:1)
将您的资源文件放入&#34; WebContent / WEB-INF / messages / global或user&#34;而不是&#34; WebContent / messages / global或user&#34;。
希望这有帮助。
答案 1 :(得分:1)
问题已解决。
两件事:
我必须将我的资源包放在我的类路径中,所以我需要更改以下代码以指向正确的位置:
String[] strBaseNames = {
"ca.gc.baseproject.messages.global.GlobalResources",
"ca.gc.baseproject.messages.user.UserResources",
};
在以下方法中缺少@Bean注释:
@Bean
public SpringTemplateEngine templateEngine()
我还尝试将我的资源包放在WEB-INF文件夹中,但没有成功。我很乐意将我的bundle放在类路径中,因此它们也可以在Java应用程序中使用。
答案 2 :(得分:0)
如果仍然是实际问题: 修复是:
只需删除@EnableWebMvc,LocaleChangeInterceptor就可以了!