• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

struts1之ActionServlet的实例详解

java 搞代码 4年前 (2022-01-09) 13次浏览 已收录 0个评论

这篇文章主要介绍了struts1之ActionServlet详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

在web.xml中我们除了配置ActionServlet还配置了一些初始化参数信息,首先我们看第一个config参数,这里配置的是/WEB-INF/struts-config.xml,因为要下面传递一个这样一个配置信息,这个xml文件名是struts1标准的名字,所以这里这个初始化信息完全可以删除,如果不用这个标准名称这里就必须要在这里配置。现在我们配置的是标准名字,所以我们可以删除,这是为什么呢?这里要看ActionServlet源代码才可以。

从图片上我们能看到ActionServlet中已经写好了默认的config信息了,就是标准名字。所以这里删除也是可以的。
在看下面的debug和detail参数,这两个参数信息是有关日志信息级别的设置,主要关于解析配置文件/WEB-INF/struts-config.xml级别的初始化参数。这里这两个参数可以完全去掉也不影响。

最后还有一个load-on-startup配置,这个是初始化servlet级别的初始化信息,这个参数如果大于等于0就是说明在服务器一启动就把servlet初始化,也就是调用ActionServlet的init方法,这个也可以到ActionServlet的源代码中去查找。

当ActionServlet初始化的时候就会读取/WEB-INF/struts-config.xml信息到内存中,读到内存是以什么样的形式展现的呢?我们现在可以看一下以前博客的那个mvc实例,那里面读取配置文件中的信息是以Actionmapping的形式展现的。另外servlet-mapping的配置就不讲解了,这个都知道就是匹配url路径的,当遇到url-pattern的路径时候就会实例化Actionservlet。

通过这篇文章我们知道了当我们请求的时候ActionServlet是怎样实例化的,也知道为什么我们要配置web.xml信息了。那么我们为什么要配置/WEB-INF/struts-config.xml文件,ActionServlet是如何传递请求的,如何和ActionForm、ActionMapping、Action等交互的最终完成用户请求的呢?

我们先从ActionServlet源代码的init方法开始。因为ActionServlet就是一个Servlet,它也是具有典型的那几个方法init、doget、dopost等方法。既然是初始化,那么我们就要看init方法。Init方法的源代码如下:

/**    * <p>Initialize this se<em>本文来源gao.dai.ma.com搞@代*码(网$</em>rvlet. Most of the processing has been factored into    * support methods so that you can overrideparticular functionality at a    * fairly granular level.</p>    *    * @exception ServletException if we cannotconfigure ourselves correctly    */   publicvoidinit() throwsServletException {       // Wraps the entire initialization in a try/catch tobetter handle     // unexpected exceptions and errors to provide better feedback     // to the developer     try {       initInternal();       initOther();       initServlet();          getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);       initModuleConfigFactory();       // Initialize modules as needed       ModuleConfig moduleConfig =initModuleConfig("", config);       initModuleMessageResources(moduleConfig);       initModuleDataSources(moduleConfig);       initModulePlugIns(moduleConfig);       moduleConfig.freeze();          Enumeration names =getServletConfig().getInitParameterNames();       while (names.hasMoreElements()) {         String name = (String)namesnextElement();         if (!name.startsWith("config/")) {           continue;         }         String prefix =name.substring(6);         moduleConfig = initModuleConfig           (prefix,getServletConfig().getInitParameter(name));         initModuleMessageResources(moduleConfig);         initModuleDataSources(moduleConfig);         initModulePlugIns(moduleConfig);         moduleConfig.freeze();       }          this.initModulePrefixes(this.getServletContext());          thisdestroyConfigDigester();     } catch (UnavailableException ex) {       throw ex;     } catch (Throwable t) {         // The follow error message is not retrieved from internal message       // resources as they may not have been able to have been       // initialized       logerror("Unable to initialize Struts ActionServlet due to an "         + "unexpected exception or error thrown, so marking the "         + "servlet as unavailable. Mostlikely, this is due to an "         + "incorrect or missing library dependency.", t);       throw new UnavailableException(t.getMessage());     }   }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:struts1之ActionServlet的实例详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址