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

Struts 入門實戰:從零構建 MVC Web 應用(登錄/導航示例)

     編輯:Java 相關知識

環境準備

  • Web 容器:Tomcat 4
  • 框架:Struts 1.1 (struts-example.war 解壓重命名為 test)

核心配置文件

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

<servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.cool</url-pattern>  <!-- 自定義後綴 -->
</servlet-mapping>

<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

ActionServlet 是 Struts 核心控制器,啟動時加載 struts-config.xml,攔截 *.cool 請求。

struts-config.xml — 應用路由中樞

<form-beans>
  <form-bean name="userForm" type="test.UserForm"/>
</form-beans>

<global-forwards>
  <forward name="failed" path="/failed.cool"/>
  <forward name="regist" path="/regist.jsp"/>
</global-forwards>

<action-mappings>
  <action path="/regist" type="test.RegistAction" name="userForm" scope="request" input="/index.jsp"/>
  <action path="/overview" forward="/hello.jsp"/>
  <action path="/failed" forward="/wuwu.jsp"/>
</action-mappings>
  • FormBean:自動綁定表單數據,避免手工 request.getParameter()
  • Global Forwards:邏輯視圖名映射到物理路徑
  • Action Mappings:請求路徑 → Action 類 + FormBean + 輸入頁面

Java 組件開發

FormBean: test.UserForm

public class UserForm extends ActionForm {
    private String name = "lpw";
    private String ps = "1111";
    // getter/setter 省略
}

屬性名與表單 name 屬性一一對應,Struts 自動填充。

Action: test.RegistAction

public class RegistAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;
        if ("lpw".equals(userForm.getName())) {
            return mapping.findForward("failed");  // 邏輯名 → global-forwards
        }
        return mapping.findForward("regist");
    }
}

業務邏輯極簡:驗證用戶名非默認值,決定轉向。

視圖層 JSP 頁面

index.jsp — 首頁/登錄表單

<form action="regist.cool" method="post">
  用戶: <input type="text" name="name"><br>
  密碼: <input type="password" name="ps"><br>
  <input type="submit" value="新增用戶">
</form>
<a href="overview.cool">站點導航</a>

表單提交到 regist.cool,觸發 /regist Action Mapping。

hello.jsp — 導航頁

<h1>site map</h1>The following is content filling by reader

wuwu.jsp — 失敗頁 (顯示現有用戶)

<jsp:useBean id="beanlpw" class="test.UserForm" scope="session"/>
現有用戶:<%=beanlpw.getName()%><br>
密碼:<%=beanlpw.getPs()%><br>
沒有得到新的用戶!

regist.jsp — 成功頁

<jsp:useBean id="beanlpw" class="test.UserForm" scope="session"/>
新用戶帳號:<%=beanlpw.getName()%><br>
密碼:<%=beanlpw.getPs()%>

運行驗證

  1. 啟動 Tomcat
  2. 訪問 https://localhost:8080/test/index.jsp
  3. 輸入非 "lpw" 用戶名 → 進入 regist.jsp
  4. 輸入 "lpw" → 進入 wuwu.jsp
  5. 點擊「站點導航」 → 進入 hello.jsp

MVC 協作流程總結

  1. 用戶提交表單 → regist.cool
  2. ActionServlet 攔截 → 查找 /regist mapping
  3. 實例化 UserForm,自動填充請求參數
  4. 調用 RegistAction.execute(),執行業務邏輯
  5. 返回 ActionForward (邏輯名 "failed"/"regist")
  6. ActionServlet 解析 global-forwards → 轉發到對應 JSP
  7. JSP 通過 jsp:useBean 讀取 session 中的 FormBean 顯示數據

進階學習方向

  • 驗證框架 (validate()、ActionErrors)
  • 模塊化配置 (多 struts-config.xml)
  • 標簽庫 (html:form, bean:write, logic:iterate)
  • 與 EJB/Spring/Hibernate 整合實現完整 Model 層
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved