关于struts国际化的一点尝试

好久没发过blog了,其实也不是忙,感觉就是没心情。今天上午忙了一上午,被msn骗子给搞死了,到中午那会终于搞定了。下午就做了一个struts国际化的小程序,其实做struts也有快半年了,但感觉一直是只见树木不见林,决定从今天开始好好补课,不然真的要成为流水线的工人了。
下面的例子如有错误,还请各位斧正。
1、修改web.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
           <param-name>application</param-name>
           <param-value>com.sharp.struts.ApplicationResources</param-value>
         </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

文件中灰色背景部分为国际化需要增加的内容,其中com.sharp.struts.ApplicationResources跟你的WEB-INF目录下classes下的目录结构是一致的,如下图所示

2、建立资源文件ApplicationResources.source(中文源文件,而不是中文资源文件)、ApplicationResources_en.properties(英文资源文件)。
首先建立ApplicationResources_en.properties文件,内容如下:
owner.greet=Greet
owner.name=Name
owner.address=Address
owner.email=Email
owner.tel=Tel

对应的中文源文件内容如下:
owner.greet=称呼
owner.name=姓名
owner.address=地址
owner.email=电子邮件
owner.tel=联系电话

然后通过在cmd中执行native2ascii -encoding GBK ApplicationResources.source .\ApplicationResources_zh_CN.properties
命令将中文源文件生成UTF-8格式的中文资源文件,内容如下
owner.greet=\u79f0\u547c
owner.name=\u59d3\u540d
owner.address=\u5730\u5740
owner.email=\u7535\u5b50\u90ae\u4ef6
owner.tel=\u8054\u7cfb\u7535\u8bdd
这里介绍一下,native2ascii是JDK包自带的一个工具,如果你不能cmd直接执行native2ascii命令,请检查你的环境变量,这里不具体介绍了。

3、接着来介绍一下struts-config.xml文件的内容,如果熟悉struts的朋友也觉得很简单的
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    <!-- ========== Form Bean Definitions ================================== -->
    <form-beans>
        <form-bean name="ownerForm" type="com.sharp.struts.form.OwnerForm">
            <form-property name="Email" type="java.lang.String" />
            <form-property name="Greet" type="java.lang.String" initial="Mr." />
            <form-property name="Address" type="java.lang.String" />
            <form-property name="Tel" type="java.lang.Integer" initial="0" />
            <form-property name="Name" type="java.lang.String" />
        </form-bean>
       
    </form-beans>
   
    <!-- ========== Global Exception Definitions ============================== -->
    <global-exceptions />
   
    <!-- ========== Global Forward Definitions =============================== -->
    <global-forwards>
        <forward name="failure" path="/failure.jsp" />
       
    </global-forwards>
   
    <!-- ========== Action Mapping Definitions =============================== -->
    <action-mappings>
        <action
            attribute="ownerForm"
            input="/owner.jsp"
            name="ownerForm"
            path="/owner"
            type="com.sharp.struts.action.OwnerAction"
            parameter="formAction"><!--因为action类继承DispatchAction类,所以需要设置这个属性-->
            <forward name="owner"   path="/owner.jsp" />
            <forward name="success" path="/success.jsp" />
        </action>

       
    </action-mappings>
   
</struts-config>

4、现在就开始准备java和jsp文件吧
owner.jsp文件内容如下:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html locale="true">
 <head>
  <meta name = "Generator" content = "Easy Struts Xslt generator for Eclipse (http://easystruts.sf.net).">
  <title>Struts Form for ownerForm</title>
  <script language="javascript">
      function changeLanguage(language) {
          if(language == "en") {
              document.all("language").value = "en";
          } else {
              document.all("language").value = "cn";
          }
          document.forms[0].submit();
      }
  </script>
 </head>
 <body>
  <html:form action="/owner">
  <input type="hidden" name="formAction" value="changeLanguage">
  <input type="hidden" name="language">

   <bean:message key="owner.greet" /> : <html:select property="greet">
      <html:option value=""></html:option>
      <html:option value="Mr.">Mr.</html:option>
      <html:option value="Miss.">Miss.</html:option>
      <html:option value="Mrs.">Mrs.</html:option>
     </html:select><html:errors property="greet"/></br>   
   <bean:message key="owner.name" /> : <html:text property="name"/><html:errors property="name"/></br>
   <bean:message key="owner.address" /> : <html:text property="address"/><html:errors property="address"/></br>
   <bean:message key="owner.email" /> : <html:text property="email"/><html:errors property="email"/></br>
   <bean:message key="owner.tel" /> : <html:text property="tel"/><html:errors property="tel"/></br>
   <html:submit/>
   <html:cancel/>
   <input type="button" name="langEn" value="英文版" onclick="changeLanguage('en')">   
   <input type="button" name="langCn" value="中文版" onclick="changeLanguage('cn')">
  </html:form>
 <body>
</html:html>

OwnerAction.java文件内容如下:
// Created by Xslt generator for Eclipse.
// XSL :  not found (java.io.FileNotFoundException:  (Bad file descriptor))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.sharp.struts.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.sharp.struts.form.OwnerForm;

/**
 * OwnerAction.java created by EasyStruts - XsltGen.
 *
http://easystruts.sf.net
 * created on 10-09-2004
 *
 * XDoclet definition:
 * @struts:action path="/owner" name="ownerForm" input="/owner.jsp" validate="true"
 * @struts:action-forward name="/success.jsp" path="/success.jsp"
 */
public class OwnerAction extends DispatchAction {

 
 public ActionForward changeLanguage(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) {

   String language = request.getParameter("language");   
   if (language.equals("en")) {
        request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH);    
   } else {    
        request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.CHINESE);
   }
   
        return mapping.findForward("owner");
    }
}
这里需要注意一下,千万不要弄错,我在这里就花费了一个多小时,因为把文件名写错了
Locale.ENGLISH是一个Locale对象,getLanguage()方法返回en,跟资源文件(ApplicationResources_en.properties)的后缀相同
Locale.CHINESE是一个Locale对象,getLanguage()方法返回zh,跟资源文件(ApplicationResources_zh_CN.properties)的后缀相同


另外还用到一个ownerForm.java文件,内容如下:
// Created by Xslt generator for Eclipse.
// XSL :  not found (java.io.FileNotFoundException:  (Bad file descriptor))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.sharp.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * OwnerForm.java created by EasyStruts - XsltGen.
 * http://easystruts.sf.net
 * created on 10-09-2004
 *
 * XDoclet definition:
 * @struts:form name="ownerForm"
 */
public class OwnerForm extends ActionForm {

 // --------------------------------------------------------- Instance Variables

 /** Email property */
 private String email;

 /** Greet property */
 private String greet = "Mr.";

 /** Address property */
 private String address;

 /** Tel property */
 private int tel = 0;

 /** Name property */
 private String name;
 // --------------------------------------------------------- Methods
......
//getter,setter mothod,这里不赘列了

}

5、总结
这样一个国际化的小例子就可以运行了,不过上面写得比较粗略,需要对java和struts有一定了解。
网上对国际化也有其他的做法,也可以参考。
如果上述有所偏颇,敬请斧正。

posted on 2004-10-11 16:56  候鸟南飞  阅读(6511)  评论(11编辑  收藏  举报

导航