Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Struts 工作流程全解析:從表單提交到視圖渲染

很多人學 Struts 時只記得幾個配置檔,卻說不清一個請求到底怎麼走完一圈。下面把整個流程拆成七個步驟,逐步對照程式碼來看。

1. 客戶端提交表單

Web 客戶端把資料提交到 Tomcat,表單中要指明 action 為 *.do 或 *.action,method 為 post 或 get。

2. Tomcat 接收並轉交

Tomcat 收到表單後,把資料打包進 HttpServletRequest 與 HttpServletResponse,然後透過 doPost 或 doGet 把 request、response 交給 ActionServlet(Struts 內部封裝好的中央控制器)。要使用它,得先在 web.xml 裡註冊:

<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 的三件事

(1)攔截 URL,查配置。比如 login.jsp 的 action 是 login.do,ActionServlet 依這個 URL 去 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 才能找到對應的 Action。

(2)建立 ActionForm,收集表單資料。

package com.tgb.struts;

import org.apache.struts.action.ActionForm;

/**
 * 登錄 ActionForm,負責表單收集數據
 * 表單的屬性必須和 ActionForm 中的 get、set 屬性一致
 */
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 中登記,Struts 才「看得到」這個 ActionForm:

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

至於表單資料的填充,靠的是 action-mappings 裡的 scope 屬性。

(3)建立 Action 並呼叫 execute。

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:獲得表單數據,調用業務邏輯,返回轉向視圖
 */
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 的資訊已在 struts-config.xml 的 action-mappings 中配置好,其中 forward name 為 success 對應 login_success.jsp,error 對應 login_error.jsp。

4. Action 處理業務

Action 是 Struts 的業務層控制器,它拿到 ActionServlet 交來的 ActionForm,處理其中的資料,再把結果回傳給 ActionServlet——這裡回傳的是 forward name 為 success 或 error 的 ActionForward 物件。

5. ActionServlet 選擇跳轉頁面

ActionServlet 依 Action 回傳的 ActionForward,決定要跳去哪個頁面。

6. 渲染並顯示

最後把跳轉後的頁面渲染出來,顯示在 Web 客戶端。

小提醒:如果修改了 struts-config.xml,必須重啟 Tomcat 才會生效。我在這裡吃過虧,希望大家引以為戒。

專案源碼下載:https://linux.linuxidc.com/(帳號與密碼都是 www.linuxidc.com,目錄為 /2015年資料/2月/27日/詳細剖析Struts工作流程/),下載方法見 https://www.linuxidc.com/Linux/2013-07/87684.htm

延伸閱讀

struts2 檔案上傳(保存為 BLOB 格式) https://www.linuxidc.com/Linux/2014-06/102905.htm

Struts2 的入門實例 https://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2 實作 ModelDriven 介面 https://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的 Struts2 檔案下載亂碼問題 https://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2 整合 Spring 方法及原理 https://www.linuxidc.com/Linux/2013-12/93692.htm

Struts2 註解模式的幾個知識點 https://www.linuxidc.com/Linux/2013-06/85830.htm

Struts 的詳細介紹:請點這裡 Struts 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved