watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXV3ZW56aGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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

2.Tomcat接收Webclient提交的表单。将表单数据打包到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)它负责截取Webclient提交的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.将跳转页面渲染,显示在Webclient。

温馨提示:假设改动了strus-config.xml文件,重新启动Tomcatserver后改动才干生效。我在这吃亏了。希望大家能引以为戒。

   最后把项目的源代码下载地址奉上,http://pan.baidu.com/s/1hqvdfyG,我们希望能帮助你理解Struts流程。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

具体分析Struts工作流程的更多相关文章

  1. J2EE进阶(十八)基于留言板分析SSH工作流程

    J2EE进阶(十八)基于留言板分析SSH工作流程   留言板采用SSH(Struts1.2 + Spring3.0 + Hibernate3.0)架构.   工作流程(以用户登录为例):   首先是用 ...

  2. nodejs的Express框架源码分析、工作流程分析

    nodejs的Express框架源码分析.工作流程分析 1.Express的编写流程 2.Express关键api的使用及其作用分析 app.use(middleware); connect pack ...

  3. openVswitch(OVS)源代码分析之工作流程(数据包处理)

    上篇分析到数据包的收发,这篇开始着手分析数据包的处理问题.在openVswitch中数据包的处理是其核心技术,该技术分为三部分来实现:第一.根据skb数据包提取相关信息封装成key值:第二.根据提取到 ...

  4. openVswitch(OVS)源码分析之工作流程(哈希桶结构体的解释)

    这篇blog是专门解决前篇openVswitch(OVS)源码分析之工作流程(哈希桶结构体的疑惑)中提到的哈希桶结构flex_array结构体成员变量含义的问题. 引用下前篇blog中分析讨论得到的f ...

  5. openVswitch(OVS)源代码分析之工作流程(flow流表查询)

    原文链接: openVswitch(OVS)源代码分析之工作流程(flow流表查询)

  6. Struts工作流程

    Java Web 都是使用线程来处理用户的请求(request)的,一次请求对应一个处理线程.Struts 2会为每个处理线程分配一个Action对象, 将提交的参数注射到Action属性中,并调用A ...

  7. Struts框架核心工作流程与原理

    1.Struts2架构图  这是Struts2官方站点提供的Struts 2 的整体结构.  执行流程图 2.Struts2部分类介绍  这部分从Struts2参考文档中翻译就可以了. ActionM ...

  8. Struts2的工作流程分析

    Struts2的工作流程分析 Posted on 2011-02-22 09:32 概述 本章讲述Struts2的工作原理. 读者如果曾经学习过Struts1.x或者有过Struts1.x的开发经验, ...

  9. tornado 学习笔记10 Web应用中模板(Template)的工作流程分析

             第8,9节中,我们分析Tornado模板系统的语法.使用以及源代码中涉及到的相关类,而且对相关的源代码进行了分析.那么,在一个真正的Web应用程序中,模板到底是怎样使用?怎样被渲染? ...

随机推荐

  1. iOS执行时与method swizzling

    C语言是静态语言,它的工作方式是通过函数调用,这样在编译时我们就已经确定程序怎样执行的.而Objective-C是动态语言,它并不是通过调用类的方法来执行功能,而是给对象发送消息,对象在接收到消息之后 ...

  2. 深度分析 Java 的 ClassLoader 机制(源码级别)(转)

    写在前面:Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中,JVM在加载类的时候,都是通过ClassLoa ...

  3. [文学阅读] METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments

    METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments Satanje ...

  4. 在WPF中使用PlaneProjection模拟动态3D效果

    原文:在WPF中使用PlaneProjection模拟动态3D效果 虽然在WPF中也集成了3D呈现的功能,在简单的3D应用中,有时候并不需要真实光影的3D场景.毕竟使用3D引擎会消耗很多资源,有时候使 ...

  5. Android RxJava使用介绍(三) RxJava的操作符

    上一篇文章已经具体解说了RxJava的创建型操作符.本片文章将继续解说RxJava操作符.包括: Transforming Observables(Observable的转换操作符) Filterin ...

  6. cocos2d-x-3.1在eclipse中的环境搭建

    cocos2d-x-3.0出来后,到如今3.1. 自己在eclipse配置上走了不少弯路,记下来给大家方便,给自己方便. 前提条件: * Android NDK * Android SDK **OR* ...

  7. android 中国通信乱码问题

    1.要解决中文乱码问题.首先得了解什么是字符编码 计算机要处理各种字符,就须要将字符和二进制内码相应起来,这样的相应关系就是字符编码. 要制定字符编码首先要确定字符集,并将 字符集内的字符排序.然后和 ...

  8. NOI 评价体系 arbiter 安装方法 常见的问题 移植

    #!/bin/bash AppPath="$PWD"   读取当前文件夹 echo "Arbiter is installing..." sudo apt-ge ...

  9. Linux 常用命令解析和Bash Shell使用示例脚本演示

     摘要 Linux命令是基于文本格式输入输出的一种程序,依照Unix哲学中强调的程序功能简单,输入宽松,输出严谨,各种程序组合能够具有更强大的功能,而具有这样的灵活性的主要原因是Linux规定程序 ...

  10. php 两个文件之间的相对路径的计算方法

    php 两个文件之间的相对路径的计算方法 比如: 文件A 的路径是 /home/web/lib/img/cache.php 文件B的路径是 /home/web/api/img/show.php 那么. ...