首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

天极网 > 开发频道>Struts国际化编程轻松实现

Struts国际化编程轻松实现

2004-02-20 14:47作者:kevin出处:matrix责任编辑:方舟

  7、 建立web.xml文件

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd";>

<web-app>
<display-name>test international</display-name>

<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>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

</web-app>

  上面的web.xml定义了struts的控制类、config文件和标签,因为比较简单,所以不做解释;

  8、 建立资源文件

  在classes目录下,建立一个resources目录,用来存放资源文件;

  先建立默认的资源文件application.properties和英文(美国)的资源文件application_en_US.properties,内容为:

# -- international test --
test.title=international application test
test.body=This is a international application test

  先建立这两个文件,中文的等下一步建立

  9、建立jsp文件

  在international目录下,建立index.jsp文件,内容为:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html:html locale="true">
<head>
<title><bean:message key="test.title"/></title>
<html:base/>
</head>
<body bgcolor="white">

<p><bean:message key="test.body"/></p>

</body>
</html:html>

  在这里<html:html locale="true">表示使用浏览器默认的地区和语言;<bean:message key="test.title"/>的意思是取对应资源文件里的test.title项目的内容; 启动Tomcat,在浏览器里输入http://localhost:8080/international/,查看效果,如果浏览器标题显示international application test,页面里显示This is a international application test则说明你的程序成功了;下面只要增加资源文件,你就可以在多种语言的系统里看了;

  10、 建立简体中文的资源文件

  在resources目录下建立一个application_cn.properties,输入内容:

# -- international test --
test.title=国际化程序测试
test.body=这是一个国际化程序测试例子

  因为java的国际化是通过unicode码来实现,所以要把代码转为unicode码;在Dos下,转到resources目录,执行:

native2ascii application_cn.properties application_zh_CN.properties

  转换后的application_zh_CN.properties文件内容为:

# -- international test --
test.title=\u56fd\u9645\u5316\u7a0b\u5e8f\u6d4b\u8bd5
test.body=\u8fd9\u662f\u4e00\u4e2a\u56fd\u9645\u5316\u7a0b\u5e8f\u6d4b\u8bd5\u4f8b\u5b50

  这就是上面的中文的unicode码;
 
  重新启动Tomcat, 在浏览器里输入http://localhost:8080/international/,你看,标题和内容是不是变成中文了;

  11、建立繁体中文的资源文件

  在resources目录下建立一个application_tw.properties,输入内容:

# -- international test --
test.title=???H化程式?y??
test.body=?@是一?????H化程式?y??例子

  因为java的国际化是通过unicode码来实现,所以要把代码转为unicode码;在Dos下,转到resources目录,执行:

native2ascii application_tw.properties application_zh_TW.properties

  转换后的application_zh_TW.properties文件内容为:

# -- international test --
test.title=\u570b\u969b\u5316\u7a0b\u5f0f\u6e2c\u8a66
test.body=\u9019\u662f\u4e00\u500b\u570b\u969b\u5316\u7a0b\u5f0f\u6e2c\u8a66\u4f8b\u5b50

  这就是上面的繁体中文的unicode码;

  12、 测试多语言

  打开IE的“工具”->“Internet选项”菜单,“常规”选项卡,点击其中的“语言”按钮,添加“英语(美国)-[en-us]”语言,将其他的语言删除,重新启动IE后,输入http://localhost:8080/international/index.jsp,你会发现内容已经变成英文;
用同样的方法,可以测试简体中文和繁体中文;
共2页。 9 7 1 2

关注此文的读者还看过:

返回开发频道首页

共2页。 上一页12

软件频道最新更新

热点推荐

天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络