SpringMVC 可以通过Spring框架来实现多语言
1. 创建SpringMVC项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--Spring 配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!--监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置转发器--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
配置Spring文件(dispatcher-servlet.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?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: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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 默认使用基于注释的适配器和映射器 --> <mvc:annotation-driven/> <!-- 只把动态信息当做controller处理,忽略静态信息 --> <mvc:default-servlet-handler/> <!-- 自动扫描包中的Controlller --> <context:component-scan base-package="controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/><!-- 前缀 --> <property name="suffix" value=".jsp"/><!-- 后缀,自动拼接 --> </bean> </beans>
2. 添加多语言的配置文件
添加language_en_US.properties到src目录下
1 2 3 4 5 language.cn = \u4e2d\u6587 language.en = English internationalisation = \u0020Internationalisation welcome = This is the English environment introduce= This is I18N Demo
添加language_zh_CN.properties到src目录下
1 2 3 4 5 language.cn = \u4e2d\u6587 language.en = English internationalisation = \u56fd\u9645\u5316 welcome = \u8fd9\u662f\u4e2d\u6587\u73af\u5883 introduce= \u8fd9\u662f\u56fd\u9645\u5316\u7684\u4e8b\u4f8b
3. 加入i18n 过滤器到配置文件中
1 2 3 4 5 6 7 @Controller public class HelloController { @RequestMapping("/hello.action") public String index() { return "hello"; } }
##效果演示 ##项目下载