M.R*_*R.M 9 spring-mvc internationalization thymeleaf spring-boot
我试图在spring mvc 4中使用spring boot创建一个简单的国际化示例,但它不起作用.这是我的网络应用程序结构
这是我的java配置:
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
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
public class WebConfiguration extends WebMvcConfigurerAdapter
{
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
Locale defaultLocale = new Locale("en_US");
localeResolver.setDefaultLocale(defaultLocale);
return localeResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new
LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
Run Code Online (Sandbox Code Playgroud)
我的Thymeleaf页面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport"
content="width=device-width, initialscale=1, maximum-scale=1.0, user-scalable=no" />
<title>Home</title>
<!-- CSS Links -->
</head>
<body>
<div class="container">
<div class="row">
<p th:text="#{welcome}">Hello</p>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我在两个消息属性文件中定义了欢迎消息,并将以下内容放在application.properties文件中:
spring.messages.basename=messages
spring.messages.encoding=UTF-8
Run Code Online (Sandbox Code Playgroud)
当我点击localhost:8080/home时,它会打印出来 - welcome_en_us ?? 我不知道我错过了什么.我发现有些人将基本名称写成"classpath:messages",这是什么意思?我的意思是不是我的项目结构正确,应该将属性文件放在别处吗?
| 归档时间: |
|
| 查看次数: |
3897 次 |
| 最近记录: |