且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具体分析Struts工作流程

更新时间:2022-08-22 17:01:39

具体分析Struts工作流程

         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流程。

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






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4638968.html,如需转载请自行联系原作者