随笔 - 3, 文章 - 152, 评论 - 17, 引用 - 0
数据加载中……

struts下的乱码问题的解决办法

 1、页面提示信息乱码
页面的提示信息来自ApplicationResources_zh.properties
解决方法:
(1)所有jsp页面均要用
<%@ page language="java" contentType="text/html; charset=GBK" %>
指出当前页面的charset
(2)用notepad等工具(而不是Eclipse Editor)编写中文资源文件,比如ApplicationResources_xx.properties。然后用工具native2ascii将资源文件中的中文字符转换为GBK,方法是在DOS下
native2ascii -encoding GBK ApplicationResources_xx.properties ApplicationResources_zh.properties

2、提交的中文字符在服务器端(JBOSS)乱码
解决办法:增加一个filter,里面将request中的中文转换为GBK
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
     request.setCharacterEncoding("GBK");
     chain.doFilter(request,response);
 }

另:关于native2ascii
使ApplicationResources.properties支持中文 
建立一个ApplicationResources_ISO.properties文件,把应用程序用的message都写进去,然后在dos 
下执行这个命令, 
native2ascii -encoding gb2312 ApplicationResources_ISO.properties  
ApplicationResources.properties 
这样就会将ISO编码的ApplicationResources转换成GB2312编码的格式了,同时保存到 
ApplicationResources.properties. 
native2ascii这个工具是jdk自带的一个东东,所以如果path都设定正确就可以直接运行了,你可以在 
$java_home$/bin下找到他。 
转换后的中文类似于这个样子 
iso 格式下 :tj.type=商品车类型 
gb2312格式下 :tj.type=\u5546\u54c1\u8f66\u7c7b\u578b 
然后在struts-config.xml中设置应用这个资源文件 
<message-resources parameter="com.huahang.tj.ApplicationResources"  
key="org.apache.struts.action.MESSAGE" /> 
开发jsp时在jsp的开头写上<%@ page contentType="text/html; charset=gb2312" %>,将字符集设置 
成gb2312就可以了。

posted on 2005-07-31 23:55 阅读(273) 评论(0)  编辑  收藏 所属分类: Struts


只有注册用户登录后才能发表评论。


网站导航: