在struts中使用国际化(i18n) i18n可以满足对系统的国际化,它的原理就是将页面上的所有标志都放到一个消息资源文件中,不同的语言要提供不同的消息资源文件,当用户登录系统是,系统就是根据你登录的语言,选择不同的消息资源文件显示出来,这样你就可以看到不同的效果了。 一、配置文件的设置 其实i18n的使用还是比较简单的,首先你要在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> <!– ========== form definitions =================================== –> <form-beans> <form-bean name="helloform" type="hello.helloform"/> </form-beans> <!– ========== global forward definitions?============================== –> <global-forwards> <forward name="aerror" path="/public/jsp/showerror.jsp"/> <forward name="success" path="/public/jsp/showsuccess.jsp"/> </global-forwards> <!– ========== action mapping definitions ============================== –> <action-mappings> <action path="/helloworld" type="hello.helloaction" name="helloform" scope="request"> <forward name="sayhello" path="/hello.jsp"/> </action> </action-mappings> <!– ========== message resources definitions =========================== –> <!–指定资源文件的路径–> <message-resources parameter="hello.applicationresources"/>
</struts-config>——————————————————————————————- 这个配置文件的配置项<message-resources>就是用来配置资源文件路径,在这里,路径被配置在系统classpath里面的hello/applicationresources.properties文件中。 二、资源文件 现在我们可以开始配置资源文件了,如下(applicationresources.properties文件):——————————————————————————————-; application resources
hello.title = 你好,欢迎光临!——————————————————————————————- 在这个配置文件中,只有一个注释(用逗号做标志),一个信息。对于中文来说,上面这个文件是没有办法辨认的,必须给这个文件转化编码才行。 在dos命令行进入applicationresources.properties所在的文件夹使用命令:native2ascii -encoding gb2312 applicationresources.properties applicationresources_zh.properties native2ascii是jdk的一个工具,放在jdk安装目录的bin目录下,如果出现“native2ascii不是内部命令”,那可能是你没有设置环境变量。
三、jsp文件 下面是一个简单的jsp文件,里面使用了i18n,如下: ——————————————————————————————-<%@ page contenttype="text/html;charset=gbk"%>
<%@ 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" %>
<html> <body> <td> <bean:message key="hello.title"/> </td> </body></html>——————————————————————————————- 其中,前面三句话是将标记库文件包含进文件中,这样在下面使用的标记还可以被辨认,如下面的<bean>标记。 下面这句话<bean:message key="hello.title"/>,就是将内容显示出来,这个内容是从文件applicationresources_zh.properties读取的,在这里,hello.title是“你好,欢迎光临!”,当然,这要你系统的编码的简体中文才行。
