Struts国际化实例

拿中文、英文为例:首先建properties文件。test_en_US.properties、test_zh_CN.properties
其中test可以自己定义,(_en_US.properties)这些是固定的。
在test_zh_CN.properties写:(这里需要注意的是在打开的时候需要右键openwith PropertiesEditor,不然会乱码。这个插件的安装步骤
student.name=李雷//在properties里面是以key和value保存的。
test_en_US.properties写:
student.name=LiLei
写一个测试类。

public class I18NTest {
  public static void main(String[] args) {
      //ResourceBundle.getBundle(baseName, locale);baseName指代的是test_en_US_properties.
      ResourceBundle rb= ResourceBundle.getBundle("test", Locale.CHINA);
      System.out.println(rb.getString("student.name"));//获取value值
 }
}

struts国际化。这2个file文件命名是固定的,不能随意更改。

package_zh_CN.properties:
login.username=用户名
login.password=密码
login.login=提交
package_en_US.properties:
login.username=username
login.password=password
login.login=login

action:


public class LoginAction extends ActionSupport {
    private String username;
    private String password;
     public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String execute(){

         return "success";
     }
}

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="login" namespace="/login" extends="struts-default" >

       <action name="loginAction" class="com.i18n.action.LoginAction">
            <result>/index.jsp</result>
       </action>


    </package>


</struts>

index.jsp:

<form action="" method="post">
   //获取properties里面的value值:
   <s:property value="getText('login.username')"/> <input type="text" name="username">
   <s:property value="getText('login.password')"/> <input type="tpassword" name="password">
    <input type="submit" value="提交">
  <a href="login/loginAction?request_locale=en_US">英语</a>
  <a href="login/loginAction?request_locale=zh_CN">汉语</a>
   </form>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值