转: 应用Struts 2 的国际化支持特性(7)下

(4) 同样,success.jsp也使用了标签库,如代码4.10所示。

代码4.10  注册成功界面success.jsp

 

<% @ page contentType="text/html;charset=UTF-8" language="java"  %>

<% @ taglib prefix="s" uri="/struts-tags"  %>

< html >

< head >

< title >< s:text  name ="regsuccess" /></ title >

< s:head  />

</ head >

< body >

< table >

< h2 >< s:text  name ="username" /> < s:property   value ="username"   /></ h2 >

< h2 >< s:text  name ="password1" /> < s:property   value ="password1"   /></ h2 >

< h2 >< s:text  name ="birthday" /> < s:property   value ="birthday"   /></ h2 >

</ table >

</ body >

</ html >

 

上面两个JSP用户视图,所有的显示内容都使用了国际化信息,可以根据用户不同的语言与区域配置,来显示相应的国际化内容。

那么,用户视图可以访问国际化资源,在Action中可以访问吗?答案是可以的,前面已经介绍过了,Struts 2提供了一个ActionSupport工具类,开发自己的Action,只需要继承该类就可以。在该类的API文档中,可以发现,该类提供了一个getText(String aTextName)方法,该方法根据资源文件中的key值来返回一个国际化资源信息,如果找不到则为null

(5) 在本示例的Action中,会调用getText(String aTextName)方法,获得资源文件中的国际化信息,并在控制台中打印出来,如代码4.11所示。

代码4.11  国际化业务控制器reg

 

package  ch4;

import  java.util.Date;

import  com.opensymphony.xwork2.ActionSupport;

public   class  Reg  extends  ActionSupport  {

    
//定义用户名属性

    
private String username;

    
//定义处理信息:注意与http中的msg名称不同

    
private String mymsg;

    
//定义密码属性

    
private String password1;

    
//定义确认密码

    
private String password2;

    
//定义生日属性

    
private Date birthday;

    
public String execute() throws Exception {

        
//判断用户输入参数

        
if (username != null && getPassword1().equals(getPassword2())

                
&& !getUsername().trim().equals("")) {

            
//打印国际化信息

            System.out.println(getText(
"username"+ ":" + username);

            System.out.println(getText(
"password1"+ ":" + password1);

            System.out.println(getText(
"birthday"+ ":" + birthday);

            
return SUCCESS;

        }
 else {

            
return INPUT;

        }


    }


    
//getter和setter方法

    
public String getUsername() {

        
return username;

    }


    
public void setUsername(String username) {

        
this.username = username;

    }


    
public String getMymsg() {

        
return mymsg;

    }


    
public void setMymsg(String mymsg) {

        
this.mymsg = mymsg;

    }


    
public String getPassword1() {

        
return password1;

    }


    
public void setPassword1(String password1) {

        
this.password1 = password1;

    }


    
public String getPassword2() {

        
return password2;

    }


    
public void setPassword2(String password2) {

        
this.password2 = password2;

    }


    
public Date getBirthday() {

        
return birthday;

    }


    
public void setBirthday(Date birthday) {

        
this.birthday = birthday;

    }


}


 

 (6)运行该应用,在中文、英文的语言与区域配置环境中,注册界面分别如图4.7和图4.8所示。当设置语言与区域选项为“简体中文”时,Tomcat控制台会打印出中文信息:

注册用户名:pla

密码:123456

生日:Wed Nov 28 00:00:00 CST 2007

当设置语言与区域选项为“英语美国”时,Tomcat控制台会打印出英文信息:

信息: Detected AnnotationActionValidatorManager, initializing it...

Your Name:pla

Password:123456

Birthday:Sun Oct 28 00:00:00 CST 2007

          

4.7  中文注册界面        

                

  4.8  英文注册界面

分别在中文和英文配置下,输入“pla”等注册信息,单击“注册”按钮或者“Reg!”按钮,结果分别如图4.9和图4.10所示,界面显示的所有元素都使用国际化信息输出。

           

   4.9  注册成功中文界面                      

4.10  注册成功英文界面

Action中可以使用ActionSupport类提供的getText(String aTextName)方法来获得国际化信息,证实了前面所讲的国际化拦截器将用户请求参数中的Locale值存入了当前session中,Action正是获得了与Locale相关联的国际化信息资源。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值