Struts2 Action/動作

動作是Struts2框架的核心,因為他們的任何MVC(模型 - 視圖 - 控制器)框架。每個URL將被映射到一個特定的動作,它提供了來自用戶的請求提供服務所需的處理邏輯。

但動作也提供其他兩個重要的能力。首先,操作從請求數據的傳輸中起著重要的作用,通過向視圖,無論是一個JSP或其他類型的結果。二,動作必須協助的框架,在確定結果應該渲染視圖,在回應該請求將被退回。

創建動作:

在Struts2的動作,唯一的要求是必須有一個無參數的方法返回String或結果的對象,必須是一個POJO。如果不帶參數的方法是不指定,則默認動作是使用execute()方法。

也可以選擇擴展ActionSupport類實現了6個介面,包括動作介面。動作介面如下:

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示例的操作方法:

package com.zaixian.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;
   }
}

為了說明這一點,操作方法控制視圖,讓我們做出以下更改執行方法和擴展類ActionSupport 如下:

package com.zaixian.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,所以我們可以使用字串常量的成功和錯誤。現在,讓我們修改我們的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.zaixian.struts2.HelloWorldAction"
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
            <result name="error">/AccessDenied.jsp</result>
         </action>
      </package>
</struts>

創建視圖

讓我們創建以下JSP檔 helloWorld.jsp 的WebContent檔夾在eclipse專案。要做到這一點,右鍵單擊WebContent檔夾在專案資源管理器,選擇New >JSP File。該檔將要求返回的結果是SUCCESS,這是一個字串常量“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>

以下是由框架的動作的結果將被調用的檔,該檔是等於字串常量“錯誤”的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。該檔將作為初始動作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應用程式。

執行應用程式

右鍵點擊專案名稱,並單擊 Export > WAR File 創建一個WAR檔。然後在Tomcat 的webapps目錄下部署這個WAR。最後,啟動Tomcat伺服器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp。這會給出以下畫面:

Hello World Input

讓我們為“SECRET”,並輸入一個字,應該看到以下頁面:

Success Result

現在輸入任何單詞而非“SECRET”,應該看到以下頁面: 

Access Denied Result

建立多個動作:

經常會定義一個以上的動作,以處理不同的請求,並提供不同的用戶的URL,因此可以定義不同的類定義如下:

package com.zaixian.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檔中配置這些操作如下:

<?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.zaixian.struts2.HelloWorld"
         method="execute">
         <result name="success">/HelloWorld.jsp</result>
         <result name="error">/AccessDenied.jsp</result>
      </action>
      <action name="something"
         class="com.zaixian.struts2.SomeOtherClass"
         method="execute">
         <result name="success">/Something.jsp</result>
         <result name="error">/AccessDenied.jsp</result>
      </action>
   </package>
</struts>

正如看到在上述假設的例子,動作的結果是重複的SUCCESS和ERROR。要解決這個問題,建議創建一個類包含結果的結果。


上一篇: Struts2 配置檔 下一篇: Struts2快速入門