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="英文版" οnclick="changeLanguage('en')">   
   <input type="button" name="langCn" value="中文版" οnclick="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,这里不赘列了

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值