Struts的工作原理

2008.11.24 / 标签: ,,, / 分类: J2EE技术
Sofa

昨天写了那个[struts工作流程分析] 感觉有点乱,今天整理了一下,当然不是我自己研究出来的,我喜欢看完技术资料后用自己所理解的意思去表述一遍,当然我有时也会原文转来。今天又增加了一个栏目《系统漏洞》,这里的漏洞就更不可能是我发现的了,都是直接转来的。博客是不是必须纯原创我才不去管它,我只关注和学习我所感兴趣的。

struts的工作原理可分为如下8步。

1.读取配置(初始化ModuleConfig对象)
Struts框架总控制器(ActionServlet)是一个Servlet,在web.xml中被配置成一个自动启动的Servlet。读取配置文件struts-config.xml的配置信息,为不同的Struts模块初始化相应的ModuleConfig对象。

2.用户请求
用户提交表单或调用URL向WEB应用程序服务器提交一个请求,请求的数据用HTTP协议上传给WEB服务器。

3.填充FormBean
(*.do请求)从ActionConfig中找出对应该请求的Action子类,如有对应的Action且这个Action又一个相应的ActionForm,ActionForm被实例化并用HTTP请求的数据填充其属性,并保存在ServletContext中,这样他们就可以被其它Action对象或JSP调用。如果没有对应的Action,控制器则直接转发给JSP或静态页面。

4.派发请求
控制器根据配置信息ActionConfig将请求派发到具体的Action,相应的FormBean一并传给这个Action的execute()方法。

5.处理业务
Action一般只包含一个execute方法,它负责执行相应的业务逻辑。执行完毕后返回一个ActionFoward对象,控制器通过该ActionFoward对象来进行转发工作。

6.返回响应
Action根据业务处理的不同结果返回一个响应对象给总控制器,该目标响应对相对应一个具体的JSP页面或另一个Action。

7.查找响应
总控制器根据业务功能Action返回的目标响应对象找到对应的资源对象,通常是一个具体的JSP页面。

8.响应用户
JSP将结果展现给用户。

struts工作流程分析

2008.11.23 / 标签: ,,, / 分类: J2EE技术

技术关键在于学习其原理,而不是浅浅淡淡的会用,免得别人问起的时候什么也不知道,尤其是在面试时!
首先页面上的数据请求被发送到服务器,此时的请求先进入到前端控制器(ActionServlet)。ActionServlet再将请求转发给后端控制器(Action),在转发中顺便送了一份礼物form(ActionForm),在Action中对form中的数据做实际的处理操作并返回一份礼物forward(ActionForward)给ActionServlet,ActionServlet收到这份礼物后根据其中的关键字从struts配置文件Struts-config.xml中获取具体的跳转页。
那么ActionServlet是怎么知道该找哪个后端控制器呢?又是怎么知道返回的ActionForward具体是什么呢?为了回答这个问题,我们参考一个用Struts做的登陆程序。

登陆表单代码:

 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4.   <head>
  5.     <title>struts工作流程分析</title>
  6.   </head> 
  7.   <body>
  8.     <form action="login.do" method="POST">
  9.         用户名:<input type="text" name="userName"><br/>
  10.         密码:<input type="password" name="userPwd"><br/>
  11.         <input type="submit" value="登录">
  12.     </form>
  13.   </body>
  14. </html>

 

LoginAction代码:

 
  1. package cn.ineeke.action;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5.  
  6. import org.apache.struts.action.Action;
  7. import org.apache.struts.action.ActionForm;
  8. import org.apache.struts.action.ActionForward;
  9. import org.apache.struts.action.ActionMapping;
  10.  
  11. import cn.ineeke.form.LoginForm;
  12. /**
  13.  *作者:Neeke
  14.  *BLOG:http://www.ineeke.com
  15.  */
  16. public class LoginAction extends Action {
  17.  
  18.     @Override
  19.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  20.             HttpServletRequest request, HttpServletResponse response)
  21.             throws Exception {
  22.             LoginForm loginForm = (LoginForm)form;
  23.             String forwardName = "failure";
  24.             if(loginForm.getUserName().equals("neeke") && loginForm.getUserPwd().equals("123456")){
  25.                 forwardName = "success";
  26.             }
  27.             return mapping.findForward(forwardName);
  28.     }
  29.  
  30. }

LoginForm代码:

 
  1. package cn.ineeke.form;
  2.  
  3. import org.apache.struts.action.ActionForm;
  4.  
  5. public class LoginForm extends ActionForm {
  6.     private String userName = null;
  7.     private String userPwd = null;
  8.     public String getUserName() {
  9.         return userName;
  10.     }
  11.     public void setUserName(String userName) {
  12.         this.userName = userName;
  13.     }
  14.     public String getUserPwd() {
  15.         return userPwd;
  16.     }
  17.     public void setUserPwd(String userPwd) {
  18.         this.userPwd = userPwd;
  19.     }
  20. }

 

Struts-config.xml文件:

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
  3.  
  4. <struts-config>
  5.   <form-beans>
  6.     <form-bean name="loginForm" type="cn.ineeke.form.LoginForm"></form-bean>
  7.   </form-beans>
  8.   <action-mappings>
  9.     <action path="/login" type="cn.ineeke.action.LoginAction" name="loginForm">
  10.         <forward name="success" path="/success.jsp"></forward>
  11.         <forward name="failure" path="/failure.jsp"></forward>
  12.     </action>
  13.   </action-mappings>
  14.   <message-resources parameter="cn.ineeke.struts.ApplicationResources" />
  15. </struts-config>
  16.  

当我们的表单提交给login.do的时候,ActionServlet就会去读取Struts-config.xml文件并根据<action path="/login" type="cn.ineeke.action.LoginAction" name="loginForm">中的type属性找到具体的后端控制器LoginAction,与此同时又根据name属性找到<form-bean name="loginForm" type="cn.ineeke.form.LoginForm"></form-bean>并将表单中的数据存储在LoginForm的实例中(这个实例就是前端控制器送给后端控制器的礼物了),在LoginAction的execute方法中进行数据处理并返回一个ActionForward对象(回赠一份礼物)给ActionServlet。最后ActionServlet再根据这份礼物的关键字在
<forward name="success" path="/success.jsp"></forward>
<forward name="failure" path="/failure.jsp"></forward>
中进行查找,并跳转到相应的path。

无觅相关文章插件,快速提升流量