关于get请求获取参数乱码我采用了一个过滤器,利用动态代理来增强request的getParameter()方法,但是用jfinal的controller里面的request并不是增强后的request代理类,也就是我配置的该过滤器没有任何作用,希望詹老师帮我看看是什么原因导致的,如下是我的过滤器代码:
package cn.com.geostar.filter;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
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 URLCodeFilter implements Filter {
@SuppressWarnings("rawtypes")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//获取类加载器
ClassLoader loader=URLCodeFilter.class.getClassLoader();
//获取所有实现接口
Class[] reqInterfaces=request.getClass().getInterfaces();
//获取动态代理对象
ServletRequest deCodeRequest=(ServletRequest) Proxy.newProxyInstance(loader, reqInterfaces,new DeCodeInvocationHandler(request));
//传入代理对象放行
chain.doFilter(deCodeRequest, response);
}
public void init(FilterConfig fConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
//代理对象的调用处理程序
class DeCodeInvocationHandler implements InvocationHandler{
//构造函数传入request对象;
ServletRequest request;
public DeCodeInvocationHandler(ServletRequest request) {
super();
this.request=request;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//判断如果是getParameter方法则增强
if("getParameter".equals(method.getName())){
String str=(String) method.invoke(request, args);
if(str!=null)
return new String(str.getBytes("iso-8859-1"),"utf-8");
}
//返回方法本身的返回值
return method.invoke(request, args);
}
}
啥太早,太晚啊,我不吃早饭