SpringMVC实现中英文切换(通过a便签动态切换)
这个是本人花了几小时解决的一个问题,老师要我们学习 “SpringMVC实现国际化”,我很快找到了清晰易懂的资料,但是自己运行时总是出现各种异常,下面总结以下我这次所遇到的问题(第一次发文,请轻喷)
具体实现方式有很多种,下面是我觉得最容易的方式,想要了解更多可以查看下面这篇博博文
下面直接上核心代码(不必要和我包结构一样)
1.依赖 pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
截图
2.配置 beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- spring mvc 视图解析器 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- spring 包扫描 -->
<context:component-scan base-package="cn.powerfully.demo" />
<!-- 注解驱动 默认使用基于注释的适配器和映射器 -->
<mvc:annotation-driven />
<!-- 只把动态信息当做controller处理,忽略静态信息 对于spring form表单非必需 -->
<mvc:default-servlet-handler />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 国际化资源文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 表示多语言配置文件在根路径下,以language开头的文件 -->
<property name="basename" value="classpath:language" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!-- 变量改变拦截器 -->
<!-- 配置SessionLocaleResolver用于将Locale对象存储于Session中供后续使用 -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<!-- 配置LocaleChangeInterceptor 主要用于获取请求中的locale信息,将期转为Locale对像,获取LocaleResolver对象-->
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
</beans>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>spring-form</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring配置文件beans.xml 以及 转发器 *.do 字符集过滤 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</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>
4.中英文文件
language_en_US.properties (英文)
language.cn = \u4e2d\u6587
language.en = English
internationalisation = \u0020Internationalisation
welcome = This is the English environment
introduce= This is I18N Demo
language_zh_CN.properties(中文)
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
5.controller层的 FormAction.java
@Controller
public class FormAction {
@RequestMapping("/hello.do")
public String index() {
return "hello";
}
}
6.view层的 hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
<head>
<title>SpringMVC<spring:message code="internationalisation" /></title>
</head>
<body>
Language:
<a href="?lang=zh_CN"><spring:message code="language.cn" /></a>
<a href="?lang=en_US"><spring:message code="language.en" /></a>
<h1>
<!-- 将配置文件中的内容读取 -->
<spring:message code="welcome" />
</h1>
当前语言: ${pageContext.response.locale }
</body>
</html>
注意:直接访问/hello.do
运行截图
总结
配置的时候细心一点,中英文切换其实就是以下步骤:
- 建立maven工程
- 配置web.xml
- 导入依赖(pom.xml)
- 配置beans.xml
- 编写中英文文件
- 编写controller层代码
- 编写jsp
- 测试