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

    步骤
        1)加载国际化资源文件:|--Action以及包范围内国际化资源文件自动加载
                                               |--JSP、全局范围的国际化资源文件,分别使用标签、配置常量方式手动加载
        2)输出国际化:|--视图上:使用Struts2标签库
                                  |--Action类中:使用ActionSupport的getText()方法完成
     
    加载资源文件的顺序:
        1Action中:    类文件->父类文件->实现的接口所在文件->实现的ModelDriven,找到getModel()方法对应的model对象重复前面几步操作->baseName为package的系列资源文件->沿当前包上溯,直到顶层包查找baseName为package的系列资源文件->查找struts.custom.i18n.resources常量指定的baseName的系列资源文件->没有找到,直接输出该key的字符串值。
        2JSP中:       1) 对于使用<s:i18n../>标签作为父标签的<s:text../>标签、表单标签:
                                            <s:i18n../>指定的资源文件->struts.custom.i18n.resources指定的资源文件->没有找到,直接                                             输出该key的字符串值。
                            2)对于没有使用<s:i18n../>标签的:
                                              struts.custom.i18n.resources指定的资源文件->没有找到,直接输出该key的字符串值。

    JSP视图页面:
    资源文件在WEB-INFsrcviewResources下,资源文件的baseName为loginForm
    <s:i18n name="viewResources.loginForm">
    <html>
    <head>
        <meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
        <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
        <!-- 使用text标签来输出国际化消息 -->
        <title><s:text name="loginPage"/></title>
    </head>
    <body>
        <s:form action="login">
            <!-- 表单标签中使用key属性来输出国际化消息 -->
            <s:textfield name="username" key="user"/>
            <s:textfield name="password" key="pass"/>
            <s:submit key="login"/>
        </s:form>
    </body>
    </html>
    </s:i18n>


    Action范围内的国际化:

        在Action所在路径建立ActionName_language_country.properties文件即可
       
      Action范围内的国际化资源消息可以通过下面三种方式使用:
    1. JSP页面中输出国际化消息,可以使用struts2的<s:text../>标签,该标签可以指定一个name属性,该属性的值为国际化资源文件中的key。
    2. 如果想在表单元素的label中输出国际化信息,可以为该表单标签指定一个key值,改属性的值为国际化资源文件中的key。
    3. Action类中可以使用ActionSupport类的getText()方法,该方法可以接受一个name参数,该参数指定国际化资源文件中的key。
        
    public String execute() throws Exception
        {
            ActionContext ctx = ActionContext.getContext();
            if (getUsername().equals("crazyit.org")
                && getPassword().equals("leegang"))
            {
                ctx.getSession().put("user" , getUsername());
                // 获取国际化消息
                ctx.put("tip" , getText("succTip"));
                return SUCCESS;
            }
            // 获取国际化消息
            ctx.put("tip" , getText("failTip"));
            return ERROR;
        }

    如果需要在Action校验规则文件中使用Action范围的国际化消息,可通过<message../>元素指定key属性来实现。
    <?xml version="1.0" encoding="GBK"?>
    <!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
    <validators>
        <!-- 校验Action的name属性 -->
        <field name="username">
            <!-- 指定name属性必须满足必填规则 -->
            <field-validator type="requiredstring">
                <param name="trim">true</param>
                <message key="username.required"/>
            </field-validator>
        </field>
    </validators>

     


    包范围内使用国际化资源:
           与Action范围的国际化资源文件功能相似,区别是:包范围内的可以 被该包下所有Action使用。
            文件名为package_<language>_<country>.properties
           
            
         当Action范围的和包范围的资源文件同时存在时,系统优先使用Action范围内的。推荐尽量使用Action范围的国际化资源消息,以提高更好的维护性。

    全局范围内使用国际化资源:
        struts.properties文件中
     
      struts.custom.i18n.resources=messageResource
        struts.xml中:
     
      <constant name="struts.custom.i18n.resources" value="messageResource"/>
    (注:messageResource为国际化资源文件的baseName)
        
      实际项目中不推荐把JSP页面、Action、Action 校验文件中的国际化消息放到全局国际化资源文件中管理,因为这样会导致全局国际化资源文件爆炸式增长,难以维护。
    输出带有占位符的国际化消息:
     
    JSP页面中:
    <s:i18n name="viewResources.result">
    <html>
    <head>
        <meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
        <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
        <title><s:text name="resultPage"/></title>
    </head>
    <body>
        <jsp:useBean id="d" class="java.util.Date" scope="page"/>
        ${requestScope.tip}<br/>
        <!-- 输入带占位符的国际化消息 -->
        <s:text name="welcomeMsg">
            <s:param><s:property value="username"/></s:param>
            <s:param>${d}</s:param>
        </s:text>
    </body>
    </html>
    </s:i18n>
        
    Action中:
    public String execute() throws Exception
        {
            ActionContext ctx = ActionContext.getContext();
            if (getUsername().equals("crazyit.org")
                && getPassword().equals("leegang"))
            {
                ctx.getSession().put("user" , getUsername());
                // 获取国际化消息
                ctx.put("tip" , getText("succTip" , new String[]{username}));
                return SUCCESS;
            }
            // 获取国际化消息
            ctx.put("tip" , getText("failTip" , new String[]{username}));
            return ERROR;
        }
     

    Struts2还提供了对占位符的一种替代方式,直接在国际化消息中使用表达式:
        failTip=${username},对不起,您不能登录!
        succTip=${username},欢迎,您已经登录!


  • 相关阅读:
    Go Revel
    Go Revel
    Go Revel
    Go Revel
    deployment:声明式的升级应用
    Kubernetes架构及相关服务详解
    Docker 安装MySQL
    日志收集-Elk6
    Jenkins-Multijob plugin多任务串并行
    ansible创建vmware虚拟机
  • 原文地址:https://www.cnblogs.com/goingforward/p/5729946.html
Copyright © 2011-2022 走看看