快速编写struts2中国际化的相关文件

来源:互联网 发布:幸福大街 知乎 编辑:程序博客网 时间:2024/04/28 00:48

 国际化

  1说明

一个系统的国际化就是根据操作系统的语言,页面上的表现形式发生相应的变化。比如如果操作系统是英文,页面的文字应该用英语,如果操作系统是中文,页面的语言应该是中文。

IE浏览器设置语言环境:

Windows系统下:

IE -->工具-->Intener选项--->语言--->设置语言首选项--->添加XX语言,就可实现添加。

  2 步骤

(1)、建立资源文件

资源文件的命名规则:

              默认的命名为:

                     文件名前缀.properties

              根据语言的命名为:

                     文件名前缀.语言种类.properties

例如:

       中文:

              resource_zh_CN.properties

 

注:

   关于资源文件中中文字符编码的转换:

  对于中文的属性文件,我们编写好后,应该使用jdk提供的native2ascii命令把文件转换为unicode编码的文件。命令的使用方式如下:

native2ascii  源文件.properties  目标文件.properties但是 Myeclipse智能可自动将中文字符转化成ASCII码。

              内容:

                     item.username=用户名

                     item.password=密码

       英文:

              resource_en_US.properties

              内容:

                     item.username=username_en

                     item.password=password_en

       默认:

              resource.properties

              内容:

                     item.username=username

                     item.password=password

(2)、 配置文件中

需要在配置文件中加入:

<constant

name="struts.custom.i18n.resources"value=

"cn.itcast.struts2.action.i18n.resource">

</constant>

说明:

1、 这样struts2就会去找你写的资源文件

2、 name的值可以在org/apache/struts2/default.properties中找到。

3、 如果放置的资源文件在src下,则value的值可以直接写,如果在

包中则可以写成包名.resource。

4、 在这里resource是个基名,也就是说可以加载以resource开头的文件。

(3)、页面中

利用<s:text name=”item.username”/>就可以把资源文件中的内容输出来。

I18n/login.jsp

<s:form action="i18n/loginAction_login.action" method="post">

                  <table border="1">

                         <tr>

                                <td><s:text name="item.username"/></td>

                                <td><s:textfield name="username"/></td>

                         </tr>

                         <tr>

                                <td><s:text name="item.password"/></td>

                                <td><s:password name="password"/></td>

                         </tr>

                         <tr>

                                <td><s:submit name="submit" value="%{getText('item.username')}"/></td>

                         </tr>

                  </table>

           </s:form>

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

说明:

1、  标红色部分的是要从资源文件中找的内容。item.username和item.password代码key的值。

2、  也可以利用%{getText('item.username')}方式来获取资源。采取的是OGNL表达式的方式。

3、  getText的来源:

从源代码可以看出ActionSupport实现了TextProvider接口。

Provides access to {@link ResourceBundle}s and their underlying text messages.意思是说提供了访问资源文件的入口。而TextProvider中提供了getText方法,根据key可以得到value

  

(4)、在action中

可以利用ActionSupport中的getText()方法获取资源文件的value值。

I18n/LoginAction

public class LoginAction extends ActionSupport{

       public String login(){

              String username = this.getText("item.username");

              System.out.println(username);

              String password = this.getText("item.password");

              System.out.println(password);

              return "";

       }

}

说明:通过this.getText()方法可以获取资源文件的值。

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 


原创粉丝点击