解决资源文件传递中文问题:
还是拿注册为例:
自定义一个资源文件temp.properties.
这个资源文件想要当前系统中可用的话,就必需到struts-conf.xml中注册
方法是:<message-resources parameter="prj2.temp" key="T"/>
,将它和系统默认的资源文件放在一起:
其是key:是自己随便取的,不同的KEY表示不同的资源文件。
注册好后:就可以在网页中调用了
调用方法:
<bean:message key="inf.input.password" bundle="T" arg0="account"/> <html:text property="account"/><br>
其中的bundle="T"是相应资源文件的key值。
---------------------------------------------------------------------
ApplicationResources.properties
# Resources for parameter 'prj2.ApplicationResources'
# Project prj2
inf.input.password=<font color=red>请输入 {0}:</font>
login.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<body>
<html:form action="/login">
<h3>利用资源文件传参数</h3>
<bean:message key="inf.input.password" arg0="password"/><html:password property="password"/><br>
<bean:message key="inf.input.account" arg0="account"/> <html:text property="account"/><br>
<html:submit/><html:cancel/>
<h3>利用资源文件传参数 传中文</h3>
<bean:message key="inf.input.password" bundle="CH" arg0="password"/><html:password property="password"/><br>
<bean:message key="inf.input.password" bundle="T" arg0="account"/> <html:text property="account"/><br>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
这样将会出现乱码问题;如下:
---------------------------------------------------------------------------------------------------------------
那么怎样转中文呢?
就必需把资源文件temp.properties.转中文
方法:native2ascii –encoding gb2312 原文件名 目标文件名
如上例:native2ascii –encoding gb2312 temp.properties ApplicationCHResources.properties
上面的我定义一个资源文件(源文件),命名为:temp.properties
----------------------------------------------------------------------------------------------
inf.input.password=<font color=red>请输入 {0}:</font>
------------------------------------------------------
然后到DOC下,到temp.properties目录下,
输入命令:native2ascii –encoding gb2312 temp.properties ApplicationCHResources.properties
可以得到ApplicationCHResources.properties资源文件。
得到的ApplicationCHResources.properties资源文件是这样的:
inf.input.password=<font color=red>/u8bf7/u 8f 93/u5165 {0}:</font>
然后在struts-conf.xml中注册使用:
如下:
<message-resources parameter="prj2.ApplicationResources" />
<message-resources parameter="prj2.ApplicationCHResources" key="CH"/>
<message-resources parameter="prj2.temp" key="T"/>
之后可以到JSP中调用:
Login.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<body>
<html:form action="/login">
<h3>利用资源文件传参数</h3>
<bean:message key="inf.input.password" arg0="password"/><html:password property="password"/><br>
<bean:message key="inf.input.account" arg0="account"/> <html:text property="account"/><br>
<html:submit/><html:cancel/>
<h3>利用资源文件传参数 传中文</h3>
<bean:message key="inf.input.password" bundle="T" arg0="password"/><html:password property="password"/><br>
<bean:message key="inf.input.password" bundle="CH" arg0="account"/> <html:text property="account"/><br>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
-----------------------------------------------------------------
可以得到如下结果:
从图形中可以看出:
?????? password:之所以会出现乱码:是因为它没有调用:处理中文问题的资源文件
从JSP的页面中可以看到:
<bean:message key="inf.input.password" bundle="T" arg0="account"/> <html:text property="account"/><br>
它所绑定的是temp.properties文件(从bundle="T"可以看出)。而它是处理中文前的文件。
请输入 account:可以看到它处理了中文问题,
<bean:message key="inf.input.password" bundle="CH" arg0="account"/> <html:text property="account"/><br>
它所绑定的是ApplicationCHResources.properties文件(从bundle="CH"可以看出)
上面的bundle=“资源文件中的KEY值”