首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Struts 開發指南:安裝配置與核心運行流程

     編輯:Java 程式設計入門

環境要求與核心 Servlet

Struts 可運行於任何支援 JSP 1.2 / Servlet 2.3 的 Web 容器。所有請求統一提交至核心控制器 org.apache.struts.action.ActionServlet。

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>
  <load-on-startup>2</load-on-startup>
</servlet>

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

兩種常用 URL 模式:

  • 擴展映射:https://host/context/actionName.do
  • 路徑映射:https://host/context/do/actionName

Struts 啟動與 struts-config.xml 結構

容器啟動時調用 ActionServlet.init(),讀取 struts-config.xml 初始化配置。標準結構:

<struts-config>
  <data-sources />        <!-- 數據源 -->
  <form-beans />         <!-- ActionForm 定義 -->
  <global-exceptions />  <!-- 全局異常處理 -->
  <global-forwards />    <!-- 全局轉向 -->
  <action-mappings />    <!-- Action 映射 -->
  <controller />         <!-- 控制器參數 -->
  <message-resources />  <!-- 國際化資源 -->
</struts-config>

核心組件為 Action 與 Form。

請求處理流程

  1. 請求提交給 ActionServlet
  2. 根據 action-mappings 尋找對應 Form 與 Action
  3. 將 request 參數自動映射填充至 Form Bean
  4. 將填充後的 Form 傳遞給 Action
  5. Action 獲得 mapping、form、request、response,調用 execute() 處理業務
  6. Action 返回 ActionForward(邏輯視圖名)
  7. ActionServlet 根據轉向配置導向對應 JSP,響應客戶端

Struts 處理流程圖

最簡單實戰範例

input.jsp

<form method="post" action="/example.do">
  請輸入值: <input type="text" name="test"/>
  <input type="submit" name="Submit" value="提交">
  <input type="reset" name="reset" value="重置">
</form>

struts-config.xml

<struts-config>
  <form-beans>
    <form-bean name="exampleActionForm"
        type="com.zotn.struts.example1.ExampleActionForm" />
  </form-beans>
  <action-mappings>
    <action name="exampleActionForm" path="/example"
        type="com.zotn.struts.example1.ExampleAction">
      <forward name="forward" path="/forward.jsp" />
    </action>
  </action-mappings>
</struts-config>

Action 類

public class ExampleAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        ExampleActionForm myForm = (ExampleActionForm) form;
        String input = myForm.getTest();
        if (input == null || input.trim().isEmpty()) {
            return mapping.findForward("empty"); // 需在配置中定義
        }
        // 業務處理...
        return mapping.findForward("forward");
    }
}

ActionForm 類

public class ExampleActionForm extends ActionForm {
    private String test;
    public String getTest() { return test; }
    public void setTest(String test) { this.test = test; }
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        test = null; // 重置表單
    }
}

關鍵點總結

  • ActionServlet 為單例,多線程共享 → Action 類必須線程安全(僅用局部變量)
  • struts-config.xml 為核心配置,所有映射關係集中定義
  • ActionForm 負責數據承載與簡單驗證,業務邏輯置於 Action 或業務 Bean
  • 邏輯轉向名(forward)解耦 Action 與具體 JSP 路徑

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved