圈子首页 java JSF 论坛

UIViewRoot 和Locale,产生乱码

ygwtxz 2008-04-24
帮忙解决下
需要做中英文界面的切换
当用户点击英文时,执行FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.ENGLISH);
界面是换成英文版了,但是如果页面上的输入框曾被输入过中文值,切换后里面的值就全是乱码了!
ltian 2008-04-24
做一个过滤器解决这个问题,JSF在生成页面时忘记设置local了。
/*
* 创建日期 2005-3-18
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package com.xdfsoft.web.servlet;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
* @author 互联网 Craig McClanahan
* @version 1.3
* <p><strong>这个过滤器用来解决网站中JSF页面的中文输入乱码问题。</strong></p>
* <p>JSF页面接受客户提交请求后,JSF对页面内容进行解码的过程中丢失了页面设定的
* 字符集,这样它会按照缺省的iso_1编码方式进行字符串转化,导致中文出现乱码。
* 本过滤器会在请求到达JSF页面处理器处理之前设定其字符集,来解决乱问题。</p>
* <p><strong>本过滤器在网站的web.xml配置如下:</strong></p>
* <filter><br>
    <filter-name>SetCharacterEncodingFilter</filter-name><br>
    <display-name>SetCharacterEncodingFilter</display-name><br>
    <filter-class>com.xdf.common.SetCharacterEncodingFilter</filter-class><br>
    <init-param><br>
      <param-name>encoding</param-name><br>
      <param-value>gb2312</param-value><br>
    </init-param><br>
    <init-param><br>
      <param-name>ignore</param-name><br>
      <param-value>true</param-value><br>
    </init-param><br>
</filter><br>
<filter-mapping><br>
    <filter-name>SetCharacterEncodingFilter</filter-name><br>
    <url-pattern>/*</url-pattern><br>
</filter-mapping><br>

* <ul>
* <li><strong>encoding</strong> - 指定的目标字符集,如:"gb2312"。</li>
*
* <li><strong>ignore</strong> - 如果被设置为 "true",则有客户端设定的任何字符集都将失效,
* 被<strong>encoding</strong>参数所指定的字符集替代。如果将其值设为"false",则只有在
* 客户端没有指定字符集的时候才用<strong>encoding</strong>参数作指定的字符集作为请求
* 的字符集。
</li>
* </ul>
*
*/
public class SetCharacterEncodingFilter implements Filter {
/**
* 通过本过滤器设置给请求的缺省的字符集编码。
*/
protected String encoding = null;
/**
*过滤器的配置对象。
*/
protected FilterConfig filterConfig = null;
/**
* 是否由客户端指定的字符集被忽略。
*/
protected boolean ignore = true;

/**
* 过滤器销毁时调用,释放某些资源
*/
public void destroy() {

this.encoding = null;
this.filterConfig = null;

}
/**选择或者设置请求中的字符集编码。这个方法会被自动调用,以完成过滤功能。
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request 到来的请求
* @param response 我们所创建的相应。
* @param chain 我们需要处理的过滤链。
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

HttpServletRequest httprequest=(HttpServletRequest)request;
String encoding=null;
if (ignore || (request.getCharacterEncoding() == null)) {
encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}

chain.doFilter(request, response);

}
/**
* 过滤器初始化。被自动调用。
*
* @param filterConfig 过滤器配置对象。
*/
public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/**
* 根据当前请求或者过滤器初始化参数设置,选择一个合适的字符集。<br>
* 如果没有设置,字符集,则返回<code>null</code>.
* <p>缺省的实现是返回了<strong>encoding</strong> 初始化参数中配置的字符集。
* @param request 需要处理的Servelt 请求。
*/
protected String selectEncoding(ServletRequest request) {

return (this.encoding);
}
}