国际化资源命名规范 基名_语言_地区.properties
地区可省 基名_语言.properties
springmvc需要使用国际化需要导入二个jar包jstl 和standard
<!-- 国际化需要的二个jar包 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
创建二个区域文件
i18n_en_US.properties
welcomelog=Welcome to the login page
usernamelog=username
passwordlog=password
loginlog=login
i18n_zh_CN.properties
welcomelog=欢迎来到登录页面
usernamelog=用户名
passwordlog=密码
loginlog=登录
项目架构
applicationContext-springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.stone.mvc"/>
<!-- 配置视图解析器 InternalResourceViewResolver-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix配置前置 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- suffix配置后置 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- springmvc 高级注解驱动 -->
<!-- 使用mvc注解声明 -->
<mvc:default-servlet-handler/>
<!-- 处理静态资源(js,css,html) -->
<mvc:annotation-driven/>
<!-- 加载国际怀资源文件 id名必须为messageSource 因为springmvc在启动时会自动加载名为messageSource的bean -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 配置基名 -->
<property name="basename" value="i18n.i18n"></property>
</bean>
</beans>
Myi18n 类
@Controller
public class Myi18n
{
// 自动注入ResourceBundleMessageSource类型的值
@Autowired
private MessageSource messageSource;
@RequestMapping(value = "i18Test")
public String i18Test(Locale locale){
System.out.println("区域信息:"+locale);
// getMessage(key,占位符数组,区域信息)
String welcomelog = messageSource.getMessage("welcomelog", null, locale);
System.out.println(welcomelog);
System.out.println("i18Test方法运行");
return "i18n";
}
}
首页 index.jsp
i18n.jsp 跳转页
中文语言下
英文语言下
方法打印信息