Struts 2 - Action



Action 是 Struts2 框架的核心,就像任何 MVC(模型檢視控制器)框架一樣。每個 URL 都對映到一個特定的 action,它提供處理請求所需的處理邏輯。

但是 action 也在另外兩個重要的方面發揮作用。首先,action 在將資料從請求傳輸到檢視(無論是 JSP 還是其他型別的結果)的過程中起著重要作用。其次,action 必須協助框架確定哪個結果應該呈現將作為對請求的響應返回的檢視。

建立 Action

Struts2 中,action 的唯一要求是必須有一個不帶引數的方法,該方法返回一個 String 或 Result 物件,並且必須是 POJO。如果未指定不帶引數的方法,則預設行為是使用 execute() 方法。

可以選擇擴充套件 ActionSupport 類,該類實現了包括 Action 介面在內的六個介面。Action 介面如下所示:

public interface Action {
   public static final String SUCCESS = "success";
   public static final String NONE = "none";
   public static final String ERROR = "error";
   public static final String INPUT = "input";
   public static final String LOGIN = "login";
   public String execute() throws Exception;
}

讓我們看一下 Hello World 示例中的 action 方法:

package com.tutorialspoint.struts2;

public class HelloWorldAction {
   private String name;

   public String execute() throws Exception {
      return "success";
   }
   
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

為了說明 action 方法控制檢視這一點,讓我們對 execute 方法進行以下更改,並擴充套件類 ActionSupport,如下所示:

package com.tutorialspoint.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
   private String name;

   public String execute() throws Exception {
      if ("SECRET".equals(name)) {
         return SUCCESS;
      } else {
         return ERROR;  
      }
   }
   
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

在這個例子中,我們在 execute 方法中有一些邏輯來檢視 name 屬性。如果該屬性等於字串 "SECRET",則返回 SUCCESS 作為結果,否則返回 ERROR 作為結果。因為我們擴充套件了 ActionSupport,所以我們可以使用字串常量 SUCCESS 和 ERROR。現在,讓我們修改我們的 struts.xml 檔案,如下所示:

<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">
      <action name = "hello" 
         class = "com.tutorialspoint.struts2.HelloWorldAction"
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
         <result name = "error">/AccessDenied.jsp</result>
      </action>
   </package>
</struts>

建立檢視

讓我們在 eclipse 專案的 WebContent 資料夾中建立以下 jsp 檔案 HelloWorld.jsp。為此,右鍵單擊專案資源管理器中的 WebContent 資料夾,然後選擇 新建 > JSP 檔案。如果返回的結果是 SUCCESS(在 Action 介面中定義的字串常量“success”),則會呼叫此檔案:

<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>

<html>
   <head>
      <title>Hello World</title>
   </head>
   
   <body>
      Hello World, <s:property value = "name"/>
   </body>
</html>

以下是框架在 action 結果為 ERROR(等於字串常量“error”)時將呼叫的檔案。以下是 AccessDenied.jsp 的內容:

<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>

<html>  
   <head>
      <title>Access Denied</title>
   </head>
   
   <body>
      You are not authorized to view this page.
   </body>
</html>

我們還需要在 WebContent 資料夾中建立 index.jsp。此檔案將作為初始 action URL,使用者可以在其中單擊以告訴 Struts 2 框架呼叫 HelloWorldAction 類的 execute 方法並呈現 HelloWorld.jsp 檢視。

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
   pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd">

<html>  
   <head>
      <title>Hello World</title>
   </head>
   
   <body>
      <h1>Hello World From Struts2</h1>
      <form action = "hello">
         <label for = "name">Please enter your name</label><br/>
         <input type = "text" name = "name"/>
         <input type = "submit" value = "Say Hello"/>
      </form>
   </body>
</html>

就是這樣,web.xml 檔案不需要更改,因此讓我們使用我們在 示例章節中建立的相同的 web.xml。現在,我們準備使用 Struts 2 框架執行我們的 Hello World 應用程式。

執行應用程式

右鍵單擊專案名稱,然後單擊 匯出 > WAR 檔案以建立 War 檔案。然後將此 WAR 部署到 Tomcat 的 webapps 目錄中。最後,啟動 Tomcat 伺服器並嘗試訪問 URL https://:8080/HelloWorldStruts2/index.jsp。這將為您提供以下螢幕:

Hello World Struts4

讓我們輸入一個單詞“SECRET”,您應該會看到以下頁面:

helloworldstruts51

現在輸入除“SECRET”之外的任何單詞,您應該會看到以下頁面:

helloworldstruts6

建立多個 Action

您將經常定義多個 action 來處理不同的請求併為使用者提供不同的 URL,因此您將定義如下所示的不同類:

package com.tutorialspoint.struts2;
import com.opensymphony.xwork2.ActionSupport;

class MyAction extends ActionSupport {
   public static String GOOD = SUCCESS;
   public static String BAD = ERROR;
}

public class HelloWorld extends ActionSupport {
   ...
   public String execute() {
      if ("SECRET".equals(name)) return MyAction.GOOD;
      return MyAction.BAD;
   }
   ...
}

public class SomeOtherClass extends ActionSupport {
   ...
   public String execute() {
      return MyAction.GOOD;
   }
   ...
}

您將在 struts.xml 檔案中配置這些 action,如下所示:

<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <constant name = "struts.devMode" value = "true" />
   
   <package name = "helloworld" extends = "struts-default">
      <action name = "hello" 
         class = "com.tutorialspoint.struts2.HelloWorld" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
         <result name = "error">/AccessDenied.jsp</result>
      </action>
      
      <action name = "something" 
         class = "com.tutorialspoint.struts2.SomeOtherClass" 
         method = "execute">
         <result name = "success">/Something.jsp</result>
         <result name = "error">/AccessDenied.jsp</result>
      </action>
   </package>
</struts>

如您在上述假設示例中看到的,action 結果 SUCCESSERROR 是重複的。

為了解決此問題,建議您建立一個包含結果輸出的類。

廣告