Struts2 internationalization(国际化)

1:什么是国际化?

国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式。它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素。换言之,应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产。开发这样的程序的过程,就称为国际化。

2:在什么样的状况下使用国际化?

国际化是指的多语言支持,比方说你登录一个系统后,页面上的标签可以默认显示中文,或者默认显示英文。国际化在web开发中比较常用,以便于不同母语的用户使用系统。
3:为什么使用国际化?
原因:因为语言种类繁多 
4.如何国际化一个项目?
1》在struts.xml文件中配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <!-- 在struts2.xml中配置指定资源文件的基名 -->
    <constant name="struts.custom.i18n.resources" value="message"></constant>
    <!-- 指定编码方案,如果想要转换国际化 必须指定为UTF-8 默认就是u8-->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <!--  -->
    <constant name="struts.ui.theme" value="simple"></constant>
     <!-- 修改该文件,tomcat不用重启 -->
    <constant name="struts.devMode" value="true"/>
    <package name="default" namespace="/" extends="struts-default">
        <action name="loignAction" class="cn.action.languageAction">
        <result name="success">/success.jsp</result>
          <result name="input">/regist.jsp</result>
        </action>
    </package>
</struts>

 

 
 

  2》配置资源文件

  中文:message.properties

 
 
register.page=\u7528\u6237\u6CE8\u518C
register.title=\u65B0\u7528\u6237\u6CE8\u518C
name=\u59D3\u540D
password=\u5BC6\u7801
repassword=\u786E\u8BA4\u5BC6\u7801
telephone=\u7535\u8BDD\u53F7\u7801
username=\u7528\u6237\u540D
submit=\u7ACB\u5373\u6CE8\u518C

name.null=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A
 
 

  英文:message_en.properties

 
 
register.page=HouserRent-User Register
register.title=New User Register
name=Name
password=Password
repassword=RePassword
telephone=Telephone
username=UserName
submit=Register Now
name.null=Name cannot be null
 
 

  3》创建Action

 

package cn.action;



import com.opensymphony.xwork2.ActionSupport;

public class languageAction  extends ActionSupport{
    private String name;
    private String password;
    private String repassword;
    private String telephone;
    private String username;
    @Override
    public String execute() throws Exception {
        
        return SUCCESS;
    }
    
    @Override
    public void validate() {
        if(this.name==null||this.name.equals(""))
        {
          this.addActionError(this.getText("name.null"));
        }
    }

    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getRepassword() {
        return repassword;
    }
    public void setRepassword(String repassword) {
        this.repassword = repassword;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }



}

 

 

 

 
 

  4》创建jsp页面

使用ognl表达式展示国际化!<s:text>,也可以放在<s:i81n>标签中
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title><s:text name="register.page"></s:text></title>
</head>
<body>
    <s:fielderror></s:fielderror>
    <h2><s:text name="register.title"></s:text></h2>
    <s:form action="loignAction">
    <table>
    <tr>
    <td><s:text name="name" ></s:text></td>
    <td><s:textfield name="name" key="name"></s:textfield><td>
    </tr>
    <tr>
    <td><s:text name="password"></s:text></td>
    <td><s:textfield name="password"></s:textfield></td>
    </tr>
    <tr>
    <td><s:text name="repassword"></s:text></td>
    <td><s:textfield name="repassword"></s:textfield></td>
    </tr>
    <tr>
    <td><s:text name="telephone"></s:text></td>
    <td><s:textfield name="telephone"></s:textfield></td>
    </tr>
    <tr>
    <td><s:text name="username"></s:text></td>
    <td><s:textfield name="username"></s:textfield></td>
    </tr>
    <tr>
    <td colspan="2"><s:submit
    value="%{getText('submit')}"></s:submit></td>
    </tr>
    </table>
    </s:form>
</body>
</html>
 
 

5》改变浏览器的语言首选项,通过上移,下移按钮来改变!

 

 

 

 

效果:

中文

英文

 

 使用国际化验证错误

Action类中植入如下代码

    public void validate() {
        if(this.name==null||this.name.equals(""))
        {
          this.addActionError(this.getText("name.null"));
        }
    }

 

在资源文件中各自添加代码

name.null=Name cannot be null

name.null=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A

 

 

 效果:
中文错误:

英文错误:

 

 

 

转载于:https://www.cnblogs.com/jingpeipei/p/5953357.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值