解决资源文件传递中文问题:

本文介绍如何在Struts框架中配置资源文件以正确显示中文内容,包括使用native2ascii工具转换资源文件、在struts-conf.xml中注册资源文件及在JSP页面中调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

解决资源文件传递中文问题:

还是拿注册为例:

自定义一个资源文件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值”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值