大家都知道,Struts控制器组件负责接受用户请求,更通模型,以及返回给用户合适的视图组件.
控制器将模型层和视图层分开,这样分离,可以为同一个模型开发出不同的视图.
下面时Struts的三大主要组件
ActionServlet组件:充当Struts框架的中央控制器
RequestProcessor组件:充当每个子应用模块的请求处理器
Action组件:真正来处理一项具体的业务.

一. Struts的init()方法
Struts应用中只存在ActionServlet的一个实例,Servlet容器在启动时,或者用户首次请求ActionServlet时加载ActionServlet类.在这两种情况下,servlet容器都会在ActionServlet容器被加载后立即执行它的init()方法,这可以保证ActionServlet处理用户请求时已经被初始化.

下面根据Init()讲述Struts的初始化过程

Java代码
  1. public void init() throws ServletException {
  2. // Wraps the entire initialization in a try/catch to better handle
  3. // unexpected exceptions and errors to provide better feedback
  4. // to the developer
  5. try {
  6. //调用initInternal()方法,初始化Struts框架内的消息资源,如与系统日志相关的通知,警告,和错误消息.
  7. 1)initInternal();
  8. //调用ininOther()方法,从web.xml文件中加载ActionServlet的初始化参数,如config参数
  9. 2)initOther();
  10. //调用initServlet()方法,从web.xml文件中加载ActionServlet的URL映射信息.同时还会注册web.xml文件和Struts配置文件所使用的DTD文件,这些DTD文件用户验证web.xml和struts配置文件的语法.其中方法里的 digester类负责解析web.xml,对字符串servletMapping属性进行初始化
  11. 3) initServlet();
  12. //把ActionServlet实例放到ServletContext里
  13. getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY,this);
  14. //初始化一个factory,用于创建moduleConfig
  15. initModuleConfigFactory();
  16. //,加载并解析默认struts配置文件/WEB-INF/struts-config.xml,同时创建MoudleConfig实例,放到ServletContext中
  17. 4)ModuleConfig moduleConfig = initModuleConfig("", config);
  18. //加载并初始化默认子应用模块的消息资源;讲解MessageResources对象,把它存储在ServletContext中.
  19. 5)initModuleMessageResources(moduleConfig);
  20. //加载并初始化默认子应用模块的数据源,如果在struts配置文件中没有定义<data-sources >元素,忽略这一流程.
  21. 6)initModuleDataSources(moduleConfig);
  22. //加载并初始化默认子应用的所有插件
  23. 7)initModulePlugIns(moduleConfig);
  24. //冻结moduleConfig(,在方法返回之前不能修改它,否则将抛出异常)
  25. moduleConfig.freeze();
  26. //如果还有其他子应用模块,将重复4-7步
  27. Enumeration names = getServletConfig().getInitParameterNames();
  28. while (names.hasMoreElements()) {
  29. String name = (String) names.nextElement();
  30. if (!name.startsWith("config/")) {
  31. continue;
  32. }
  33. String prefix = name.substring(6);
  34. moduleConfig = initModuleConfig
  35. (prefix, getServletConfig().getInitParameter(name));
  36. initModuleMessageResources(moduleConfig);
  37. initModuleDataSources(moduleConfig);
  38. initModulePlugIns(moduleConfig);
  39. moduleConfig.freeze();
  40. }
  41. //将各个子模块应用(除了默认的)的前缀存到一个字符数组中,并放到servletcontext中
  42. this.initModulePrefixes(this.getServletContext());
  43. //释放创建的用于读取配置文件的digester实例,释放内存
  44. this.destroyConfigDigester();
  45. } catch (UnavailableException ex) {
  46. throw ex;
  47. } catch (Throwable t) {
  48. // The follow error message is not retrieved from internal message
  49. // resources as they may not have been able to have been
  50. // initialized
  51. log.error("Unable to initialize Struts ActionServlet due to an "
  52. + "unexpected exception or error thrown, so marking the "
  53. + "servlet as unavailable. Most likely, this is due to an "
  54. + "incorrect or missing library dependency.", t);
  55. thrownew UnavailableException(t.getMessage());
  56. }
  57. }
