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

Struts 2 入門:從零構建第一個 Struts 2 應用程序完整指南

     編輯:Java 相關知識

Struts 2 是基於 WebWork 重構的全新 MVC 框架,與 Struts 1.x 在配置、開發模式上徹底不同。本教程將引導你從環境搭建、核心組件編寫、配置文件編寫、JSP 頁面開發,完整構建一個「輸入兩整數求代數和,非負顯示 positive.jsp,負數顯示 negative.jsp」的入門應用。

開發環境與依賴

  • IDE:MyEclipse 6(不內建 Struts 2 支援)
  • Server:Tomcat 6
  • Struts:2.0.11.1
  • JDK:1.5.0_12
  • 必備 5 個 JAR(放入 WEB-INF/lib):
    struts2-core-2.0.11.1.jar、xwork-2.0.4.jar、commons-logging-1.0.4.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar

五步構建流程(對比 Struts 1.x)

步驟Struts 1.xStruts 2
1. 安裝框架web.xml 配置 ActionServletweb.xml 配置 FilterDispatcher 過濾器
2. 編寫 Action繼承 org.apache.struts.action.Action繼承 com.opensymphony.xwork2.ActionSupport
3. 編寫 Form單獨 ActionForm 類Action 與 Form 合一(屬性直接寫在 Action)
4. 配置映射struts-config.xml (WEB-INF)struts.xml (WEB-INF/classes)
5. JSP 頁面Struts 標籤庫多個統一 /struts-tags 標籤庫

詳細實作步驟

第 1 步:web.xml 配置核心過濾器

<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>

Struts 2 入口是 Servlet Filter,攔截所有請求。

第 2 步:編寫 Action 類 (FirstAction.java)

package action;

import com.opensymphony.xwork2.ActionSupport;

public class FirstAction extends ActionSupport {
    private int operand1;
    private int operand2;

    @Override
    public String execute() throws Exception {
        if (getSum() >= 0) {
            return "positive";   // 對應 struts.xml result name
        } else {
            return "negative";
        }
    }

    public int getSum() { return operand1 + operand2; }
    // getter/setter 省略
}

關鍵差異:Struts 2 execute() 無參數、返回 String 結果標識(非 ActionForward)。

第 3 步:ActionForm 已合併至 Action

屬性 operand1、operand2 及其 getter/setter 即為表單數據綁定,無需單獨 Form 類。

第 4 步:struts.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "https://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="struts2" namespace="/mystruts" extends="struts-default">
        <action name="sum" class="action.FirstAction">
            <result name="positive">/positive.jsp</result>
            <result name="negative">/negative.jsp</result>
        </action>
    </package>
</struts>
  • namespace="/mystruts":URL 前綴(不含 action 名)
  • extends="struts-default":繼承預設攔截器、結果類型等配置
  • 訪問 URL:https://localhost:8080/struts2/mystruts/sum.action

第 5 步:JSP 頁面開發

主頁面 (sum.jsp)

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head><title>輸入操作數</title></head>
<body>
    求代數和 <br/>
    <s:form action="mystruts/sum.action">
        <s:textfield name="operand1" label="操作數1"/>
        <s:textfield name="operand2" label="操作數2"/>
        <s:submit value="代數和"/>
    </s:form>
</body>
</html>

提示:<s:form> 內建使用 <table> 佈局,建議全部使用 Struts 標籤,避免混用 HTML 導致佈局錯亂。

結果頁 (positive.jsp / negative.jsp)

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html><head><title>顯示代數和</title></head>
<body>
    代數和為非負整數<h1><s:property value="sum" /></h1>
</body></html>

<s:property value="sum" /> 從 ValueStack 取得 getSum() 返回值。底層原理:

OgnlValueStack ovs = (OgnlValueStack) request.getAttribute("struts.valueStack");
out.println(ovs.findString("sum"));

啟動測試

  1. 啟動 Tomcat
  2. 訪問 https://localhost:8080/struts2/sum.jsp
  3. 輸入兩整數提交,觀察跳轉頁面與結果顯示

核心概念總結

  • FilterDispatcher:框架入口,替代 Struts 1.x 的 ActionServlet
  • ActionSupport:基礎 Action 類,提供驗證、國際化等功能
  • 屬性驅動:表單字段自動映射至 Action 同名屬性(Setter 注入)
  • 結果導航:execute() 返回字串匹配 <result name="">
  • 統一標籤庫:/struts-tags 整合所有 UI 標籤
  • ValueStack/OGNL:數據存儲與表達式語言核心

掌握這個最小化完整流程,你已踏入 Struts 2 開發大門。後續教程將深入攔截器、驗證、類型轉換、標籤庫等進階主題。

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