springMVC国际化
程序员文章站
2022-05-24 14:46:26
...
1.pom.xml
<profiles>
<!-- JDK:JRE转化版本 -->
<profile>
<id>development</id>
<activation>
<jdk>1.8</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<!-- 统一属性设置 -->
<properties>
<!-- 字符编码配置 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- springMVC相关依赖版本 -->
<spring.version>5.0.7.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--springMVC相关依赖-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
2.web.xml
<!-- POST防止中文乱码 -->
<filter> <!--注意这里是filter,不要配置成servlet -->
<filter-name>CharactorFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping> <!--过滤器映射 -->
<filter-name>CharactorFilter</filter-name><!--过滤器名称 -->
<url-pattern>/*</url-pattern><!--URL映射,给所有页面处理乱码 -->
</filter-mapping>
<!-- struts2 是通过过滤器对请求进行处理 springMVC通过servlet对所有的请求进行处理 servlet也称为前端控制器、*控制器
1、servlet比过滤器效率高 2、好管理 -->
<!-- Spring -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置spring的bean配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC_beans.xml</param-value>
</init-param>
<!-- 加载顺序 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 前端控制器的映射路径配置,由于前端控制器是处理所有请求的,所以,此处路径配置为根目录 -->
<!-- "/*":根目录底下所有的文件 "/":根目录下所有的资源都能处理,除了.jsp. .js .jpg.... .jsp管不到,不经过前端控制器
*.do: 只处理.do文件 -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.springMVC_beans.xml
<!-- 扫描注解包 -->
<context:component-scan base-package="com.xalo.controller"></context:component-scan>
<!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!-- 配置国际化的资源 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 表示多语言配置文件在根路径下,以language开头的文件 -->
<property name="basename" value="language"/>
<property name="useCodeAsDefaultMessage" value="true"/>
<!-- 防中文乱码 -->
<property name="cacheSeconds" value="0" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!-- 处理国际化的拦截器 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
<mvc:interceptors>
<ref bean="localeChangeInterceptor"/>
</mvc:interceptors>
<!-- 界面解析器配置 根据逻辑视图名称匹配到物理视图. 例如:逻辑视图名称为 hello 逻辑视图拼接上前缀后缀 /hello.jsp -->
<bean id="viewParse" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property> <!-- 前缀 -->
<property name="suffix" value=".jsp"></property> <!-- 后缀 -->
</bean>
4.language_en_US.properties
title=login
name=name
password=password
user.age=must be greater than or equal to 4 //年龄必须大于4岁
5.language_zh_CN_properties
title=\u767B\u5F55
name=\u59D3\u540D
password=\u5BC6\u7801
user.age=\u5E74\u9F84\u5FC5\u987B\u5927\u4E8E4\u5C81
6.LoginController.java
@Controller
public class LoginController {
//国际化界面
@RequestMapping("/index")
public String index(){
return "index";
}
}
7.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- spring提供的标签库 -->
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>国际化</title>
<meta charset=UTF-8/>
</head>
<body>
<spring:message code="title"></spring:message><br>
<spring:message code="name"></spring:message><br>
<spring:message code="password"></spring:message><br>
<spring:message code="user.age"></spring:message><br>
<a href="index?lang=zh_CN">chinese</a>
<a href="index?lang=en_US">english</a>
</body>
</html>
8.http://localhost:8080/MavenSpringMVC/index?user.age=2
姓名
密码
年龄必须大于4岁
chinese english
login
name
password
must be greater than or equal to 4
chinese english
上一篇: Spring之自动装配
下一篇: js判断手机端并跳转