源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

深入理解Struts2国际化信息机制

  • 时间:2021-05-28 11:08 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:深入理解Struts2国际化信息机制
这两天学习了Struts2国际化信息机制,感觉很重要,所以,今天添加一点小笔记。 [b]国际化信息机制  (三种 Action范围、 Package范围、 全局)[/b] [b]1. 全局国际化配置信息文件[/b] 全局国际化文件,对所有Action 生效,任何程序都可以访问到,需要在struts.xml 配置常量 struts.custom.i18n.resources指定信息文件 页面product.jsp
<s:fielderror/>
  <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的方法进行校验[code] ProductAction-product_add-validation.xml[/code]) [code]ProductAction-product_add-validation.xml[/code]其中[code]product_add[/code]是Struts.xml中[code]action标签中的name的值[/code]
<!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>
新建国际化信息文件 [code] src[/code]下[code] messages.properties [/code](默认的国际化文件) 注意: [img]http://files.jb51.net/file_images/article/201707/201707121006034.png[/img] 1. 其中<message key="wc"/>中的Key必须是messages.properties  的Key值 [img]http://files.jb51.net/file_images/article/201707/201707121006035.png[/img] 2.messages.properties  的value值必须装换成Unicode码, 使用myeclipse开发工具,内置properties editor 自动将中文转换 Unicode码 [b]2.  Action范围国际化文件[/b]  在Action类 所在包 创建 [code]Action类名.properties [/code](无需在struts.xml 配置 ) [img]http://files.jb51.net/file_images/article/201707/201707121006036.png[/img] [img]http://files.jb51.net/file_images/article/201707/201707121006037.png[/img] [b]3.   package范围国际化文件[/b] 在package下面 建立 [code]package.properties [/code](无需在struts.xml ) [img]http://files.jb51.net/file_images/article/201707/201707121006038.png[/img] [img]http://files.jb51.net/file_images/article/201707/201707121006039.png[/img] [b]4.   在JSP页面获取[/b] 在国际化 messages.properties 添加一个信息 [img]http://files.jb51.net/file_images/article/201707/2017071210060310.png[/img] JSP页面代码:
<h1><s:i18n name="messages">
       <s:text name="cn.wc"></s:text>
  </s:i18n></h1>
[b]5.    在Action代码获取[/b] 在messages.properties 添加国际化信息 [img]http://files.jb51.net/file_images/article/201707/2017071210060311.png[/img] 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;
    
  }
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部