struts2怎样实现一个替换处理请求的action的拦截器
举个例子,有两个struts2的action:OneAction和OtherAction,原本是OneAction处理某个请求(比如abc.action),现在我要在st...
举个例子,有两个struts2的action:OneAction和OtherAction,原本是OneAction处理某个请求(比如abc.action),现在我要在struts.xml中给OneAction配置一个拦截器,让abc.action请求由OtherAction处理,这个拦截器怎么实现(不要用过滤器实现)?在线等答案!
展开
2个回答
展开全部
额,实现倒是没问题,可能有这方面的需求吧,我以前还没遇到过。
1. LoginActionInterceptor.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginActionInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {
ActionContext actionContext = actioninvocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) actionContext
.get(StrutsStatics.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) actionContext
.get(StrutsStatics.HTTP_RESPONSE);
request.getRequestDispatcher("/homepage.jsp") // 此处亦可为XX.action
.forward(request, response);
return null;
}
}
不过需要注意的是,这种是属于forward方式,默认情况不能为struts2的filter拦截,所以需要在web.xml中为filter-mapping配置:
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
至于配置REQUEST,是为了让直接访问jsp或者action的方式也能被struts的核心filter拦截
1. LoginActionInterceptor.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginActionInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {
ActionContext actionContext = actioninvocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) actionContext
.get(StrutsStatics.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) actionContext
.get(StrutsStatics.HTTP_RESPONSE);
request.getRequestDispatcher("/homepage.jsp") // 此处亦可为XX.action
.forward(request, response);
return null;
}
}
不过需要注意的是,这种是属于forward方式,默认情况不能为struts2的filter拦截,所以需要在web.xml中为filter-mapping配置:
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
至于配置REQUEST,是为了让直接访问jsp或者action的方式也能被struts的核心filter拦截
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询