struts:Struts国际化编程轻松实现

  struts是个MVC框架像Java和其他Java框架struts可以轻松实现国际化;于是根据网上资料做了个尝试次做多语言还是拐了很多弯路;但所幸经过不断尝试终于成功实现多语言版本简单页面;

  非常简单所以在整个尝试过程中全部使用手工编码没有使用任何辅助工具;

  1、 建立服务器

  我使用Tomcat4作为测试环境建立过程(略);

  2、 下载struts

  可以到http://jakarta.apache.org/struts/index.html下载下载后解压把其中.war文件拷贝到Tomcatwebapps目录下启动Tomcat如果http://localhost:8080/struts-example/ 运行没有问题介绍说明环境建立成功;这些.war文件在Tomcat启动后会自动展开成文件里面有源代码可以作为源码研究;

  3、 建立工程

  在webapps目录下建立ernational文件夹再在ernational目录下建立WEB-INF文件夹和WEB-INF/es文件夹这些都是个JSP工程必须

  4、 加了struts

  在WEB-INF目录下建立个lib子目录把struts-exampleWEB-INFlib目录下将所有.jar文件拷贝到该目录下;这些文件是struts控制类库和标签类库等;

  commons-beanutils.jar

  commons-collections.jar

  commons-digester.jar

  commons-fileupload.jar

  commons-lang.jar

  commons-logging.jar

  commons-validator.jar

  jakarta-oro.jar

  struts.jar

  5、 加入struts标签定义文件

  从struts-exampleWEB-INF目录下把.TLD文件拷贝到ernationalWEB-INF目录下这些文件标签库定义文件;

  struts-bean.tld

  struts-html.tld

  struts-logic.tld

  struts-nested.tld

  struts-template.tld

  struts-tiles.tld

  6、 建立strutsconfig文件

  建立strutsconfig文件struts-config.xml,内容如下:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<message-resources parameter="resources.application"/>
</struts-config>
  message-resources标签是指message资源文件就是我们存放我们多种语言提示信息文件resources.application表是es目录下resources目录用来存放资源文件默认语言文件名为application.properties,中文为application_zh_CN.properties,其他语言类似;

  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 ernational</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet->org.apache.struts.action.ActionServlet</servlet-
<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、 建立资源文件

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

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

  # -- ernational test --

  test.title=ernational application test

  test.body=This is a ernational application test

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

  9、建立jsp文件

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

<%@ page contentType="text/html;char=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/ernational/,查看效果如果浏览器标题显示ernational application test页面里显示This is a ernational application test则介绍说明你成功了;下面只要增加资源文件你就可以在多种语言系统里看了;

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

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

# -- ernational test --
test.title=国际化测试
test.body=这是个国际化测试例子
  java国际化是通过unicode码来实现所以要把代码转为unicode码;在Dos下转到resources目录执行:

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

  # -- ernational test --

  test.title=u56fdu9645u5316u7a0bu5e8fu6d4bu8bd5

  test.body=u8fd9u662fu4e00u4e2au56fdu9645u5316u7a0bu5e8fu6d4bu8bd5u4f8bu5b50

  这就是上面中文unicode码;

  重新启动Tomcat, 在浏览器里输入http://localhost:8080/ernational/,你看标题和内容是不是变成中文了;

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

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

  # -- ernational 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文件内容为:

  # -- ernational test --

  test.title=u570bu969bu5316u7a0bu5f0fu6e2cu8a66

  test.body=u9019u662fu4e00u500bu570bu969bu5316u7a0bu5f0fu6e2cu8a66u4f8bu5b50

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



  12、 测试多语言

  打开IE“工具”->“Internet选项”菜单“常规”选项卡点击其中“语言”按钮添加“英语(美国)-[en-us]”语言将其他语言删除重新启动IE后输入http://localhost:8080/ernational/index.jsp你会发现内容已经变成英文;

  用同样思路方法可以测试简体中文和繁体中文;



Tags:  struts标签 struts2.0 struts2 struts

延伸阅读

最新评论

发表评论