Struts2拦截器原理
struts2版本:2.2.3
当一个请求来了后,从org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 开始处理
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req; try {
if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { //包装request,对有文件上传的特殊处理下 request = prepare.wrapRequest(request); //查找对应的ActionMapping ActionMapping mapping = prepare.findActionMapping(request, response, true); //如果找不到ActionMapping则当作静态资源来处理 if (mapping == null) { //使用ActionMapping来执行action execute.executeAction(request, response, mapping); |
跟踪execute.executeAction(),则到了 org.apache.struts2.dispatcher.Dispatcher,如下:
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException { Map<String, Object> extraContext = createContextMap(request, response, mapping, context); // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action String timerKey = "Handling request from Dispatcher"; Configuration config = configurationManager.getConfiguration();
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy( request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack()); // if the ActionMapping says to go straight to a result, do it! //执行action proxy.execute(); // If there was a previous value stack then set it back onto the request |
DefaultActionProxyFactory创建ActionProxy,在com.opensymphony.xwork2.DefaultActionProxyFactory:
public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map<String, Object> extraContext, boolean executeResult, boolean cleanupContext) { ActionInvocation inv = new DefaultActionInvocation(extraContext, true); container.inject(inv); return createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext); } |
接下来看看 org.apache.struts2.impl.StrutsActionProxy的execute()方法,如下:
public String execute() throws Exception { ActionContext previous = ActionContext.getContext(); ActionContext.setContext(invocation.getInvocationContext()); try { //这里就是调用拦截器的入口了 return invocation.invoke(); } finally { if (cleanupContext) ActionContext.setContext(previous); } } |
最关键的,com.opensymphony.xwork2.DefaultActionInvocation.invoke()方法,这个DefaultActionInvocation是ActionInvocation的一个实现类,如下:
//保存了执行当前action方法时需要调用的拦截器栈,按照struts.xml中配制的拦截器顺序,从前到后,依次加入到了这个Iterator里面 protected Iterator<InterceptorMapping> interceptors; public String invoke() throws Exception { if (executed) { //执行拦截器的intercept()方法,并将当前ActionInvocation对象传递给这个方法 resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this); // this is needed because the result will be executed, then control will return to the Interceptor, which will String _profileKey = "preResultListener: "; // now execute the result, if we're supposed to executed = true; return resultCode; |
基本原理到此为止,下面弄个小例子再说明一下:
//拦截器,相当于struts2的拦截器 public interface Interceptor { String intercept(InvocationContext context); } //很多拦截器的实现 public class ExceptionInterceptor implements Interceptor { public String intercept(InvocationContext context) { public String intercept(InvocationContext context) { public String intercept(InvocationContext context) { //执行拦截器的invocation上下文,相当于struts2的ActionInvocation public class InvocationContext { // 这里存放当前执行当前action所需要执行的拦截器栈 public InvocationContext() { public String invoke() { private String executeAction() { //模拟请求进行测试 public class Test { public static void main(String[] args) { |
最新文章
- 【转】浅谈html5网页内嵌视频
- HTML5实战——svg学习
- dll 导出函数名的那些事
- Entity Framework6 with Oracle
- Struts2文件上传功能浅析
- Oracle数据库sys和system用户的默认密码及如何修改密码
- SPOJ Query on a tree V
- 以写作为例说下IT人如何培养挣钱DNA
- SpringCloud Config手动刷新及自动刷新
- jQuery相关用法
- 关于<;checkbox>;checked默认问题
- [转]skynet Lua中的协程
- day 018 面向对象--约束和异常处理
- MongoDB中的一些坑( 2.4.10 版本)
- 20165207 Exp4 恶意代码分析
- Django查询orm的前一天,前一周,一个月的数据
- kali_install_complete_no_sound
- 实用爬虫-01-检测爬虫的 IP
- iOS-【UIDynamic-UIKit动力学】
- sublime 非常好用的注释工具