博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2源码分析----StrutsPrepareAndExecuteFilter
阅读量:5423 次
发布时间:2019-06-15

本文共 4571 字,大约阅读时间需要 15 分钟。

这个是struts2的核心过滤器(Filter),代码如下:

1 /*  2  * $Id: DefaultActionSupport.java 651946 2008-04-27 13:41:38Z apetrelli $  3  *  4  * Licensed to the Apache Software Foundation (ASF) under one  5  * or more contributor license agreements.  See the NOTICE file  6  * distributed with this work for additional information  7  * regarding copyright ownership.  The ASF licenses this file  8  * to you under the Apache License, Version 2.0 (the  9  * "License"); you may not use this file except in compliance 10  * with the License.  You may obtain a copy of the License at 11  * 12  *  http://www.apache.org/licenses/LICENSE-2.0 13  * 14  * Unless required by applicable law or agreed to in writing, 15  * software distributed under the License is distributed on an 16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17  * KIND, either express or implied.  See the License for the 18  * specific language governing permissions and limitations 19  * under the License. 20  */ 21 package org.apache.struts2.dispatcher.ng.filter; 22  23 import org.apache.struts2.StrutsStatics; 24 import org.apache.struts2.dispatcher.Dispatcher; 25 import org.apache.struts2.dispatcher.mapper.ActionMapping; 26 import org.apache.struts2.dispatcher.ng.ExecuteOperations; 27 import org.apache.struts2.dispatcher.ng.InitOperations; 28 import org.apache.struts2.dispatcher.ng.PrepareOperations; 29  30 import javax.servlet.*; 31 import javax.servlet.http.HttpServletRequest; 32 import javax.servlet.http.HttpServletResponse; 33 import java.io.IOException; 34 import java.util.List; 35 import java.util.regex.Pattern; 36  37 /** 38  * Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use 39  * when you don't have another filter that needs access to action context information, such as Sitemesh. 40  */ 41 public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter { 42     protected PrepareOperations prepare; 43     protected ExecuteOperations execute; 44     protected List
excludedPatterns = null; 45 46 public void init(FilterConfig filterConfig) throws ServletException { 47 InitOperations init = new InitOperations(); 48 try { 49 FilterHostConfig config = new FilterHostConfig(filterConfig); 50 init.initLogging(config); 51 Dispatcher dispatcher = init.initDispatcher(config); 52 init.initStaticContentLoader(config, dispatcher); 53 54 prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher); 55 execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher); 56 this.excludedPatterns = init.buildExcludedPatternsList(dispatcher); 57 58 postInit(dispatcher, filterConfig); 59 } finally { 60 init.cleanup(); 61 } 62 63 } 64 65 /** 66 * Callback for post initialization 67 */ 68 protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) { 69 } 70 71 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 72 73 HttpServletRequest request = (HttpServletRequest) req; 74 HttpServletResponse response = (HttpServletResponse) res; 75 76 try { 77 prepare.setEncodingAndLocale(request, response); 78 prepare.createActionContext(request, response); 79 prepare.assignDispatcherToThread(); 80 if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { 81 chain.doFilter(request, response); 82 } else { 83 request = prepare.wrapRequest(request); 84 ActionMapping mapping = prepare.findActionMapping(request, response, true); 85 if (mapping == null) { 86 boolean handled = execute.executeStaticResourceRequest(request, response); 87 if (!handled) { 88 chain.doFilter(request, response); 89 } 90 } else { 91 execute.executeAction(request, response, mapping); 92 } 93 } 94 } finally { 95 prepare.cleanupRequest(request); 96 } 97 } 98 99 public void destroy() {100 prepare.cleanupDispatcher();101 }102 }
doFilter方法的作用是捕获以.action为结尾的请求,然后给他分配action,处理request,返回response!

转载于:https://www.cnblogs.com/imhurley/archive/2012/07/17/2594548.html

你可能感兴趣的文章
八:Razor(MVC框架视图引擎)
查看>>
java代码编辑器 pdf文件预览 主流SSM 代码生成器 shrio redis websocket即时通讯
查看>>
final
查看>>
Win8下更改Chrome缓存目录
查看>>
django框架小技巧
查看>>
(八)8-3多线程共享变量
查看>>
Parameter配置文件获取
查看>>
[Operating System] {ud923} P3L1: Scheduling
查看>>
(转载)悟透JavaScript
查看>>
java后端发送http请求使用RestTemplate
查看>>
PAT1002
查看>>
避免商品超卖的4种方案
查看>>
AtCoder - 1999 Candy Piles
查看>>
python中calendar模块的常用方法
查看>>
Checklist: 2019 05.01 ~ 06.30
查看>>
【最短路】Vijos P1022Victoria的舞会2
查看>>
(原创)大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 线性回归分析算法)...
查看>>
调整Tomcat的并发线程到5000+
查看>>
KextWizard 的使用方法;以及Kext安装的几种工具下载
查看>>
生成折扣日记账
查看>>