关闭

Struts做的一个国际化

388人阅读 评论(0) 收藏 举报

1.首先是用struts做一个jsp页面locale.jsp,代码如下:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="taglibs.jsp" %>
<html:html locale="true">
  <head><title>国际化</title></head>
    <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>
  <body>
    <html:form action="/regist/customers_regist">
    <input type="hidden" name="formAction" value="changeLanguage">
    <input type="hidden" name="language">
     <input type="button" name="langEn" value="English" onclick="changeLanguage('en')">  
     <input type="button" name="langCn" value="中文版" onclick="changeLanguage('cn')">
    </html:form>
  </body>
</html:html>
其中引用了一个taglibs,jsp页面,这个页面主要是引用了标签必须用到的.tld文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Listing of all of the taglibs that we reference in this application --%>
<%-- Struts provided Taglibs --%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
 
2.创建一个Action:LocaleAction.java,代码如下:
/**
 * <p>application name: LocaleAction</p>
 * <p>application describing: 此类为实现国际化的action</p>
 * <p>copyright: Copyrigtht 2006 东软 成都Java定制班版权所有</p>
 * <p>company: neusoft</p>
 * <p>time: 2006.11.05</P>
 *
 * @author ZhongHui
 * @version ver 2.1
 * @email:2031120607@smail.ccniit.com
 *
 * */
package com.neusoft.tis.control.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.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
public class LocaleAction 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("customers_regist");
 }
}
3.因为我是为一个客户注册进行的国际化,所以用到的是CustomerAdminsForm.java,代码如下:
/**
 * <p>application name: CustomerAdminsForm</p>
 * <p>application describing: 此类为前台页面与数据库数据交换。用于设置与获取相关变量</p>
 * <p>copyright: Copyrigtht 2006 东软 成都Java定制班版权所有</p>
 * <p>company: neusoft</p>
 * <p>time: 2006.11.05</P>
 *
 * @author Yichao Zeng
 * @version ver 2.1
 * @email:2032120714@smail.ccniit.com
 *
 * */
package com.neusoft.tis.control.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class CustomerAdminsForm extends ActionForm {
 private static final long serialVersionUID = -1L;
 private String loginUser = null;  //登录ID
 
 private String customerID = null;  //客户ID(消费者)
 private String passwd = null;   //密码
 private String email = null;   //电子邮箱(供找回密码使用)
 private String userStatus = null;  //用户状态(包括:停用、正常)
 private String question = null;      //找回密码问题
 private String answer = null;   //找回密码答案
 private String sex = null;    //性别
 private String birthDay = null;   //出生日期
 private String lastLoginDate = null; //上次登录时间
 private String closer = null;   //关闭人
 private String closeDate = null;  //关闭日期
 private String remark = null;   //备注
 public String getAnswer() {
  return answer;
 }
 public void setAnswer(String answer) {
  this.answer = answer;
 }
 public String getBirthDay() {
  return birthDay;
 }
 public void setBirthDay(String birthDay) {
  this.birthDay = birthDay;
 }
 public String getCloseDate() {
  return closeDate;
 }
 public void setCloseDate(String closeDate) {
  this.closeDate = closeDate;
 }
 public String getCloser() {
  return closer;
 }
 public void setCloser(String closer) {
  this.closer = closer;
 }
 public String getCustomerID() {
  return customerID;
 }
 public void setCustomerID(String customerID) {
  this.customerID = customerID;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getLastLoginDate() {
  return lastLoginDate;
 }
 public void setLastLoginDate(String lastLoginDate) {
  this.lastLoginDate = lastLoginDate;
 }
 public String getLoginUser() {
  return loginUser;
 }
 public void setLoginUser(String loginUser) {
  this.loginUser = loginUser;
 }
 public String getPasswd() {
  return passwd;
 }
 public void setPasswd(String passwd) {
  this.passwd = passwd;
 }
 public String getQuestion() {
  return question;
 }
 public void setQuestion(String question) {
  this.question = question;
 }
 public String getRemark() {
  return remark;
 }
 public void setRemark(String remark) {
  this.remark = remark;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public String getUserStatus() {
  return userStatus;
 }
 public void setUserStatus(String userStatus) {
  this.userStatus = userStatus;
 }
 
 
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  
  loginUser = null;    
  customerID = null;  
  passwd = null;   
  email = null;   
  userStatus = null;  
  question = null;  
  answer = null;   
  sex = null;    
  birthDay = null;   
  lastLoginDate = null;  
  closer = null;   
  closeDate = null;  
  remark = null;   
 }
 
 public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
   ActionErrors errors = new ActionErrors();
   return errors;
 }
}
4.接下来修改struts-config.xml文件
在<action-mappings>下边添加如下代码:
<action
      attribute="customerAdminsForm"
      input="/locale.jsp"
      name="customerAdminsForm"
      path="/regist/customers_regist"
      scope="request"
      parameter="formAction"
      type="com.neusoft.tis.control.action.LocaleAction">
      <forward name="customers_regist" path="/regist/customers_regist.jsp" />
