昨天写了个简体繁体的代码,在过滤器里实现对输出页面的编码转换
核心代码如下:
......
HttpServletResponseWrapper wrappedResp = factory.newSimplifiedWrapper(response);
chain.doFilter(request, wrappedResp);
chain.doFilter(request, wrappedResp);
......
并重写HttpServletResponseWrapper 的PrintWriter getWriter()方法
public HttpServletResponseWrapper newSimplifiedWrapper(
ServletResponse response) {
return new HttpServletResponseWrapper((HttpServletResponse)response) {
public PrintWriter getWriter()
throws IOException {
return new MyPrintWriter(getResponse().getWriter());
}
};
}
ServletResponse response) {
return new HttpServletResponseWrapper((HttpServletResponse)response) {
public PrintWriter getWriter()
throws IOException {
return new MyPrintWriter(getResponse().getWriter());
}
};
}
类MyPrintWriter,重写PrintWriter的public void write(char[] buf, int off, int len) 方法,这个方法里可以读到整个页面的HTML格式字符,将其转成对应的简繁字符。
在此感谢金大为先生提供必要的帮助
,谢谢!