zoukankan      html  css  js  c++  java
  • 四、Struts2之国际化

     

    一、国际化介绍

    Struts2国际化的目的和Java国际化一致,对于浏览器支持的语言显示不同的文字,如果浏览器支持中文,则显示中文,如果浏览器支持英文,则显示英文;


    国际化的目的:

     

    很多人刚开始学国际化觉得国际化没有用,因为我们通常都只需要显示中文即可;但是比如google,全世界的人都在访问此网站,这时如果只显示中文网页肯定是不合理的。因此需要国际化;

     

    Eclipse for JavaEE和MyEclipse在国际化的优劣:

     

    一般来说,资源文件的中文都需要通过native2ascii.exe进行转换;

    Eclipse for JavaEE需要手动转换;

    而MyEclipse可以自动转换,只需要输入中文即可;


    二、浏览器中修改语言

     

     

    三、资源文件介绍

    国际化资源文件都是通过Xxx_language_Country.properties 形式;

    比如: message_zh_CN.properties、message_en_US.properties;

    如果浏览器支持中文,则会加载第一个资源文件;

    注意:在资源文件中不支持中文,因此需要通过JDK\bin中的native2ascii.exe进行转换;

    在资源文件中内容是以key=value的形式存放的,比如name=xiazdong;

     

    四、Struts2国际化

     

     

    在Struts2中,在JSP页面和Action类中都可以使用国际化;

    国际化文件分为:

    1.全局国际化资源文件:对于所有Action、JSP都有效,放在WEB-INF\classes下;

    2.包范围国际化资源文件:对于在此包中的Action都有效,放在包路径下;

    3.Action范围国际化资源文件:对于特定的Action有效,放在Action类的同目录下;

    4.临时指定国际化资源文件:需要特定语法进行指定才有效,放在WEB-INF\classes下; 

    1.全局国际化资源文件

    此文件适用于Action和JSP;

    如果需要使用全局资源文件,则需要在struts.xml中添加常量:

    <constant name="struts.custom.i18n.resources" value="basename" />

    basename是指资源文件的基础名,比如message_zh_CN.properties的basename是message;

    message_zh_CN.properties

    title=\u9875\u9762\u6807\u9898
    body=\u9875\u9762\u5934
    button=\u70b9\u51fb
    name=\u590f\u6b63\u51ac

    message_en_US.properties

    title=Page title
    body=Page body
    button=click
    name=xiazdong

    1.1在JSP中使用国际化

    1.1.1一般文本国际化

    通过<s:text name="title"/> 可以根据浏览器支持的语言取出全局国际化文件的key为title对应的value;

    1.1.2表单控件的国际化

    对于表单中的控件,比如按钮中的文字、textfield前的描述文字,通过指定key属性进行国际化;

    比如<s:textfield key="username"/> 可以在文本域前面的描述中填入username对应的value;

    示例代码:

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
       <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title><s:text name="title"/></title>
    </head>
    <body>
    	<s:text name="body"/>
    	<s:form action="aaaa">
    		<s:textfield name="name" key="name"></s:textfield>
    	</s:form>
    </body>
    </html>

    由于本人的浏览器默认为中文,因此显示的是中文的资源文件;

    1.2在Action中使用国际化

    在Action中可以使用 getText(String key);这个函数返回国际化文件中key对应的value;

    示例代码:

    package org.locale.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class Demo01Action extends ActionSupport{
    	public String execute()throws Exception{
    		System.out.println(getText("title"));
    		System.out.println(getText("body"));
    		System.out.println(getText("button"));
    		return SUCCESS;
    	}
    }
    


    1.3 占位符

     

     

    在资源文件中可以使用占位符,占位符是指在资源文件中留出一个位置,在使用时动态插入一个任意值;比如在登陆界面时,如果登陆的用户名是xiazdong,则欢迎界面就会出现xiazdong,欢迎你! 在资源文件中通过 {0},欢迎你!!! 实现;

    message_zh_CN.properties

    title=\u9875\u9762\u6807\u9898
    body={0}\u9875\u9762\u5934{1}
    button=\u70b9\u51fb
    name=\u590f\u6b63\u51ac

     

    1.3.1在JSP中填充占位符

     

    在JSP中通过在<s:text>中添加<s:param>进行填充;

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
       <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title><s:text name="title"/></title>
    </head>
    <body>
    	<s:text name="body">
    		<s:param>xiazdong</s:param>
    		<s:param>xiazdong</s:param>
    	</s:text>
    </body>
    </html>

     

    1.3.2在Action中填充占位符

    在Action中通过 getText(String key, String[] params); 第二个参数进行填充;

    package org.locale.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class Demo02Action extends ActionSupport{
    	public String execute()throws Exception{
    		System.out.println(getText("body",new String[]{"xiazdong","xiazdong"}));
    		return SUCCESS;
    	}
    }
    


    2.包范围资源文件

    如果一个工程全部的资源文件都在全局国际化资源文件中配置,会很凌乱,因此我们需要进行模块化,包范围的资源文件是在包下平进行配置,对于包中所有Action都有效;

    包范围的资源文件只对Action有效,而JSP不能突显;

    包范围的资源文件的名称形式为: package_language_country.properties, 注意 package是常量,比如 package_zh_CN.properties, package_en_US.properties;

    如果在包下放置 包范围资源文件,并且配置全局资源文件,在Action类代码不变的情况下,则Action类优先加载包范围的资源文件;

    比如目录结构如图所示:

    则在 org.locale.action包下的Action都优先加载package_****.properties;

     

    3.Action范围资源文件

     

     

    Action范围的资源文件是针对某个特定的Action的,

    Action范围的资源文件的形式: ActionName_language_country.properties; 比如 LoginAction类的资源文件为:LoginAction_zh_CN.properties;

    Action范围的资源文件优先级比包范围的资源文件优先级高。

    比如目录结构如图所示:

    则Demo04Action优先加载Demo04Action_****.properties;

     

     

    4.利用标签指定资源文件

     

     

    此种方式针对JSP加载指定的资源文件,即可以加载任意范围的资源文件;

    <s:i18n>是指定资源文件的标签;

    在<s:text>等加载资源文件的外面加上<s:i18n name="filename">标签; filename指定加载的资源文件的名称;

    比如:

    	<s:i18n name="message">
    	<s:text name="body"/>
    	
    	<s:form action="aaaa">
    		<s:textfield name="name" key="name"></s:textfield>
    	</s:form>
    	</s:i18n>


    就可以加载message_**_**.properties 全局资源文件;

    目录结构如下:

    接下去将分别在JSP中加载全局范围、包范围、Action范围的资源文件;

    4.1 加载全局范围的资源文件

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
       <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title><s:text name="title"/></title>
    </head>
    <body>
    	<s:i18n name="message">
    	<s:text name="body"/>
    	
    	<s:form action="aaaa">
    		<s:textfield name="name" key="name"></s:textfield>
    	</s:form>
    	</s:i18n>
    </body>
    </html>


    4.2 加载包范围的资源文件

    加载包范围的资源文件是,<s:i18n>的name属性的值为 包1/包2/package的形式,比如上图包层次为 org.locale.action,则为 org/locale/action/package ;

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
       <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title><s:text name="title"/></title>
    </head>
    <body>
    	<s:i18n name="org/locale/action/package">
    	<s:text name="body"/>
    	
    	<s:form action="aaaa">
    		<s:textfield name="name" key="name"></s:textfield>
    	</s:form>
    	</s:i18n>
    </body>
    </html>


    4.3加载action范围的资源文件

    加载Action范围的资源文件时,<s:i18n>的name属性值的形式为: 包层次/ActionName,比如 org/locale/action/Demo04Action 则加载Demo04Action的资源文件;

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
       <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title><s:text name="title"/></title>
    </head>
    <body>
    	<s:i18n name="org/locale/action/Demo04Action">
    	<s:text name="body"/>
    	
    	<s:form action="aaaa">
    		<s:textfield name="name" key="name"></s:textfield>
    	</s:form>
    	</s:i18n>
    </body>
    </html>


     

    此文章的源代码下载地址:http://download.csdn.net/detail/xiazdong/4032669

    作者:xiazdong
    出处:http://blog.xiazdong.info
    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    Eloquent ORM模型中添加自定义值
    wget
    带坑的几道PHP面试题
    php字符型转整型
    SELECT 1
    GD库
    Windows下.svn文件夹的最简易删除方法(附linux)
    svn 撤销修改
    mysql应用基本操作语句(转)
    i春秋broken
  • 原文地址:https://www.cnblogs.com/xiazdong/p/3058090.html
Copyright © 2011-2022 走看看