标签:
http://www.iteye.com/topic/829843
一、概述
Struts2的核心是一个Filter,Action可以脱离web容器,那么是什么让http请求和action关联在一起的,下面我们深入源码来分析下Struts2是如何工作的。
FilterDispatcher API 写道
Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one
鉴于常规情况官方推荐使用StrutsPrepareAndExecuteFilter替代FilterDispatcher,我们此文 将剖析StrutsPrepareAndExecuteFilter,其在工程中作为一个Filter配置在web.xml中,配置如下:
- < filter >
- < filter-name > struts2 </ filter-name >
- < filter-class > org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</ filter-class >
- </ filter >
- < filter-mapping >
- < filter-name > struts2 </ filter-name >
- < url-pattern > /* </ url-pattern >
- </ filter-mapping >
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
二、源码属性方法简介
下面我们研究下StrutsPrepareAndExecuteFilter源码,类的主要信息如下:
StrutsPrepareAndExecuteFilter与普通的Filter并无区别,方法除继承自Filter外,仅有一个回调方法,第三部分我 们将按照Filter方法调用顺序,由init—>doFilter—>destroy顺序地分析源码。
三、源码剖析
1、init方法
init是Filter第一个运行的方法,我们看下struts2的核心Filter在调用init方法初始化时做哪些工作:
- public void init(FilterConfig filterConfig) throws ServletException {
- InitOperations init = new InitOperations();
- try {
- FilterHostConfig config = new FilterHostConfig(filterConfig);
- init.initLogging(config);
- Dispatcher dispatcher = init.initDispatcher(config);
- init.initStaticContentLoader(config, dispatcher);
- prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
- execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
- this .excludedPatterns = init.buildExcludedPatternsList(dispatcher);
- postInit(dispatcher, filterConfig);
- } finally {
- init.cleanup();
- }
- }
- public void init(FilterConfig filterConfig) throws ServletException {
- InitOperations init = new InitOperations();
- try {
- FilterHostConfig config = new FilterHostConfig(filterConfig);
- init.initLogging(config);
-
-
-
-
-
-
-
- Dispatcher dispatcher = init.initDispatcher(config);
- init.initStaticContentLoader(config, dispatcher);
- prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
- execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
- this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
- postInit(dispatcher, filterConfig);
- } finally {
- init.cleanup();
- }
- }
首先看下FilterHostConfig ,源码如下:
- public class FilterHostConfig implements HostConfig {
-
- private FilterConfig config;
-
- public FilterHostConfig(FilterConfig config) {
- this .config = config;
- }
-
- public String getInitParameter(String key) {
- return config.getInitParameter(key);
- }
-
- public Iterator<String> getInitParameterNames() {
- return MakeIterator.convert(config.getInitParameterNames());
- }
-
- public ServletContext getServletContext() {
- return config.getServletContext();
- }
- }
- public class FilterHostConfig implements HostConfig {
-
- private FilterConfig config;
-
- public FilterHostConfig(FilterConfig config) {
- this.config = config;
- }
-
- public String getInitParameter(String key) {
- return config.getInitParameter(key);
- }
-
- public Iterator<String> getInitParameterNames() {
- return MakeIterator.convert(config.getInitParameterNames());
- }
-
- public ServletContext getServletContext() {
- return config.getServletContext();
- }
- }
只有短短的几行代码,getInitParameterNames是这个类的核心,将Filter初始化参数名称有枚举类型转为Iterator。此类的主要作为是对filterConfig 封装。
重点来了,创建并初始化Dispatcher
- public Dispatcher initDispatcher( HostConfig filterConfig ) {
- Dispatcher dispatcher = createDispatcher(filterConfig);
- dispatcher.init();
- return dispatcher;
- }
- public Dispatcher initDispatcher( HostConfig filterConfig ) {
- Dispatcher dispatcher = createDispatcher(filterConfig);
- dispatcher.init();
- return dispatcher;
- }
创建Dispatcher,会读取 filterConfig 中的配置信息,将配置信息解析出来,封装成为一个Map,然后根绝servlet上下文和参数Map构造Dispatcher :
- private Dispatcher createDispatcher( HostConfig filterConfig ) {
- Map<String, String> params = new HashMap<String, String>();
- for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {
- String name = (String) e.next();
- String value = filterConfig.getInitParameter(name);
- params.put(name, value);
- }
- return new Dispatcher(filterConfig.getServletContext(), params);
- }
- private Dispatcher createDispatcher( HostConfig filterConfig ) {
- Map<String, String> params = new HashMap<String, String>();
- for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {
- String name = (String) e.next();
- String value = filterConfig.getInitParameter(name);
- params.put(name, value);
- }
- return new Dispatcher(filterConfig.getServletContext(), params);
- }
Dispatcher初始化,加载struts2的相关配置文件,将按照顺序逐一加载:default.properties,struts-default.xml,struts-plugin.xml,struts.xml,……
- public void init() {
-
- if (configurationManager == null ) {
- configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
- }
-
- try {
-
- init_DefaultProperties();
-
- init_TraditionalXmlConfigurations();
- init_LegacyStrutsProperties();
-
- init_CustomConfigurationProviders();
-
- init_FilterInitParameters() ;
- init_AliasStandardObjects() ;
-
- Container container = init_PreloadConfiguration();
- container.inject(this );
- init_CheckConfigurationReloading(container);
- init_CheckWebLogicWorkaround(container);
-
- if (!dispatcherListeners.isEmpty()) {
- for (DispatcherListener l : dispatcherListeners) {
- l.dispatcherInitialized(this );
- }
- }
- } catch (Exception ex) {
- if (LOG.isErrorEnabled())
- LOG.error("Dispatcher initialization failed" , ex);
- throw new StrutsException(ex);
- }
- }
- public void init() {
-
- if (configurationManager == null) {
- configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
- }
-
- try {
-
- init_DefaultProperties();
-
- init_TraditionalXmlConfigurations();
- init_LegacyStrutsProperties();
-
- init_CustomConfigurationProviders();
-
- init_FilterInitParameters() ;
- init_AliasStandardObjects() ;
-
- Container container = init_PreloadConfiguration();
- container.inject(this);
- init_CheckConfigurationReloading(container);
- init_CheckWebLogicWorkaround(container);
-
- if (!dispatcherListeners.isEmpty()) {
- for (DispatcherListener l : dispatcherListeners) {
- l.dispatcherInitialized(this);
- }
- }
- } catch (Exception ex) {
- if (LOG.isErrorEnabled())
- LOG.error("Dispatcher initialization failed", ex);
- throw new StrutsException(ex);
- }
- }
初始化default.properties,具体的初始化操作在DefaultPropertiesProvider类中
- private void init_DefaultProperties() {
- configurationManager.addConfigurationProvider(new DefaultPropertiesProvider());
- }
- private void init_DefaultProperties() {
- configurationManager.addConfigurationProvider(new DefaultPropertiesProvider());
- }
下面我们看下DefaultPropertiesProvider类源码:
- public void register(ContainerBuilder builder, LocatableProperties props)
- throws ConfigurationException {
-
- Settings defaultSettings = null ;
- try {
- defaultSettings = new PropertiesSettings( "org/apache/struts2/default" );
- } catch (Exception e) {
- throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties" , e);
- }
-
- loadSettings(props, defaultSettings);
- }
- public void register(ContainerBuilder builder, LocatableProperties props)
- throws ConfigurationException {
-
- Settings defaultSettings = null;
- try {
- defaultSettings = new PropertiesSettings("org/apache/struts2/default");
- } catch (Exception e) {
- throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);
- }
-
- loadSettings(props, defaultSettings);
- }
其他的我们再次省略,大家可以浏览下各个初始化操作都加载了那些文件
3、doFilter方法
doFilter是过滤器的执行方法,它拦截提交的HttpServletRequest请求,HttpServletResponse响应,作为strtus2的核心拦截器,在doFilter里面到底做了哪些工作,我们将逐行解读其源码,源码如下:
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
-
- HttpServletRequest request = (HttpServletRequest) req;
- HttpServletResponse response = (HttpServletResponse) res;
-
- try {
-
- prepare.setEncodingAndLocale(request, response);
-
- prepare.createActionContext(request, response);
- prepare.assignDispatcherToThread();
- if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
- chain.doFilter(request, response);
- } else {
- request = prepare.wrapRequest(request);
- ActionMapping mapping = prepare.findActionMapping(request, response, true );
- if (mapping == null ) {
- boolean handled = execute.executeStaticResourceRequest(request, response);
- if (!handled) {
- chain.doFilter(request, response);
- }
- } else {
- execute.executeAction(request, response, mapping);
- }
- }
- } finally {
- prepare.cleanupRequest(request);
- }
- }
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
-
- HttpServletRequest request = (HttpServletRequest) req;
- HttpServletResponse response = (HttpServletResponse) res;
-
- try {
-
- prepare.setEncodingAndLocale(request, response);
-
- prepare.createActionContext(request, response);
- prepare.assignDispatcherToThread();
- if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
- chain.doFilter(request, response);
- } else {
- request = prepare.wrapRequest(request);
- ActionMapping mapping = prepare.findActionMapping(request, response, true);
- if (mapping == null) {
- boolean handled = execute.executeStaticResourceRequest(request, response);
- if (!handled) {
- chain.doFilter(request, response);
- }
- } else {
- execute.executeAction(request, response, mapping);
- }
- }
- } finally {
- prepare.cleanupRequest(request);
- }
- }
setEncodingAndLocale调用了dispatcher方法的prepare方法:
- public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {
- dispatcher.prepare(request, response);
- }
- public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {
- dispatcher.prepare(request, response);
- }
下面我们看下prepare方法,这个方法很简单只是设置了encoding 、locale ,做的只是一些辅助的工作:
- public void prepare(HttpServletRequest request, HttpServletResponse response) {
- String encoding = null ;
- if (defaultEncoding != null ) {
- encoding = defaultEncoding;
- }
-
- Locale locale = null ;
- if (defaultLocale != null ) {
- locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
- }
-
- if (encoding != null ) {
- try {
- request.setCharacterEncoding(encoding);
- } catch (Exception e) {
- LOG.error("Error setting character encoding to ‘" + encoding + "‘ - ignoring." , e);
- }
- }
-
- if (locale != null ) {
- response.setLocale(locale);
- }
-
- if (paramsWorkaroundEnabled) {
- request.getParameter("foo" );
- }
- }
- public void prepare(HttpServletRequest request, HttpServletResponse response) {
- String encoding = null;
- if (defaultEncoding != null) {
- encoding = defaultEncoding;
- }
-
- Locale locale = null;
- if (defaultLocale != null) {
- locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
- }
-
- if (encoding != null) {
- try {
- request.setCharacterEncoding(encoding);
- } catch (Exception e) {
- LOG.error("Error setting character encoding to ‘" + encoding + "‘ - ignoring.", e);
- }
- }
-
- if (locale != null) {
- response.setLocale(locale);
- }
-
- if (paramsWorkaroundEnabled) {
- request.getParameter("foo");
- }
- }
Action上下文创建(重点)
ActionContext是一个容器,这个容易主要存储request、session、application、parameters等相关信 息.ActionContext是一个线程的本地变量,这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问 题。其实质是一个Map,key是标示request、session、……的字符串,值是其对应的对象:
- static ThreadLocal actionContext = new ThreadLocal();
- Map<String, Object> context;
- static ThreadLocal actionContext = new ThreadLocal();
- Map<String, Object> context;
下面我们看下如何创建action上下文的,代码如下:
- public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
- ActionContext ctx;
- Integer counter = 1 ;
- Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
- if (oldCounter != null ) {
- counter = oldCounter + 1 ;
- }
-
- ActionContext oldContext = ActionContext.getContext();
- if (oldContext != null ) {
-
- ctx = new ActionContext( new HashMap<String, Object>(oldContext.getContextMap()));
- } else {
- ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
- stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
-
- ctx = new ActionContext(stack.getContext());
- }
- request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
-
- ActionContext.setContext(ctx);
- return ctx;
- }
- public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
- ActionContext ctx;
- Integer counter = 1;
- Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
- if (oldCounter != null) {
- counter = oldCounter + 1;
- }
-
- ActionContext oldContext = ActionContext.getContext();
- if (oldContext != null) {
-
- ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
- } else {
- ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
- stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
-
- ctx = new ActionContext(stack.getContext());
- }
- request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
-
- ActionContext.setContext(ctx);
- return ctx;
- }
上面代码中dispatcher.createContextMap,如何封装相关参数:
- public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
- ActionMapping mapping, ServletContext context) {
-
-
- Map requestMap = new RequestMap(request);
-
-
- Map params = new HashMap(request.getParameterMap());
-
-
- Map session = new SessionMap(request);
-
-
- Map application = new ApplicationMap(context);
-
- Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context);
-
- if (mapping != null ) {
- extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
- }
- return extraContext;
- }
- public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
- ActionMapping mapping, ServletContext context) {
-
-
- Map requestMap = new RequestMap(request);
-
-
- Map params = new HashMap(request.getParameterMap());
-
-
- Map session = new SessionMap(request);
-
-
- Map application = new ApplicationMap(context);
-
- Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context);
-
- if (mapping != null) {
- extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
- }
- return extraContext;
- }
我们简单看下RequestMap,其他的省略。RequestMap类实现了抽象Map,故其本身是一个Map,主要方法实现:
- public Object get(Object key) {
- return request.getAttribute(key.toString());
- }
- public Object put(Object key, Object value) {
- Object oldValue = get(key);
- entries = null ;
- request.setAttribute(key.toString(), value);
- return oldValue;
- }
- public Object get(Object key) {
- return request.getAttribute(key.toString());
- }
- public Object put(Object key, Object value) {
- Object oldValue = get(key);
- entries = null;
- request.setAttribute(key.toString(), value);
- return oldValue;
- }
下面是源码展示了如何执行Action控制器:
- public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
- dispatcher.serviceAction(request, response, servletContext, mapping);
- }
-
- public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
- ActionMapping mapping) throws ServletException {
-
- Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
-
-
- ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
- boolean nullStack = stack == null ;
- if (nullStack) {
- ActionContext ctx = ActionContext.getContext();
- if (ctx != null ) {
- stack = ctx.getValueStack();
- }
- }
- if (stack != null ) {
- extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
- }
-
- String timerKey = "Handling request from Dispatcher" ;
- try {
- UtilTimerStack.push(timerKey);
-
- String namespace = mapping.getNamespace();
-
- String name = mapping.getName();
-
- String method = mapping.getMethod();
-
- Configuration config = configurationManager.getConfiguration();
-
- ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
- namespace, name, method, extraContext, true , false );
-
- request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
-
-
-
- if (mapping.getResult() != null ) {
- Result result = mapping.getResult();
- result.execute(proxy.getInvocation());
- } else {
- proxy.execute();
- }
-
-
- if (!nullStack) {
- request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
- }
- } catch (ConfigurationException e) {
-
- if (devMode) {
- String reqStr = request.getRequestURI();
- if (request.getQueryString() != null ) {
- reqStr = reqStr + "?" + request.getQueryString();
- }
- LOG.error("Could not find action or result/n" + reqStr, e);
- }
- else {
- LOG.warn("Could not find action or result" , e);
- }
- sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
- } catch (Exception e) {
- sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
- } finally {
- UtilTimerStack.pop(timerKey);
- }
- }
- public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
- dispatcher.serviceAction(request, response, servletContext, mapping);
- }
-
- public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
- ActionMapping mapping) throws ServletException {
-
- Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
-
-
- ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
- boolean nullStack = stack == null;
- if (nullStack) {
- ActionContext ctx = ActionContext.getContext();
- if (ctx != null) {
- stack = ctx.getValueStack();
- }
- }
- if (stack != null) {
- extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
- }
-
- String timerKey = "Handling request from Dispatcher";
- try {
- UtilTimerStack.push(timerKey);
-
- String namespace = mapping.getNamespace();
-
- String name = mapping.getName();
-
- String method = mapping.getMethod();
-
- Configuration config = configurationManager.getConfiguration();
-
- ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
- namespace, name, method, extraContext, true, false);
-
- request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
-
-
-
- if (mapping.getResult() != null) {
- Result result = mapping.getResult();
- result.execute(proxy.getInvocation());
- } else {
- proxy.execute();
- }
-
-
- if (!nullStack) {
- request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
- }
- } catch (ConfigurationException e) {
-
- if(devMode) {
- String reqStr = request.getRequestURI();
- if (request.getQueryString() != null) {
- reqStr = reqStr + "?" + request.getQueryString();
- }
- LOG.error("Could not find action or result/n" + reqStr, e);
- }
- else {
- LOG.warn("Could not find action or result", e);
- }
- sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
- } catch (Exception e) {
- sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
- } finally {
- UtilTimerStack.pop(timerKey);
- }
- }
文中对如何解析Struts.xml,如何将URL与action映射匹配为分析,有需要的我后续补全,因为 StrutsXmlConfigurationProvider继承XmlConfigurationProvider,并在register方法回调父 类的register,有兴趣的可以深入阅读下下XmlConfigurationProvider源码:
- public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {
- if (servletContext != null && !containerBuilder.contains(ServletContext. class)) {
- containerBuilder.factory(ServletContext.class , new Factory<ServletContext>() {
- public ServletContext create(Context context) throws Exception {
- return servletContext;
- }
- });
- }
-
- super .register(containerBuilder, props);
- }
- public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {
- if (servletContext != null && !containerBuilder.contains(ServletContext.class)) {
- containerBuilder.factory(ServletContext.class, new Factory<ServletContext>() {
- public ServletContext create(Context context) throws Exception {
- return servletContext;
- }
- });
- }
-
- super.register(containerBuilder, props);
- }
struts2-core-2.2.1.jar包中struts-2.1.7.dtd对于Action的定义如下:
- <!ELEMENT action (param|result|interceptor-ref|exception-mapping)* >
- <!ATTLIST action
- name CDATA #REQUIRED
- class CDATA #IMPLIED
- method CDATA #IMPLIED
- converter CDATA #IMPLIED
- >
- <!ELEMENT action (param|result|interceptor-ref|exception-mapping)*>
- <!ATTLIST action
- name CDATA #REQUIRED
- class CDATA #IMPLIED
- method CDATA #IMPLIED
- converter CDATA #IMPLIED
- >
从上述DTD中可见Action元素可以含有name 、class 、method 、converter 属性。
XmlConfigurationProvider解析struts.xml配置的Action元素:
- protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {
- String name = actionElement.getAttribute("name" );
- String className = actionElement.getAttribute("class" );
- String methodName = actionElement.getAttribute("method" );
- Location location = DomHelper.getLocationObject(actionElement);
-
- if (location == null ) {
- LOG.warn("location null for " + className);
- }
-
- methodName = (methodName.trim().length() > 0 ) ? methodName.trim() : null ;
-
-
-
- if (StringUtils.isEmpty(className)) {
-
-
-
- } else {
- if (!verifyAction(className, name, location)) {
- if (LOG.isErrorEnabled())
- LOG.error("Unable to verify action [#0] with class [#1], from [#2]", name, className, location.toString());
- return ;
- }
- }
-
-
-
- Map<String, ResultConfig> results;
- try {
- results = buildResults(actionElement, packageContext);
- } catch (ConfigurationException e) {
- throw new ConfigurationException( "Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);
- }
-
- List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);
-
- List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);
-
- ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className)
- .methodName(methodName)
- .addResultConfigs(results)
- .addInterceptors(interceptorList)
- .addExceptionMappings(exceptionMappings)
- .addParams(XmlHelper.getParams(actionElement))
- .location(location)
- .build();
- packageContext.addActionConfig(name, actionConfig);
-
- if (LOG.isDebugEnabled()) {
- LOG.debug("Loaded " + (StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/" ) : "" ) + name + " in ‘" + packageContext.getName() + "‘ package:" + actionConfig);
- }
- }
- protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {
- String name = actionElement.getAttribute("name");
- String className = actionElement.getAttribute("class");
- String methodName = actionElement.getAttribute("method");
- Location location = DomHelper.getLocationObject(actionElement);
-
- if (location == null) {
- LOG.warn("location null for " + className);
- }
-
- methodName = (methodName.trim().length() > 0) ? methodName.trim() : null;
-
-
-
- if (StringUtils.isEmpty(className)) {
-
-
-
- } else {
- if (!verifyAction(className, name, location)) {
- if (LOG.isErrorEnabled())
- LOG.error("Unable to verify action [#0] with class [#1], from [#2]", name, className, location.toString());
- return;
- }
- }
-
-
-
- Map<String, ResultConfig> results;
- try {
- results = buildResults(actionElement, packageContext);
- } catch (ConfigurationException e) {
- throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);
- }
-
- List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);
-
- List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);
-
- ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className)
- .methodName(methodName)
- .addResultConfigs(results)
- .addInterceptors(interceptorList)
- .addExceptionMappings(exceptionMappings)
- .addParams(XmlHelper.getParams(actionElement))
- .location(location)
- .build();
- packageContext.addActionConfig(name, actionConfig);
-
- if (LOG.isDebugEnabled()) {
- LOG.debug("Loaded " + (StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "") + name + " in ‘" + packageContext.getName() + "‘ package:" + actionConfig);
- }
- }
工作中不涉及Struts2,本周工作有个2天的空档期,稍微看了下struts2的文档,写了个demo,从源码的角度研究了下运行原理,如有分析不当请指出,我后续逐步完善更正,大家共同提高。
StrutsPrepareAndExecuteFilter(转)
标签:
原文地址:http://www.cnblogs.com/shootercheng/p/5901347.html