详细剖析Struts工作流程


详细剖析Struts工作流程

 详细剖析Struts工作流程

1.Web客户端提交数据到Tomcat,在form表单中需说明表单提交的action是*.do或*.action,mothod是post或get;

2.Tomcat接收Web客户端提交的表单,将表单数据打包到HttpServletRequest和HttpServletResponse对象中,然后通过doPost或doGet方式把request、response提交到ActionServlet(ActionServlet是Struts内部封装好的);

要使用Struts封装的ActionServlet,需要在web.xml中配置ActionServlet,配置信息如下: 

 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>2</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>2</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>


 <!-- Standard Action Servlet Mapping -->
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

3.ActionServlet是struts的中央控制器,它任务如下:

(1)它负责截取Web客户端提交的URL。比如login.jsp的action是login.do,然后根据URL从struts-config.xml中获得配置信息。配置信息需要手动在struts-config.xml中配置,配置信息如下:

 <action-mappings>
  <action path="/login" type="com.tgb.struts.LoginAction" name="loginForm"
   scope="request">
   <forward name="success" path="/login_success.jsp"></forward>
   <forward name="error" path="/login_error.jsp"></forward>
  </action>
 </action-mappings>

login.jsp中action的url名必须和配置信息中的path名一致,这样ActionServlet才能根据URL找到对应的Action,完成请求响应。

(2)创建ActionForm类的对象,用于收集表单数据,ActionForm类代码如下:

package com.tgb.struts;

import org.apache.struts.action.ActionForm;

/**
 * 登录ActionForm,负责表单收集数据
 * 表单的属性必须和ActionForm中的get、set属性一致
 * @author quwenzhe
 *
 */

public class LoginActionForm extends ActionForm {
 private String username;
 private String password;

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}

创建后需要在struts-config.xml中配置ActionForm信息,这样struts才能检测到有ActionForm的存在,配置信息如下:

 <form-beans>
  <form-bean name="loginForm" type="com.tgb.struts.LoginActionForm" />
 </form-beans>

通过action-mappings中的scope属性,把表单数据赋值给ActionForm。

(3)创建Action,Action类代码如下:

package com.tgb.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * 登录Action 负责获得表单数据,调用业务逻辑,返回转向视图
 *
 * @author quwenzhe
 *
 */
public class LoginAction extends Action {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  LoginActionForm laf = (LoginActionForm) form;
  String username = laf.getUsername();
  String password = laf.getPassword();

  if ("admin".equals(username) && "admin".equals(password)) {
   // 登录成功
   return mapping.findForward("success");
  } else {
   // 登录失败
   return mapping.findForward("error");
  }
 }

}

action的信息已经在strut-config.xml中的action-mappings中配置。并且在配置信息中我们已经说明,forward name为"success"的对应login_success.jsp页面,forward name为"error"的对应login_error.jsp页面。

(4)调用action的execute方法,将ActionForm中的信息提交到业务层控制器Action中处理

4.Action是struts的业务层控制器,它获得ActionServlet提交的ActionForm,对ActionForm中的信息进行处理,将处理结果返回到ActionServlet。这里返回的是forward name为"success"或"error"的ActionFoward对象。

5.ActionServlet根据Action返回的ActionFoward,选择需要跳转的页面。

6.将跳转页面渲染,显示在Web客户端。

温馨提示:如果修改了strus-config.xml文件,重启Tomcat服务器后修改才能生效。我在这吃亏了,希望大家能引以为戒。

最后把项目的源码下载地址奉上,希望能帮助大家理解Struts的工作流程。

------------------------------------------分割线------------------------------------------

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2015年资料/2月/27日/详细剖析Struts工作流程/

下载方法见

------------------------------------------分割线------------------------------------------

struts2文件上传(保存为BLOB格式)

Struts2的入门实例

Struts2实现ModelDriven接口

遇到的Struts2文件下载乱码问题

Struts2整合Spring方法及原理

Struts2 注解模式的几个知识点

Struts 的详细介绍:请点这里
Struts 的下载地址:请点这里

本文永久更新链接地址:

相关内容