---
title: "使用ajax4jsf 乱码问题"
author: "stvanstar"
date: "2010-02-09T16:56:14Z"
tags: []
categories: ["Web前端"]
slug: "ajax4jsf"
draft: false
lastmod: "2010-02-09T16:56:14Z"
id: 592396
url: "https://stvanstar.iteye.com/blog/592396"
---

在使用richfaces控件提交时，画面上有部分会出现乱码，出现的地方和时间不是确定的

一开始以为是控件用法有问题导致的，

经过调查，发现后台有个filter，会对每个请求设置编码GBK，

这个对普通的request是正确的，但是ajax的request的默认编码是UTF-8，

返回前台也是用UTF-8解码的

所以filter里不加区别的对ajax请求设置编码，就会导致返回前台时ajax无法正确解码

filter修改如下

 

```
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
	  {
	    if (this.setEncoding) {
	    	// 暂时用UTF-8判断ajax request。。。
	    	if (!"UTF-8".equalsIgnoreCase(request.getCharacterEncoding())) {
	    		request.setCharacterEncoding(this.encoding);
	    	}
	    }
	    chain.doFilter(request, response);
	  }

链接两个说编码的：
http://chen-516888.iteye.com/blog/297448

http://www.blogjava.net/smcdl/archive/2009/06/07/280415.html
```