☆蓝色梦想☆

世界总是反反覆覆错错落落地飘去 来不及叹息 生活不是平平淡淡从从容容的东西 不能放弃
posts - 57, comments - 5, trackbacks - 0, articles - 0

关于struts国际化的一点尝试

Posted on 2006-02-27 14:51 ☆蓝色梦想☆ 阅读(318) 评论(0)  编辑  收藏 所属分类: Struct

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下的目录结构是一致的,如下图所示
web-xml.jpg

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有一定了解。
网上对国际化也有其他的做法,也可以参考。
如果上述有所偏颇,敬请斧正。


只有注册用户登录后才能发表评论。


网站导航: