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

從 Servlet 遷移至 Struts 2.1.6:零組態慣例外掛實戰

     編輯:Java 相關知識

本文示範如何將傳統「JSP + Servlet + JavaBean」MVC 專案,逐步重構為 Struts 2.1.6 應用。採用 Convention Plugin 零組態開發,大幅減少 XML 設定,讓 Action 類自動映射至 URL,JSP 置於 WEB-INF/content 保護直接訪問。

原始專案結構

  • 專案名:MvcModel
  • 資料庫:SQL Server 2000
  • 原始架構:Servlet 處理請求 → Manager 業務邏輯 → POJO 實體 → JSP 顯示

Struts 2.1.6 核心 JAR 引入

解壓 struts-2.1.6-all.zip,將下列 JAR 複製至 WEB-INF/lib:

xwork-2.1.2.jar
struts2-core-2.1.6.jar
struts2-convention-plugin-2.1.6.jar
ognl-2.6.11.jar
freemarker-2.3.13.jar
commons-logging-1.0.4.jar
commons-fileupload-1.2.1.jar

web.xml:以 Filter 取代 Servlet

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

單一 Filter 取代多個 Servlet 映射,極度簡潔。

目錄結構調整

web/
├── WEB-INF/
│   ├── content/          <-- 新增,受保護目錄
│   │   ├── index.jsp
│   │   ├── people.jsp
│   │   └── people-input.jsp
│   ├── lib/              <-- Struts JARs
│   └── web.xml
└── src/
    └── cn/he/action/PeopleAction.java

JSP 移入 WEB-INF/content 可防止用戶直接訪問,提升安全性。

首頁 index.jsp:Action 導向連結

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html><head><title>MVC MODEL</title></head><body>
<h1>MVC MODEL</h1>
<ul>
    <li><a href="people!input.action">添加人員</a></li>
    <li><a href="people.action">人員列表</a></li>
</ul>
</body></html>

注意:

  • 連結使用 !methodName.action 語法指定 Action 方法
  • 訪問 https://localhost:8080/MvcModel/index(無 .jsp 後綴)或 index.action
  • Struts 2.1.6 若找不到對應 Action,會自動尋找同名 JSP/HTML

Action 類:PeopleAction(Convention Plugin 零組態)

package cn.he.action;

import java.util.List;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import cn.he.manager.Manager;
import cn.he.pojo.People;
import com.opensymphony.xwork2.ActionSupport;

@Results({
    @Result(name = "reload", location = "people.action", type = "redirect")
})
public class PeopleAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private Manager manager = new Manager();
    private int id;
    private People people;
    private List<People> peoples;

    // 預設執行 list()
    @Override
    public String execute() throws Exception { return list(); }

    // 查詢列表
    public String list() throws Exception {
        System.out.println("list");
        peoples = manager.queryAllPeople();
        System.out.println("name = " + peoples.size());
        return SUCCESS;
    }

    // 進入新增頁面
    public String input() throws Exception {
        System.out.println("input");
        return INPUT;
    }

    // 保存新增
    public String save() throws Exception {
        manager.addPeople(people);
        return "reload";  // 重導向 people.action 避免重複提交
    }

    // 刪除
    public String delete() throws Exception {
        manager.delPeople(id);
        return "reload";
    }

    // Getter/Setter 略
}

關鍵慣例

  • 類名以 Action 結尾,套件名含 action/struts/web 會被自動掃描
  • 方法名對應 !methodName.action(如 input() 對應 people!input.action)
  • 返回 SUCCESS 預設轉向 /WEB-INF/content/people.jsp
  • @Result(name="reload", type="redirect") 實現 POST-Redirect-GET 模式

JSP 頁面:Struts 標籤庫與 OGNL 表達式

people.jsp:列表顯示與刪除連結

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<html><head><title>People列表頁</title></head><body>
<table>
    <tr><td>id</td><td>name</td><td>delete</td></tr>
    <s:iterator value="peoples" var="peo">
        <tr>
            <td><s:property value="people.id"/></td>
            <td>${peo.name}</td>
            <td><a href="people!delete.action?id=${peo.id}">刪除</a></td>
        </tr>
    </s:iterator>
</table>
</body></html>

people-input.jsp:新增表單

<%@ taglib prefix="s" uri="/struts-tags" %>
<html><head><title>People添加頁</title></head><body>
<form action="people!save.action" method="post">
    名字: <input type="text" name="people.name"/> <br/>
    <input type="submit" value="添加"/>
</form>
</body></html>

name="people.name" 透過 OGNL 自動填充 Action 的 people.name 屬性。

執行驗證

  1. 啟動伺服器,訪問 https://localhost:8080/MvcModel/index
  2. 點選「人員列表」→ 顯示 people.jsp 資料
  3. 點選「添加人員」→ 顯示 people-input.jsp 表單
  4. 提交表單 → 執行 save() → 重導向列表頁
  5. 點選「刪除」 → 執行 delete() → 重導向列表頁

遷移總結:Servlet vs Struts 2

面向Servlet 手工 MVCStruts 2.1.6
請求映射web.xml 逐一定義 Servlet單一 Filter + Convention 自動掃描
Action 類需實作特定介面/繼承基類POJO 繼承 ActionSupport,註解配置
參數綁定手動 request.getParameter()OGNL 自動填充屬性
結果導向RequestDispatcher.forward()註解/慣例定義,支援 redirect/chain
JSP 保護需手工放置 WEB-INF慣例預設 WEB-INF/content
程式碼量大量樣板代碼極少,專注業務邏輯

下載資源

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