zoukankan      html  css  js  c++  java
  • Struts2国际化信息机制

     国际化信息机制  (三种 Action范围、 Package范围、 全局)

    1. 全局国际化配置信息文件

    全局国际化文件,对所有Action 生效,任何程序都可以访问到,需要在struts.xml 配置常量 struts.custom.i18n.resources指定信息文件

    页面product.jsp

    <span style="color: red"><s:fielderror/></span>
        <form action="${pageContext.request.contextPath }/product_add.action" method="post">
            商品名:<input type="text" name="name"/><br/>
            价格:<input type="password" name="price"/><br/>
            <input type="submit" value="登录"/>
        </form>

    编写ProductAction

    public class ProductAction extends ActionSupport {
        private static final long serialVersionUID = 1L;
        private String name;
        private double price;
        public String add(){
            System.out.println(name+"---------"+price);
            return SUCCESS;
                /*
    
                         get(),set()方法略去.................
    
                 */
            
        }
    }

    添加校验信息:(对Action的方法进行校验 ProductAction-product_add-validation.xml

    ProductAction-product_add-validation.xml其中product_add是Struts.xml中action标签中的name的值

    <!DOCTYPE validators PUBLIC
              "-//Apache Struts//XWork Validator 1.0.3//EN"
              "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
     <validators>
     <!-- 校验商品 -->
       <field name="name">
         <field-validator type="requiredstring">
           <message key="wc"/>
         </field-validator>
       </field>
     </validators>

    新建国际化信息文件  src messages.properties (默认的国际化文件)

    注意:

    1. 其中<message key="wc"/>中的Key必须是messages.properties  的Key值

    2.messages.properties  的value值必须装换成Unicode码, 使用myeclipse开发工具,内置properties editor 自动将中文转换 Unicode码

    2.  Action范围国际化文件

    在Action类 所在包 创建 Action类名.properties (无需在struts.xml 配置 )

    3.   package范围国际化文件

    在package下面 建立 package.properties (无需在struts.xml )

    4.   在JSP页面获取

    在国际化 messages.properties 添加一个信息

    JSP页面代码:

    <h1><s:i18n name="messages">
                  <s:text name="cn.wc"></s:text>
        </s:i18n></h1>

    5.    在Action代码获取

    在messages.properties 添加国际化信息

    Action转发的页面JSP

     <s:text name="welcome">
           <s:param>lxp</s:param>
         </s:text>

    Action代码:

    public class Product2Action extends ActionSupport {
        private static final long serialVersionUID = 1L;
        public String add(){
            System.out.println(this.getText("welcome",new String[]{"Action"}));
            return SUCCESS;
            
        }
    }
  • 相关阅读:
    软工小白菜的团队介绍和采访
    团队作业第二次——团队Github实战训练
    团队作业第一次—团队展示和项目展示
    week5:Technology: Internets and Packets
    week3:History: The Web Makes it Easy to Use
    week2:History: The First Internet
    week4:History: Commercialization and Growth
    week1:History: Dawn of Electronic Computing
    第二日会议博客
    第一日会议博客
  • 原文地址:https://www.cnblogs.com/lxp503238/p/7151842.html
Copyright © 2011-2022 走看看