%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
拦截器演示 - Struts2
🛡️ Struts2 拦截器演示
实验观察点:
- 连续刷新页面,观察 Action 调用次数如何累积
- 切换到“API 限流栈”,理解为什么拦截器顺序会影响结果
- 注意执行时间显示,这就是拦截器注入到 request 的数据
📊 执行统计
本次请求执行时间: ms
Action 调用统计:
🔗 拦截器链演示
当前使用: customStack (日志 + 计时 + 监控 + 默认栈)
使用 API 限流栈
API 栈包含: 日志 + 限流 + 验证 + 计时 + 默认栈
📚 拦截器执行顺序
请求 → LoggingInterceptor → TimingInterceptor → MonitorInterceptor → Action
响应 ← LoggingInterceptor ← TimingInterceptor ← MonitorInterceptor ← Result
注意: 拦截器像洋葱一样,先进入的后退出
💡 拦截器核心概念
- Interceptor 接口: init() → intercept() → destroy()
- intercept() 方法: 调用 invocation.invoke() 继续链
- 拦截器栈: 多个拦截器按顺序组成链
- 责任链模式: 每个拦截器决定是否继续
← 返回学习中心