nojave 2009-10-29 17:45
浏览 356
已采纳

Struts2 过滤器和拦截器执行的顺序

在web.xml ,定义首页位置:
[code="xml"]

index.jsp

[/code]

并配置了一个filter,用于过滤对jsp的直接访问.
[code="xml"]

indexfilter
com.prodinfo.filter.IndexFilter


indexfilter
/index.jsp

[/code]

[code="java"]
......
public class IndexFilter implements Filter {
......
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

    HttpServletRequest re = (HttpServletRequest) request;
    HttpSession session = re.getSession();
    //redirect to action

    request.getRequestDispatcher("loadindex.action?id=xxx").forward(
            request, response);
    System.out.println("i am filter");
}
     ......

}
[/code]

struts.xml中配置了interceptor,拦截用户的请求,在action执行之前,load页面信息,然后继续.
[code="xml"]

class="loadHeaderFooter">







method="loadCompanyInfo">
/index.jsp


[/code]

[code="java"]
......
public String intercept(ActionInvocation invocation) throws Exception {
//System.out.println("Before Action");
//get header & footer infomation
List result = ci.getHeaderFooter("xxx");
......
System.out.println("i am interceptor");

    final String res = invocation.invoke();
    //System.out.println("After Action");
    return res;
}

......
[/code]

我理解的执行顺序是()index.jsp->(filter)"loadindex.action?id=xxx"->(interceptor)"loadHeaderFooter"->(Action)"loadindex"->(jsp)index.jsp
可是现在执行的顺序是先interceptor再filter,程序运行打印结果是:
i am interceptor
loading company info of null

i am filter
请问我该在哪里配置filter 和 interceptor的顺序呢? 谢谢!
[b]问题补充:[/b]
谢谢 蔡华江 的回复.
即是说我应该从web.xml里filter的位置来下手找我的问题的原因?
[b]问题补充:[/b]
去掉了interceptor,发现还是先执行的action再进的filter. Struts2的执行顺序一定是先action再filter? 已经把FilterDispatcher放到web.xml最上面了.

  • 写回答

1条回答 默认 最新

  • CaiHuajiang 2009-10-29 18:05
    关注

    基本正确,但是有一个地方例外。filter。
    因为struts2使用的也是filter,因此filter的位置决定了filter和action的执行顺序。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突