您的位置:首页 > 编程语言 > Java开发

最近使用Struts的两个小问题

2006-10-13 15:03 169 查看
一、<html:errors/>
今天调了很长时间的<html:errors/>,在action里明明写了错误信息:
message.add("userName", new ActionMessage("用户名密码不能为空"));
在页面<html:errors/>或 <html:errors property="userName"/>就是显示不出来,也不出异常,通过调试发现request里面也存在错误消息,一时不知如何是好.
后来四处搜索,很多人建议用: <html:messages>,后来改为:
<html:messages id="error" property="userName"></html:messages>
页面出现异常,说属性文件中未定义”用户名密码不能为空”,后来改为属性文件中的key值,调试通过.
由此得出结论: <html:errors>只能用定义在属性文件中的key值做消息,否则出错.
现在一般都用:
<html:messages id="error" property="userName"></html:messages>
输出消息(无论是错误消息,还是一般消息)
二、从MessageResources.properties读取中文显示在jsp时为乱码问题。
解决方法:在cmd下通过命令native2ascii可以将编码转成Unicode编码
native2ascii的具体用法:
JDK中的描述:native2ascii Convert text to Unicode Latin-1.
参数:
-reverse
Perform the reverse operation: convert a file with Latin-1 and/or
Unicode encoded characters to one with native-encoded characters.
将Unicode字符编码转成本地字符编码。
-encoding encoding_name
Specify the encoding name which is used by the conversion procedure.
The default encoding is taken from System property file.encoding. The
encoding_name string must be taken from the first column of the table of
supported encodings in the Supported Encodings document.
指定转换的编码名字,默认取系统编码,转成Unicode编码
例:在cmd下输入:native2ascii回车后输入想转换的字符:江山,然后回车即可看到编码过的字符为:/u6c5f/u5c71
用struts的时候可以直接在cmd下输入:
native2ascii -encoding gbk MessageResources.properties,这样就得到了编码过的属性文件,在使用的时候在JSP上就不会显示乱码了。
注:
目前有个专门编辑properties的插件PropertiesEditor(呵呵,小日本产),可以用本地编码格式书写,自动转成unicode编码格式。
在Eclipse的安装方法:
Help-àSoftware updatesàfind and install-àsearch for new features to install--à
New Remote Site(输入标题:PropEditor网址:http://propedit.sourceforge.jp/eclipse/updates/)
点击finish完成,之后就会自己下载,完成后点击install all就完成了安装。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: