Struts1 处理中文乱码《续》

㈢国际化资源文件乱码

①利用JDK的native2ascii工具进行编码转换

国际化问题,主要是为了处理文件在浏览器上的显示问题,还是以登录界面来说,比如在中文浏览器上,我们要看到中文显示,对应在英文浏览器上要显示英文。那么我们在登录那个界面处理上,就不能直接写上我们的“用户名”“密码”等标识了。就要用标记转换输出了,修改为:

<bean:message key="example.login.username"/>
<bean:message key="example.login.username"/>

再者,打开项目下的资源配置文件ApplicationResources.properties,依据上面所写key值,设定成我们要的默认值(显示英文),比如

#Resource for Parameter 'com.v512.example.struts.ApplicationResources
#Project webexample2
example.login.username=username
example.login.password=password

现在我们动手新建一个资源文件,让其能显示中文,直接Ctrl+C,Ctrl+V。改名为ApplicationResources_zh.properties,代码如下:

#Resource for Parameter 'com.v512.example.struts.ApplicationResources
#Project webexample2
example.login.username=用户名
example.login.password=密码

但保存,myeclipse会报错,这时我们需要修改资源文件的编码格式。Windons---->Preferences---->Content Type------>Text----->JavaPropertiesFile,把其Default encoding改为“utf-8”,按“update”更新。这样就能进行保存了。但是当我们进行验证会不是成功时,仍然给我们的是乱码。

不急,我们还得做一项任务,打开DOS窗口,CMD到资源文件所在目录,运用JDK的native2ascii工具把我们新建的资源文件改成另一个名字的资源文件,例如bank.properties。命令如下:

>native2ascii -encoding gbk ApplicationResources_zh.properties bank.properties


打开bank.properties资源文件,自动生成的代码如下:

#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)

example.login.username = \u7528\u6237\u540D
example.login.password = \u5BC6\u7801

然后在myeclipse窗口中,把原来新建ApplicationResources_zh.properties 删除,并把bank.properties改为ApplicationResources_zh.properties (为了方便记忆,管理)。然后重起tomcat或进行reload文件,我们发现乱码问题没有了。

②利用Eclipse ResourceBundle Editor插件工具

以上我们是利用了JDK的native2ascii工具来处理国际化问题,但在EC中,还有一种更方便的工具专门用来处理编辑java的资源文件国际化乱码问题,即Eclipse ResourceBundle Editor插件工具。安装了这个插件后,我们能进行方便的可视化资源文件编辑。推荐。。


其中
<filter>   
<filter-name>characterEncoding</filter-name>
<filter-class>com.v512.example.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter> <filter-name>characterEncoding</filter-name> <filter-class>com.v512.example.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
并不是必须的,你可以设置struts.properties 或 struts.xml
分别对应增加 struts.i18n.encoding = utf-8 或 <constant name="struts.i18n.encoding" value="utf-8" />


原因在于:

org.apache.struts2.dispatcher.Dispatcher的public void prepare(HttpServletRequest request, HttpServletResponse response) 已经帮你做这件事了,并且随便还将你设置的 struts.locale = zh_CN 一并处理了:


public void prepare(HttpServletRequest request, HttpServletResponse response) {   

String encoding = null;

if (defaultEncoding != null) {

encoding = defaultEncoding;

}



Locale locale = null;

if (defaultLocale != null) {

locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLoca());

}



if (encoding != null) {

try {

request.setCharacterEncoding(encoding);

} catch (Exception e) {

LOG.error("Error setting character encoding to '" + encoding + "' -ignoring.", e);

}

}



if (locale != null) {

response.setLocale(locale);

}

if (paramsWorkaroundEnabled) {

request.getParamete("foo"); // simply read any parameter (existing or not) to "prime" the request

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值