您的位置:首页 > 编程语言 > Java开发

SPring MVC i18n 国际化使用流程Demon(Maven项目的搭建和web、spring框架的添加)

2015-07-29 16:45 871 查看
第一次接触国际化i18n,写了一个小demon测试中英文切换。Demon使用Spring框架,从创建Maven项目到搭建框架到业务层代码的编辑以下都记录下来了。

1.创建Maven项目--SpringMVCi18n

2.添加web 框架

property--->Macen-->Project Facets--->添加web框架
Project Facet 选择Dynamic web Module ,Java
Runtimes 选择 Apache Tomcat V7
Further configuration available 点击输入 src/main/webapp
添加 source folder , src/main/resource 、src/test/resource;
Build path-->configure build path-->Java Build path-->设置包属性
Build path-->configure build path-->Deployment Assembly-->支配程序集 依赖包
pom.xml jar-->war

<?xml version="1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
</web-app>


3.添加spring框架

添加spring依赖

根据需求添加对应的依赖
javax.servlet-api
[u]jsp-api[/u]
[u]jstl[/u]
[u]spring-core[/u]
spring-[u]aop[/u]
[u]spring-beans[/u]
spring-context
spring-context-support
spring-expression
spring-jdbc
[u]spring-test[/u]
spring-[u]tx[/u]
spring-web
spring-webmvc
[u]aopalliance[/u]

web.xml添加spring监听

<listener >
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class >
</listener >


配置spring的配置文件 applicationContext.xml (可以选择MVC另起一个配置文件)
web.xml配置文件

-------以上Maven项目构建成功------
[align=left]-------接下来测试i18n国际化-------[/align]

[align=left]4.国际化测试中英文转换[/align]

相关方面的基本知识可网上了解 /article/2002204.html
项目包层次



只测试静态国际化数据,动态需要储存在数据库。

[align=left]5.applicationContext.xml配置相应信息[/align]
<!-- 配置国际化对应信息 -->
<bean id = "messageSource"
class= "org.springframework.context.support.ResourceBundleMessageSource" >
<!-- 国际化信息所在的文件名 -->
<property name = "basenames">
<list >
<value >i18n/message </value >
</list >
</property >
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name = "useCodeAsDefaultMessage" value= "true" />
</bean >

<mvc:interceptors >
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<bean
class= "org.springframework.web.servlet.i18n.LocaleChangeInterceptor"  />
</mvc:interceptors >

<bean id = "localeResolver"
class= "org.springframework.web.servlet.i18n.SessionLocaleResolver" />


[align=left]6.Control代码[/align]
@RequestMapping ("/test" )
public ModelAndView defaultjsp (
HttpServletRequest request,
@RequestParam( value = "langType" , defaultValue = "zh" )String langType) {
ModelAndView view = new ModelAndView( "first" );

if (langType .equals ("zh" )) {
Locale locale = new Locale( "zh", "CN" );
request. getSession()
.setAttribute (
SessionLocaleResolver .LOCALE_SESSION_ATTRIBUTE_NAME ,
locale);
} else if (langType .equals ("en" )) {
Locale locale = new Locale( "en", "US" );
request. getSession()
.setAttribute (
SessionLocaleResolver .LOCALE_SESSION_ATTRIBUTE_NAME ,
locale);
} else
request. getSession(). setAttribute(
SessionLocaleResolver .LOCALE_SESSION_ATTRIBUTE_NAME ,
LocaleContextHolder .getLocale ());

// 从后台代码获取国际化信息
RequestContext requestContext = new RequestContext (request );
view. addObject( "name" , requestContext .getMessage ("name" ));
view. addObject( "address" , requestContext .getMessage ("address" ));

return view;
}


[align=left]7.jsp页面[/align]
<table border = "2">

<tr >
<td colspan = "2">< span style=" float: right" > <a
href= " <%=path %> /test?langType=en"> en</ a> | < a
href= " <%=path %> /test?langType=zh"> zh</ a>
</span ></td >
</tr >
<tr >
<td >${name } </td >
<td ></td >
</tr >

<tr >
<td >${address } </td >
<td ></td >
</tr >

</table >
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: