Struts2国际化

来源:互联网 发布:增值税打印软件 编辑:程序博客网 时间:2024/06/08 16:24
资源文件命名规范
baseName_language_country.properties//全局范围的命名规则
Hello_en_US.properties
Hello_zh_CN.properties

package_language_country.properties//包内的命名规则
package_en_US.properties
package_zh_CN.properties

ActionClassName_language_country.properties//只对Action有效的的命名规则
HelloWordAction_en_US.properties
HelloWordAction_zh_CN.properties

系统查找顺序为 ActionClassName_language_country.properties——》package_language_country.properties——》baseName_language_country.properties
        即从 Action——》包内的——》全局范围的
如果当前包内没有,则往上层包寻找



例子代码
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>
   
    <!-- 引用全局资源文件 -->
    <constant name="struts.custom.i18n.resources" value="Hello"/>
    <package name="default" namespace="/test" extends="struts-default">

       
        <action name="country" class="com.structs2.HelloWordAction"
            method="execute">
            <result name="success" >/hello.jsp</result>
           
        </action>
       
       
    </package>
</struts>

HelloWordAction

package com.structs2;



import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWordAction extends ActionSupport {
   

   

   
    private static final long serialVersionUID = 1L;

    public String execute() {
    String message =this.getText("welcome");//是ActionSupport类中的方法,welcome为资源文件的key值
        ActionContext.getContext().put("message", message);
       
        //往占位符里输入值
        String msg =this.getText("welcome",new String[]{"nnnnnn","mmmmmmmm"});
       
        ActionContext.getContext().put("msg", msg);
        return "success";
    }

   

}

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>国际化</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
  <h2 align="left"> 全局范围</h2><br/>
  <s:text name="welcome"></s:text><!-- 页面中访问国际化问题welcome是资源文件中的key值 -->
    <form action="test/country">
    <input type="submit" value="跳转">
    </form>
 
 
<h2 align="left"> 全局范围往占位符里输入值</h2><br/>
 <s:text name="welcome">
 <s:param>你好</s:param><!-- 往占位符里输入值 -->
 <s:param>大家好</s:param>
 </s:text>
 
    <h2 align="left">包范围</h2><br/>
 <s:i18n name="com/structs2/package"><!-- 访问包范围的资源方法,package是固定的,前边的是包名 -->
  <s:text name="welcome"></s:text><!-- 页面中访问国际化问题welcome是资源文件中的key值 -->
 </s:i18n>
 
  <h2 align="left">Action范围</h2><br/>
 <s:i18n name="com/structs2/HelloWordAction"><!-- 访问包范围的资源方法,HelloWordAction是Action名字,前边的是包名 -->
  <s:text name="welcome"></s:text><!-- 页面中访问国际化问题welcome是资源文件中的key值 -->
 </s:i18n>
  </body>
</html>

hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'hello.jsp' starting page</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
   ${message }<br/>
 <h1>插入占位符后的内容</h1><br/>
    ${msg }<br/>
  </body>
</html>