我的项目前台是用Extjs,不可避免要用到json
json当然就是用的jsonplugin我所有的配置几乎都是UTF-8的 配置见:struts.properties:struts.action.extension=do
struts.locale=zh_CN
struts.i18n.encoding=UTF-8
struts.xml: <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name= "struts.multipart.maxSize" value="1000000000" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 用户管理 -->
<package name="role_json" namespace="/role" extends="json-default">
<action name="roleTree" class="com.json.action.TestAction" method="roleTree">
<result name="success" type="json">
<param name="contentType">text/html</param>
<param name="root">trees</param>
<param name="ignoreHierarchy">false</param>
<param name="excludeNullProperties">true</param>
<param name="enableGZIP">true</param>
</result>
</action>
</package>
</struts>
貌似这样是没有问题的
但是问题还是来了,返回的中文是乱码,我用httpwatch查看,返回的头文件信息如下:
text/html;charset=UTF-8 : 48 bytes, gzip compressed to 53 bytes ( -10.4 % saving )
struts把头文件Content-Type设置成text/html;charset=UTF-8这是我把编码该为GBK,显示的中文乱码立即正确了
也就是说struts返回的编码其实是GBK各位大侠给点建议,怎么解决这个问题
json当然就是用的jsonplugin我所有的配置几乎都是UTF-8的 配置见:struts.properties:struts.action.extension=do
struts.locale=zh_CN
struts.i18n.encoding=UTF-8
struts.xml: <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name= "struts.multipart.maxSize" value="1000000000" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 用户管理 -->
<package name="role_json" namespace="/role" extends="json-default">
<action name="roleTree" class="com.json.action.TestAction" method="roleTree">
<result name="success" type="json">
<param name="contentType">text/html</param>
<param name="root">trees</param>
<param name="ignoreHierarchy">false</param>
<param name="excludeNullProperties">true</param>
<param name="enableGZIP">true</param>
</result>
</action>
</package>
</struts>
貌似这样是没有问题的
但是问题还是来了,返回的中文是乱码,我用httpwatch查看,返回的头文件信息如下:
text/html;charset=UTF-8 : 48 bytes, gzip compressed to 53 bytes ( -10.4 % saving )
struts把头文件Content-Type设置成text/html;charset=UTF-8这是我把编码该为GBK,显示的中文乱码立即正确了
也就是说struts返回的编码其实是GBK各位大侠给点建议,怎么解决这个问题
如果设置false就没有问题了大家可以看看jsonplugin的源码
package com.filter;import java.io.IOException;import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class Filter implements javax.servlet.Filter { public void destroy() {
// TODO Auto-generated method stub
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest newRequest=(HttpServletRequest)request;
String pathString=newRequest.getRequestURI(); //获取路径
if (pathString.lastIndexOf(".")>0) {
String path=pathString.substring(pathString.lastIndexOf("."),pathString.length());
if (path.equals(".css")||path.equals(".js")) { //判断是否是js或css文件
chain.doFilter(request, response);
}
else {
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
chain.doFilter(request, response);
}
}
else {
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
chain.doFilter(request, response);
}
} public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}}配置webxml.txt <filter>
<filter-name>filter</filter-name>
<filter-class>com.filter.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
不过这个已经不是过滤器的问题了/content type
if (serializationParams.isSmd())
response.setContentType("application/json-rpc;charset=" + serializationParams.getEncoding());
else
response.setContentType(serializationParams.getContentType() + ";charset=" + serializationParams.getEncoding());if (serializationParams.isGzip()) {
response.addHeader("Content-Encoding", "gzip");
GZIPOutputStream out = null;
InputStream in = null;
try {
out = new GZIPOutputStream(response.getOutputStream());
in = new ByteArrayInputStream(json.getBytes());
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
if (in != null)
in.close();
if (out != null) {
out.finish();
out.close();
}
} }else {
response.setContentLength(json.getBytes(serializationParams.getEncoding()).length);
PrintWriter out = response.getWriter();
out.print(json);
}
出问题的就是在红色这里因为我们本地的编码集是GBK 这里json.getBytes()默认就是用的GBK而ContentType设置的我们定义的UTF-8浏览器一看是UTF-8当然用utf-8解析了 就出现问题了当然把<param name="enableGZIP">true</param>设置为false可以解决问题但也不完美;
最终我的解决办法是修改jsonplugin的源码:if (serializationParams.isGzip()) {
response.addHeader("Content-Encoding", "gzip");
GZIPOutputStream out = null;
InputStream in = null;
try {
out = new GZIPOutputStream(response.getOutputStream());
in = new ByteArrayInputStream(json.getBytes(serializationParams.getEncoding()));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
if (in != null)
in.close();
if (out != null) {
out.finish();
out.close();
}
} } else {
//content type
if (serializationParams.isSmd())
response.setContentType("application/json-rpc;charset=" + serializationParams.getEncoding());
else
response.setContentType(serializationParams.getContentType() + ";charset=" + serializationParams.getEncoding()); response.setContentLength(json.getBytes(serializationParams.getEncoding()).length);
PrintWriter out = response.getWriter();
out.print(json);
}
有兴趣的同学可以去看看源码希望这个对也有这个问题的同学有所帮助遇事不慌 ,看看别人的源码还是有帮助的
和struts.properties配置struts.i18n.encoding=UTF-8是一个样的
解决方式现在看来只有修改strutsplugin的代码了 结贴了