jsp&ajax乱码解决-1

        系统中是一个用开源框架做的系统,其中包括一部分ajax调用.即大部分是以.do的方式调用的后台action,而其中一部分又是以.do方式调用的后台ajax程序.两个都是调用.do的方式进行,所以后台基本的程序代码都差不多.惟一不同的就是非ajax调用时,返回的是modelAndView对象模型,将返回对象注入到request.attribute中,而ajax调用时,则是将需要返回的值由response.writer写回客户端.
        程序中采用的编码为GBK,而不是通用的utf-8编码,ajax使用是prototype.js,使用tomcat6进行开发.所以就要在一些问题上对编码进行处理.
        在处理jsp的乱码问题上,分两步方式进行.get方式采用在tomcat的server.xml中加入URIEncoding=GBK设置,使其查询编码为GBK,并使之支持中文参数.而post方式呢,采用的是经典的tomcat setCharacterFilter进行处理.主要编码如下:
import javax.servlet.*;
import java.io.IOException;

public class SetCharacterEncodingFilter
		implements Filter{

	protected String encoding = null;
	protected FilterConfig filterConfig = null;
	public void destroy() {
		this.encoding = null;
		this.filterConfig = null;
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		/** Conditionally select and set the character encoding to be used */
		if(ignore || (request.getCharacterEncoding() == null)) {
			String encoding = selectEncoding(request);
			if(encoding != null) {
				request.setCharacterEncoding(encoding);
			}
		}
		/** Pass control on to the next filter */
		chain.doFilter(request, response);
	}
	public void init(FilterConfig filterConfig) throws ServletException {

		this.filterConfig = filterConfig;
		this.encoding = filterConfig.getInitParameter("encoding");
		String value = filterConfig.getInitParameter("ignore");
		this.ignore = value == null || value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
	}
	protected String selectEncoding(ServletRequest request) {
		return this.encoding;
	}
}

 

这样在web.xml中加入此filter,并设置ignore为true和encoding为GBK.
       本来以为事情就这样完了,不想在进行ajax调用时,就出现问题了.ajax里面也分两种方式,一种是get,一种是post.其中以get方式进行调用时,是没有问题的.参数被正确地传递到action中,而以post方式进行调用时,麻烦就出来了,action中的中文参数全部成了乱码.在网上查了一下,ajax的post请求是以utf-8进行编码的,当用filter进行过滤时,自然就成了乱码了.由于在系统中的ajax调用是经过封装的,采用的方式是以post进行调用,且参数也多,可能查询的参数也不是get所能传递的,个人也不太欣赏get方式进行ajax调用.所以就不想修改现有的ajax调用方式,坚决用post方式.
      接下来就想从这个filter中下手,只要接受到ajax请求时,不再进行编码操作,而是以默认的utf-8进行编码.由于我写的ajax调用里面有个特殊的参数为ajax=true这个查询字符串.就想到了用拦截串这个东东,将ajax请求的编码再转换为utf-8编码.所以就写了如下的一个拦截器,进行编码转换.

public class Xx extends AbstractInterceptor{
	@SuppressWarnings("unchecked")
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		Map paramMap = actionInvocation.getInvocationContext().getParameters();
		if(paramMap.get("ajax") != null) {
			for(Object key : paramMap.keySet()) {
				if(key instanceof String) {
					String value = (String) paramMap.get(key);
					paramMap.put(key, new String(value.getBytes("GBK"), "UTF-8"));
				}
			}
		}
		return actionInvocation.invoke();
	}
}

 

        这样一来,凡是碰到有ajax调用的地方,都会进行一次转换,将已经乱码的参数转换成正确的参数.且不需要对其他地方进行发动,惟一需要的就是在action的配置上,在有ajax调用的action上加一个转换拦截器.不过这样感觉不是很好,每次ajax调用都要进行一次编码转换,能不能不进行编码转换呢.
       这就是下一个问题了,setCharacterFilter深入.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值