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運行成功
測試成功
