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就可以了。