方法一:扩展requestProessor类
1:新建CharEncoding类
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
public class CharEncoding extends RequestProcessor {
protected boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
try {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
return true;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
}
}
}
2:配置struts-config.xml
<controller processorClass="com.biz.CharEncoding" />
必须在<message-resources>前面
方法二:过滤器
1:新建Tfilter类
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;
public class Tfilter implements Filter{
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
2:配置web.xml
<!-- 过滤器 -->
<filter>
<filter-name>Tfilter</filter-name>
<!--完整的类名-->
<filter-class>com.biz.Tfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Tfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>