public void init() throws ServletException {
// Wraps the entire initialization in a try/catch to better handle
// unexpected exceptions and errors to provide better feedback
// to the developer
try {
//调用initInternal()方法,初始化Struts框架内的消息资源,如与系统日志相关的通知,警告,和错误消息.
1)initInternal();
//调用ininOther()方法,从web.xml文件中加载ActionServlet的初始化参数,如config参数
2)initOther();
//调用initServlet()方法,从web.xml文件中加载ActionServlet的URL映射信息.同时还会注册web.xml文件和Struts配置文件所使用的DTD文件,这些DTD文件用户验证web.xml和struts配置文件的语法.其中方法里的 digester类负责解析web.xml,对字符串servletMapping属性进行初始化
3) initServlet();
//把ActionServlet实例放到ServletContext里
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
//初始化一个factory,用于创建moduleConfig
initModuleConfigFactory();
//,加载并解析默认struts配置文件/WEB-INF/struts-config.xml,同时创建MoudleConfig实例,放到ServletContext中
4)ModuleConfig moduleConfig = initModuleConfig("", config);
//加载并初始化默认子应用模块的消息资源;讲解MessageResources对象,把它存储在ServletContext中.
5)initModuleMessageResources(moduleConfig);
//加载并初始化默认子应用模块的数据源,如果在struts配置文件中没有定义<data-sources >元素,忽略这一流程.
6)initModuleDataSources(moduleConfig);
//加载并初始化默认子应用的所有插件
7)initModulePlugIns(moduleConfig);
//冻结moduleConfig(,在方法返回之前不能修改它,否则将抛出异常)
moduleConfig.freeze();
//如果还有其他子应用模块,将重复4-7步
Enumeration names = getServletConfig().getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
if (!name.startsWith("config/")) {
continue;
}
String prefix = name.substring(6);
moduleConfig = initModuleConfig
(prefix, getServletConfig().getInitParameter(name));
initModuleMessageResources(moduleConfig);
initModuleDataSources(moduleConfig);
initModulePlugIns(moduleConfig);
moduleConfig.freeze();
}
//将各个子模块应用(除了默认的)的前缀存到一个字符数组中,并放到servletcontext中
this.initModulePrefixes(this.getServletContext());
//释放创建的用于读取配置文件的digester实例,释放内存
this.destroyConfigDigester();
} 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
log.error("Unable to initialize Struts ActionServlet due to an "
+ "unexpected exception or error thrown, so marking the "
+ "servlet as unavailable.  Most likely, this is due to an "
+ "incorrect or missing library dependency.", t);
throw new UnavailableException(t.getMessage());
}
}

将各个子模块应用(除了默认的)的前缀存到一个字符数组中,并放到servletcontext中,对于默认的子应用模块,在appclication范围内存放他的MoudleConfig实例的key为“org.apache.struts.action.MODULE”,其他模块如/account,存放的key为org.apache.struts.action.MODULE/account,消息,数据源和插件等部分存在servletcontext的key和上述方法类似,不在说明.

二.ActionServlet的process方法
当ActionServlet接受到HTTP请求后,在doget()或doPost()方法中都会调用process()方法来处理请求.

Java代码
  1. publicvoid doGet(HttpServletRequest request,
  2. HttpServletResponse response)
  3. throws IOException, ServletException {
  4. process(request, response);
  5. }
 public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
} 
Java代码
  1. publicvoid doPost(HttpServletRequest request,
  2. HttpServletResponse response)
  3. throws IOException, ServletException {
  4. process(request, response);
  5. }
    public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
} 

下面是process方法,它看上去并不复杂,但他调用的其他方法比较复杂.

Java代码
  1. protectedvoid process(HttpServletRequest request, HttpServletResponse response)
  2. throws IOException, ServletException {
  3. //根据request里的信息从servletContext里找到相应的子模块ModuleConfig,和它下面的MessageResources,并放到request里,使其他组件可以方便的供request里取得应用配置信息和消息资源.
  4. ModuleUtils.getInstance().selectModule(request, getServletContext());
  5. //取出MoudleConfig实例config
  6. ModuleConfig config = getModuleConfig(request);
  7. //根据config里这个子模块的信息,从servletcontext里,取出这个子模块的RequestProcessor实例
  8. RequestProcessor processor = getProcessorForModule(config);
  9. //如果processor实例为空,就新建一个.同时放到servletcontext里.
  10. if (processor ==null) {
  11. processor = getRequestProcessor(config);
  12. }
  13. //调用RequestProcessor的process方法处理,
  14. processor.process(request, response);
  15. }
  protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//根据request里的信息从servletContext里找到相应的子模块ModuleConfig,和它下面的MessageResources,并放到request里,使其他组件可以方便的供request里取得应用配置信息和消息资源.
ModuleUtils.getInstance().selectModule(request, getServletContext());
//取出MoudleConfig实例config
ModuleConfig config = getModuleConfig(request);
//根据config里这个子模块的信息,从servletcontext里,取出这个子模块的RequestProcessor实例
RequestProcessor processor = getProcessorForModule(config);
//如果processor实例为空,就新建一个.同时放到servletcontext里.
if (processor == null) {
processor = getRequestProcessor(config);
}
//调用RequestProcessor的process方法处理,
processor.process(request, response);
}

三. 扩展ActionServlet类
从Struts1.1开始,为减轻ActionServlet的负担,多数功能已经移到RequestProcessor类中,所以基本不用扩展ActionServlet类

如果需要创建自己的ActionServlet,则可以创建一个它的子类.覆盖init()方法(或其他方法),可以写一些自己的操作,但要先调用super.init();
定义如下的类:

