当前位置:天才代写 > tutorial > JAVA 教程 > Struts国际化编程轻松实现

Struts国际化编程轻松实现

2017-11-11 08:00 星期六 所属: JAVA 教程 浏览:938

副标题#e#

struts是一个MVC框架,像Java和其他Java框架一样,struts可以轻松实现国际化;于是按照网上的资料,做了一个实验,因为第一次做多语言措施,照旧拐了许多弯路;但所幸,颠末不绝的实验,终于乐成的实现多语言版本的简朴页面;

因为措施很是简朴,所以在整个实验进程中,全部利用手工编码,没有利用任何帮助东西;

1、成立处事器

我利用Tomcat4作为测试情况,成立进程(略);

2、下载struts

可以到http://jakarta.apache.org/struts/index.html下载,下载后解压,把个中的.war文件拷贝到Tomcat的webapps目次下,启动Tomcat,假如http://localhost:8080/struts-example/ 运行没有问题,说明情况成立乐成;这些.war文件在Tomcat启动后会自动展开成文件,内里有源代码,可以作为源码研究;

3、成立工程

在webapps目次下成立一个international文件夹,再在international目次下成立WEB-INF文件夹和WEB-INF/classes文件夹,这些都是一个JSP工程必需的;

4、加了struts的类

在WEB-INF目次下成立一个lib子目次,把struts-example\WEB-INF\lib目次下将所有.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-example\WEB-INF目次下,把.TLD文件拷贝到international的WEB-INF目次下,这些文件标签库的界说文件;

struts-bean.tld

struts-html.tld

struts-logic.tld

struts-nested.tld

struts-template.tld

struts-tiles.tld


#p#副标题#e#

6、成立struts的config文件

成立struts的config文件的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表是classes目次下的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 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文件和标签,因为较量简朴,所以不做表明;

#p#副标题#e#

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

#p#分页标题#e#

<%@ 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则说明你的措施乐成了;下面只要增加资源文件,你就可以在多种语言的系统里看了;

#p#副标题#e#

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,你会发明内容已经酿成英文;

用同样的要领,可以测试简体中文和繁体中文;

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式