菜鸟学SSH(二)——Struts2国际化手动切换版

转载自:http://blog.csdn.net/liushuijinger/article/details/16834039

国际化(internationalization,i18n)和本地化(localization,l10n)指让产品(出版物,软件,硬件等)能够适应非本地环境,特别是其他的语言和文化。程序在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。


国际化原理:

国际化资源文件:用不同国家的语言描述相同的信息,并放在各自对应的.properties属性文件中,程序根据运行时环境决定加载哪个文件。
国际化主要通过以下类完成: 
java.util.Locale:对应一个特定的国家/区域、语言环境。 
java.util.ResourceBundle:用于加载一个资源包。 
I18nInterceptor:struts2所提供的国际化拦截器,负责处理Locale相关信息。

国际化流程:程序得到当前运行环境的国家/区域、语言环境并存放于Locale,ResourceBundle根据Locale中信息自动搜索对应的国际化资源文件并加载。当某个Action被执行前,I18nInterceptor负责检测Locale相关信息来寻找对应的国际化资源


先来看一下实例的效果图:


默认中文:


点击英文,页面变成英文显示:



接下来看具体的实现:


LoginAction

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.lsj.action;  
  2.   
  3. import java.util.Locale;  
  4. import org.apache.struts2.ServletActionContext;  
  5. import com.opensymphony.xwork2.ActionContext;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7.   
  8. public class LoginAction extends ActionSupport {  
  9.   
  10.     private static final long serialVersionUID = 1L;  
  11.       
  12.       
  13.       
  14.     private String flag;  
  15.     public String getFlag() {  
  16.         return flag;  
  17.     }  
  18.     public void setFlag(String flag) {  
  19.         this.flag = flag;  
  20.     }  
  21.       
  22.     public String ha() throws Exception {  
  23.         this.flag=ServletActionContext.getRequest().getParameter("flag");  
  24.                 Locale l = Locale.getDefault();     
  25.                 if(this.flag==null){     
  26.                    l = new Locale("zh""CN");     
  27.                 }else if (this.flag.equals("2")) {     
  28.                    l = new Locale("zh""CN");     
  29.                 } else if (this.flag.equals("1")) {     
  30.                    l = new Locale("en""US");     
  31.                 }      
  32.               ActionContext.getContext().setLocale(l);     
  33.               return "success";     
  34.    
  35.     }     
  36.   
  37. }  

web.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.      <filter>  
  8.         <filter-name>struts2</filter-name>  
  9.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  10.     </filter>  
  11.   
  12.     <filter-mapping>  
  13.         <filter-name>struts2</filter-name>  
  14.         <url-pattern>/*</url-pattern>  
  15.     </filter-mapping>  
  16.   <welcome-file-list>  
  17.     <welcome-file>index.jsp</welcome-file>  
  18.   </welcome-file-list>  
  19. </web-app>  

struts.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.       
  8.     <!-- 自动发布 -->  
  9.     <constant name="struts.devMode" value="true" />  
  10.     <!-- 过滤器 -->  
  11.     <constant name="struts.i18n.encoding" value="GBK"/>  
  12.     <!-- 配置i18n -->  
  13.     <constant name="struts.custom.i18n.resources" value="message"></constant>  
  14.   
  15.          
  16.    <!-- i18n控制 -->  
  17.    <package name="i18n" namespace="/"  extends="struts-default">  
  18.         <action name="i18n" class="com.lsj.action.LoginAction" method="ha">  
  19.             <result name="success">  
  20.                /i18nlogin.jsp  
  21.             </result>  
  22.             <result name="input">  
  23.                /i18nlogin.jsp  
  24.             </result>  
  25.         </action>  
  26.     </package>      
  27.   
  28. </struts>  

login.jsp

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags" %>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">      
  12.     <title>My JSP 'i18nlogin.jsp' starting page</title>      
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">   
  18.   </head>  
  19.     
  20.   <body>  
  21.   <s:i18n name="local.message">  
  22.     <s:form action="/i18n.action" method="post">  
  23.       <s:textfield name="user.userName" label="%{getText('login.name')}"/>  
  24.       <s:password name="user.userPassword" label="%{getText('login.pass')}"/>  
  25.       <s:submit value="%{getText('login.submit')}"></s:submit>  
  26.        <a href="i18n.action?flag=1">英文</a>  
  27.        <a href="i18n.action?flag=2">中文</a>  
  28.     </s:form></s:i18n>  
  29.   </body>  
  30. </html>  

需要引入的jar包:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值