Java代码
  1. package sample;
  2. publicclass ExtendedActionServlet extends ActionServlet {
  3. publicvoid init() throws ServletException {
  4. super.init();
  5. //do some operations
  6. ……………
  7. }
  8. }
package sample;
public class ExtendedActionServlet extends ActionServlet {
public void init() throws ServletException {
super.init();
//do some operations
……………
}
} 

扩展完类后,还应该在web.xml文件中如下配置:

Java代码
  1. <servlet>
  2. <servlet-name>sample</servlet-name>
  3. <servlet-class>sample.ExtendedActionServlet</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>sample</servlet-name>
  7. <url-pattern>/action/*<url-pattern>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>sample.ExtendedActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/action/*<url-pattern> 

上面的/action/*表示负责处理所有以/action为前缀的URL,后面的/表示转义

转自:http://ltc603.iteye.com/blog/68637

strut-控制器ActionServlet类详解相关推荐

  1. SpringSecurity权限管理框架系列(六)-Spring Security框架自定义配置类详解(二)之authorizeRequests配置详解

    1.预置演示环境 这个演示环境继续沿用 SpringSecurit权限管理框架系列(五)-Spring Security框架自定义配置类详解(一)之formLogin配置详解的环境. 2.自定义配置类 ...

  2. OpenCV Mat类详解和用法(官网原文)

    参考文章:OpenCV Mat类详解和用法 我马克一下,日后更 官网原文链接:https://docs.opencv.org/3.2.0/d6/d6d/tutorial_mat_the_basic_i ...

  3. 转载:c+string类详解

    C++ string 类详解 </h1><div class="clear"></div><div class="postBod ...

  4. JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】

    黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...

  5. JDBC学习笔记01【JDBC快速入门、JDBC各个类详解、JDBC之CRUD练习】

    黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...

  6. Android复习14【高级编程:推荐网址、抠图片上的某一角下来、Bitmap引起的OOM问题、三个绘图工具类详解、画线条、Canvas API详解(平移、旋转、缩放、倾斜)、矩阵详解】

    目   录 推荐网址 抠图片上的某一角下来 8.2.2 Bitmap引起的OOM问题 8.3.1 三个绘图工具类详解 画线条 8.3.16 Canvas API详解(Part 1) 1.transla ...

  7. Java中的Runtime类详解

    Java中的Runtime类详解 1.类注释 /**Every Java application has a single instance of class Runtime that allows ...

  8. [NewLife.XCode]实体类详解

    NewLife.XCode是一个有10多年历史的开源数据中间件,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和运行日志来进行深入分析,蕴含 ...

  9. basicdatasourcefactory mysql_Java基础-DBCP连接池(BasicDataSource类)详解

    Java基础-DBCP连接池(BasicDataSource类)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 实际开发中"获得连接"或"释放资源 ...

  10. JAVA的StringBuffer类详解

    JAVA的StringBuffer类详解 StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer ...

最新文章

  1. 若依前后端分离如何写移动端接口_前后端分离实践的架构设计
  2. Boost Asio总结(12)class basic_socket_acceptor
  3. 机器翻译pascal程序
  4. springmvc 后台向页面EasyUI的Datagrid传递数据(JSon格式)
  5. python基础_格式化输出(%用法和format用法)
  6. 软件安装(JDK+MySQL+TOMCAT)
  7. python在线解题_20. 有效的括号-----leetcode刷题(python解题)
  8. 好程序员分享居中一个float元素
  9. JavaScript获取本机浏览器UA助力Python爬取糗事百科首页
  10. idea module取得是parent的文件路径_React(或使用TS)中样式混乱解决方案 *.module.less...
  11. IntelliJ IDEA设置代码提示(常用快捷键)
  12. 会声会影x4素材_怎么给视频打马赛克?运用会声会影2019
  13. 【生信分析】clusterProfiler: universal enrichment tool for functional and comparative study(2)
  14. 歌谷服务套件gms_谷歌gms框架安装器下载-安卓9谷歌服务框架app安装-游戏大玩家...
  15. 阄阄乐-IOS抓阄抽签工具
  16. 牛客小白月赛1 F.三视图
  17. el-table表格某列添加icon图标
  18. 使用cdrecord命令刻录光盘
  19. 学习Java的第十周
  20. hadoop全家桶部署手册hadoop-solr-ranger-atlas-hive-hbase...

热门文章

  1. 如何整合JIRA、FishEye、Crucible进行CodeReview-摘自网络
  2. linux默认安装gdk目录,安装GDK
  3. 游戏攻略 一 天堂W(韩)
  4. windows操作系统服务器 网卡速度关联项
  5. 国家开放大学《国际经济法》第二章 国际货物买卖法 边学边练
  6. linu快速删除文件
  7. 【Unity】Unity 几何知识、弧度、三角函数、向量运算、点乘、叉乘
  8. 加法 java_java实现加法
  9. 初学者习字如何选择练字用的辅助格子纸?
  10. python中的eval函数的使用详解