</action>
在这里用到了一个customers_regist.jsp界面,代码如下:
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="../taglibs.jsp" %>
<html:html locale="true">
<head>
<title><bean:message key="regist.customers.titile" /></title>
</head>
<link href="<%=request.getContextPath()%>/css.css" rel="stylesheet" type="text/css">
<body>
<html:errors/>
<html:javascript formName="customerAdminsForm" />
<html:form action="/customers_regist" onsubmit="return validateCustomerAdminsForm(this)">
  <table width="779" border="1">
    <tr>
      <td><table cellSpacing=2 cellPadding=0 width="100%"
              border=0>
              <tr>
                <td class=f14><bean:message key="regist.choose.loginuser" /></td>
                <td class=f14 align=right><B><bean:message key="regist.flow"
/></B></td></tr></table><!--注册流程展示  end-->
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>             
              <tr>
                <td></td>
                <td colspan="3" align=left><font color=red><B><bean:message
key="regist.choose.loginuser.notice" /></B></font></td>
              </tr>
              <tr>
                <td></td>
                <td width="413" colspan="2" align=center>*<bean:message
key="regist.choose.loginuser.name" />
                <html:text property="loginUser" /></td>
                <td width="326" rowspan="2" align=left><p><bean:message
key="regist.flow.notice" /></p>
                </td>
              </tr>
              <tr>
                <td width=30></td>
                <td align=right>
                <html:submit property="submit">
                 <bean:message key="regist.choose.loginuser.submit" />
                </html:submit>
                </td>
                <td align=center>&nbsp;</td>
              </tr></table></td>
    </tr>
    <tr>
      <td><table width="771" border="0">
        <tr>
          <td colspan="2"><div align="center">*<bean:message key="regist.passwd" />
            <html:password property="passwd" />
          </div></td>
          <td rowspan="2"><bean:message key="regist.passwd.notice" /></td>
        </tr>
        <tr>
          <td colspan="2"><div align="center">*<bean:message key="regist.repasswd" />
            <input type="password" name="repasswd">
          </div></td>
          </tr>
      </table></td>
    </tr>
    <tr>
      <td><table width="771" border="0">
        <tr>
          <td width="263" rowspan="3"><bean:message key="regitst.passwd.search.notice" />
          </td>
        </tr>
        <tr>
          <td colspan="2"><div align="center">*<bean:message
key="regitst.passwd.search.question" />
            <html:select property="question">
             <html:option value=""><bean:message
key="regitst.passwd.search.question.option0" /></html:option>
             <html:option value="我的宠物名字?"><bean:message
key="regitst.passwd.search.question.option1" /></html:option>
             <html:option value="我最好的朋友是谁?"><bean:message
key="regitst.passwd.search.question.option2" /></html:option>
             <html:option value="我最喜爱的颜色?"><bean:message
key="regitst.passwd.search.question.option3" /></html:option>
            </html:select>
</div></td>
          </tr>
        <tr>
          <td colspan="2"><div align="center">*<bean:message
key="regitst.passwd.search.answer" />
            <html:text property="answer" />
          </div></td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td><table width="772" border="0">
        <tr>
          <td width="520" height="23"><div align="center">*<bean:message key="regist.sex"
/>
            <html:radio property="sex" value="男"><bean:message key="regist.sex.man"
/></html:radio>
           
            <html:radio property="sex" value="女"><bean:message key="regist.sex.woman"
/></html:radio>
        </div></td>
         
         
         
        <tr>
          <td width="520" height="23"><div align="center">*<bean:message
key="regist.email" />
            <html:text property="email" />
          </div></td>
        </tr>
        <tr>
      <td><div align="center">
        <html:submit property="submit">
         <bean:message key="regist.submit" />
        </html:submit>
      </div></td>
    </tr>
  </table>
</html:form>
</body>
</html:html>
这个页面是放在regist文件佳下边的,各位可以根据自己的需要修改xml文件的路径,里面都用到了相
对应的资源文件的东西,接下来将会讲到。
5.接下来讲一讲用到的资源文件
Resources.properties这个是默认的,当什么都没选择时,会根据浏览器的语言选择这个资源文件,
我默认的是中文,内容如下:
################# regist/customers_regist.jsp
regist.customers.titile = \u4f1a\u5458\u6ce8\u518c
regist.choose.loginuser = \u9009\u62e9\u767b\u5f55\u540d
regist.choose.loginuser.notice = \u8bf7\u7528\u82f1\u6587\u548c\u7b80\u4f53\u4e2d\u6587
\uff08GB\uff09\u586b\u5199
regist.choose.loginuser.name = \u767b\u5f55\u540d\uff1a
regist.choose.loginuser.submit = \u767b\u5f55\u540d \u662f\u5426\u88ab\u5360\u7528
regist.flow = \u6ce8\u518c\u6b65\u9aa4\uff1a1.\u9009\u62e9\u767b\u5f55\u540d - 2.\u6ce8
\u518c\u6210\u529f
regist.flow.notice = \u767b\u5f55\u540d\uff1a4-16\u4e2a\u5b57\u7b26(\u5305\u62ec4
\u300116)\u62162-8\u4e2a\u6c49\u5b57\uff0c\u8bf7\u7528\u82f1\u6587\u5c0f\u5199\u3001
\u6c49\u5b57\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\uff0c\u4e0d\u80fd\u5168\u90e8
\u662f\u6570\u5b57\u3002
regist.passwd = \u8f93\u5165\u767b\u5f55\u5bc6\u7801\uff1a
regist.repasswd = \u518d\u6b21\u8f93\u5165\u5bc6\u7801\uff1a
regist.passwd.notice = \u5bc6\u7801\u5b57\u6bcd\u6709\u5927\u5c0f\u5199\u4e4b\u5206
\u30024\u201416\u4f4d\uff08\u5305\u62ec4\u300116\uff09\uff0c\u9650\u7528\u82f1\u6587
\u3001\u6570\u5b57\u3002
regitst.passwd.search.notice = \u67e5\u8be2\u7b54\u6848\uff1a6\u4e2a\u5b57\u6bcd\u3001
\u6570\u5b57\u62163\u4e2a\u6c49\u5b57\u4ee5\u4e0a\uff08\u5305\u62ec6\u4e2a\uff09\u3002
\u662f\u5fd8\u8bb0\u5bc6\u7801\u65f6\uff0c\u60a8\u627e\u56de\u5bc6\u7801\u7684
\u8eab\u4efd\u51ed\u8bc1\u3002\u8bf7\u52a1\u5fc5\u7262\u8bb0\uff01
regitst.passwd.search.question = \u5bc6\u7801\u67e5\u8be2\u95ee\u9898\uff1a
regitst.passwd.search.question.option0 = --\u8bf7\u60a8\u9009\u62e9--
regitst.passwd.search.question.option1 = \u6211\u7684\u5ba0\u7269\u540d\u5b57\uff1f
regitst.passwd.search.question.option2 = \u6211\u6700\u597d\u7684
\u670b\u53cb\u662f\u8c01\uff1f
regitst.passwd.search.question.option3 = \u6211\u6700\u559c\u7231\u7684\u989c\u8272\uff1f
regitst.passwd.search.answer = \u5bc6\u7801\u67e5\u8be2\u7b54\u6848\uff1a
regist.sex = \u6027\u522b\uff1a
regist.sex.man = \u7537
regist.sex.woman = \u5973
regist.email = \u7535\u5b50\u90ae\u4ef6:
regist.submit = \u63d0\u3000\u4ea4\u3000\u8868\u3000\u5355
另外有个中文的Resources_zh_CN.properties,内容和上边一样,不多说了,当点击中文按钮时就会用
到这个文件。
还有一个Resources_en.properties,内容都是自己根据中文意思翻译的,有很多地方翻译不够准备,
还请见谅,内容如下:
######### regist/customers_regist.jsp
regist.customers.titile = Leaguer Regist
regist.choose.loginuser = Choose Login Name
regist.choose.loginuser.notice = Please use English and Chinese(GB)
regist.choose.loginuser.name = Login Name\uff1a
regist.choose.loginuser.submit = Check Login Name
regist.flow = Regist step\uff1a1.choose login name - 2.regist success
regist.flow.notice = Login name\uff1a4-16 of charactor(include 4\u300116)or 2-8 of
Chinese\uff0c please use lower-case of
English\u3001Chinese\u3001number\u3001underline\uff0c Don't all of number
regist.passwd = Input password\uff1a
regist.repasswd = Again input password\uff1a
regist.passwd.notice = Password letter partition lower-case and upper-case.4-16 place
(include 4\u300116),only use English and number.
regitst.passwd.search.notice = Query answer\uff1a over 6 of letter\u3001number or 3 of
Chinese.If you fogot password,you can find for it.Please must remember.
regitst.passwd.search.question = Password query question\uff1a
regitst.passwd.search.question.option0 = --Please choose--
regitst.passwd.search.question.option1 = what is my pet name\uff1f
regitst.passwd.search.question.option2 = who is my best friend\uff1f
regitst.passwd.search.question.option3 = what is my favorite color\uff1f
regitst.passwd.search.answer = Password query answer\uff1a
regist.sex = Sex\uff1a
regist.sex.man = Man
regist.sex.woman = Woman
regist.email = E-Mail\uff1a
regist.submit = Submit form
另外还需要在struts-config.xml文件里配置一下
<message-resources parameter="com.neusoft.tis.system.locale.Resources" />
在这里,我的默认资源文件是放到这个下边的,各位请根据自己放置的位置填写。另外还需要注意的
是对资源文件的命名,英文是Resources_en.properties,中文是Resources_zh_CN.properties,前面
的Resources可以自己随便取,后边要遵循规范来做,否则不能找到。
程序演示如下:
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2076次
    • 积分:49
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档
    最新评论