使用DWR和其它mvc框架混用时(如struts和springmvc),不会经过struts和springmvc的拦截器,如果有需求,需要自定义一个dwr拦截器。
dwr.xml配置如下
<dwr>
<allow>
<filter class="com.twl.util.DWRFilter"></filter>
</allow>
</dwr>
DWRFilter.java
由于doFilter没有提供HttpServletRequest的参数,所以获取request需要使用WebContextFactory去获取
package com.twl.util;
import java.lang.reflect.Method;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.directwebremoting.AjaxFilter;
import org.directwebremoting.AjaxFilterChain;
import org.directwebremoting.WebContextFactory;
import com.twl.core.framework.constant.UserConstant;
import com.twl.core.framework.user.UserSessionInfo;
import com.twl.core.framework.util.ContextContainer;
/**
* DWR过滤器
*
* @version 1.0
* @author Quinn He
* @date 2013-6-4
*/
public class DWRFilter implements AjaxFilter {
/**
* @see Filter#destroy()
*/
public void destroy() {
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
}
@Override
public Object doFilter(Object arg0, Method arg1, Object[] arg2,
AjaxFilterChain arg3) throws Exception {
final HttpServletRequest request = WebContextFactory.get().getHttpServletRequest(); //获取request对象
final UserSessionInfo userInfo=(UserSessionInfo) request.getSession().getAttribute(UserConstant.USER_SESSION_INFO);
ContextContainer.setCurrentLoginUser(userInfo);
return arg3.doFilter(arg0, arg1, arg2);
}
}