日期:2014-05-17 浏览次数:20701 次
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<filter>
<filter-name>encodeFilter</filter-name>
<filter-class>com.test.common.EncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
package com.test.common;
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 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
public class EncodeFilter extends StrutsPrepareAndExecuteFilter implements
Filter
{
private FilterConfig config = null;
private String encoding = null;
@Override
public void init(FilterConfig config) throws ServletException
{
this.config = config;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
if (encoding == null)
{
encoding = config.getInitParameter("encoding");
}
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
@Override
public void destroy()
{
config = null;
encoding = null;
}
}
<property