Struts1运行原理以及整合步骤

Struts1

 struts1运行步骤

1、项目初始化:项目启动时加载web.xml,struts1的总控制器ActionServlet是一个Servlet,它在web.xml中是配置成自动启动的Servlet,在启动时总控制器   会读取配置文件(struts-config.xml)的配置信息,为struts中不同的模块初始化相应的对象。

2、发送请求:用户发送请求,请求都被ActionServlet中央控制器(在web.xml里面配置好的)接收到,会读取配置文件(srtuts- config)找到请求对应的Action对  象。

3、请求参数:struts的总控制器ActionServlet在用户提交请求时将数据放到对应的ActionForm对象中,actionForm根据配置文件里配置的name=“”来自动接收  表单数据。

4、分发请求:控制器根据配置信息对象ActionConfig将请求派发到具体的Action,对应的FormBean一并传给这个Action中的excute()方法。

5、处理业务:Action一般只包含一个excute()方法,它负责执行相应的业务逻辑(调用其它的业务模块)完毕后返回一个ActionForward对象。服务器通过      ActionForward对象进行转发工作。

6、返回响应:Action将业务处理的不同结果返回一个目标响应对象给总控制器。

7、查找响应:总控制器根据Action处理业务返回的目标响应对象,找到对应的资源对象,一般情况下为jsp页面。

8、响应用户:目标响应对象将结果传递给资源对象,将结果展现给用户。

总结:客户端发送请求.do,分发给相应的action进行处理。进行处理的时候需要传几个参 数:request,response,mapping(把配置拿出来封装成一个对象取出来), 还有一个actionForm(根据配置文件里配置的name=“”来自动接收表单数据,最终调用业逻辑,拿到一些数据返回来,返回 ActionServlet的是一个actionForward的跳转信息,通过mapping.findForward找到然后servlet里面它会 帮你自动的挑战到相应的页面

整合步骤:(导入相应jar包)

第一步:配置web.xml

    <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
   </servlet>

 <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
 </servlet-mapping>

 第二步:配置struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	<action-mappings>
          <!-- path 客户请求的路径welcome.do type请求对应的Action对象 --> <action path="/welcome" type="com.lwl.Action.WelcomeAction" >
              <forward name="success" path="/WEB-INF/page/welcome.jsp"></forward>
          </action>
           <init-param>
              <param-name>config</param-name>
              <param-value>/WEB-INF/struts-config.xml</param-value>
            </init-param>
</action-mappings> </struts-config>

第三步:编写Action对象

public class WelcomeAction extends ActionServlet {
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) return mapping.findForward("success");
}

 第四步:编写welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>PersonList</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
      你好 struts1运行成功
  </body>
</html>

 第五步:测试

  访问地址:localhost:8080/项目名/welcome.do

  跳转至welcome.jsp输出:

  你好 struts1运行成功

  测试成功

原文地址:https://www.cnblogs.com/lwl-cnblogs/p/5640714.html