Java的Struts架構中Action的編寫與攔截器的使用方法_java

來源:互聯網
上載者:User

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.yiibai.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.yiibai.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.yiibai.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。這會給出以下畫面:

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

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

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

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

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

Struts2 攔截器
攔截器的概念是Servlet過濾器或JDK代理類一樣的。攔截器允許橫切功能分開實現的動作,以及架構。使用攔截器,可以實現如下:

  • 提供預先處理行動之前被稱為邏輯。
  • 提供後處理邏輯動作後被調用
  • 捕獲異常,這樣可以進行替代處理。

Struts2架構提供的許多功能都使用攔截實現的例子包括異常處理,檔案上傳,生命週期回調和驗證等事實上作為Struts2的基礎,其功能攔截,這可能有7或8攔截器分配給每個動作。

Struts2架構的攔截器:
Struts 2架構提供了良好的箱攔截列表來預先設定的,並準備使用。下面列出了幾個重要的攔截:

請看Struts 2文檔的完整細節上面提到的攔截。會告訴如何使用Struts應用程式在一個攔截器。

如何使用攔截器?
讓我們來看看如何使用已有的攔截,我們的“Hello World”程式。我們將使用計時器來測量過了多長時間執行操作方法,其目的是攔截。同時使用params攔截器,其目的是發送請求參數的動作。您可以嘗試不使用這個攔截您的樣本中會發現,沒有被設定name屬性,因為參數是無法達到動作。

我們將繼續HelloWorldAction.java,web.xml 的helloWorld.jsp 和 index.jsp 檔案,因為他們已經建立了範例章節,但讓我們如下修改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.yiibai.struts2.HelloWorldAction"   method="execute">   <interceptor-ref name="params"/>   <interceptor-ref name="timer" />   <result name="success">/HelloWorld.jsp</result>  </action> </package></struts>

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

現在,在給定的文字框中輸入單詞,並單擊“Say Hello按鈕執行已定義的動作。現在,如果將檢查產生的日誌,會發現下面的文字:

 

INFO: Server startup in 3539 ms27/08/2011 8:40:53 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger infoINFO: Executed action [//hello!execute] took 109 ms.

這裡底行,正在產生因為這告訴動作發生要執行的總共為 109ms定時器的攔截器。

建立自訂的攔截器
在應用程式中使用自訂的攔截器是一種優雅的方式提供橫切的應用功能。建立一個自訂攔截器是很容易的,需要擴充的介面,下面的Interceptor介面:

 

public interface Interceptor extends Serializable{ void destroy(); void init(); String intercept(ActionInvocation invocation) throws Exception;}

正如其名稱所表明的,init()方法提供了一種方法來初始化攔截器,並destroy() 方法提供了一種工具攔截清理。不同的行動,攔截被重用跨請求和需要是安全執行緒的,尤其是intercept() 方法。

ActionInvocation對象可以訪問運行時環境。它允許訪問的動作本身和方法調用的動作,並確定動作是否已被調用。

如果不需要初始化或清除代碼,可以擴充AbstractInterceptor類。這提供了一個預設的無操作實現的init()和 destroy()方法。

建立攔截器類:
讓我們建立Java資源 MyInterceptor.java> src 檔案夾:

 

package com.yiibai.struts2;import java.util.*;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class MyInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation)throws Exception{  /* let us do some pre-processing */  String output = "Pre-Processing";   System.out.println(output);  /* let us call action or next interceptor */  String result = invocation.invoke();  /* let us do some post-processing */  output = "Post-Processing";   System.out.println(output);  return result; }}

就像看到的,實際行動將使用攔截器執行invocation.invoke()調用。所以,可以做一些前處理和一些處理後,根據需要。

該架構本身啟動的過程中,在第一次調用ActionInvocation對象的invoke()。每次 invoke()被調用,ActionInvocation的諮詢的狀態和執行為準攔截接下來。通過請求流以下資料圖顯示了相同的概念:

建立動作類:
讓我們建立一個Java檔案HelloWorldAction.java的Java下Java Resources > src下面給出的內容包名為 com.yiibai.struts2。

package com.yiibai.struts2;import com.opensymphony.xwork2.ActionSupport;public class HelloWorldAction extends ActionSupport{ private String name; public String execute() throws Exception {  System.out.println("Inside action....");  return "success"; }  public String getName() {  return name; } public void setName(String name) {  this.name = name; }}

在前面的例子中,我們已經看到,這是一個相同的類。我們有標準的“名稱”屬性的getter和setter方法​​,並返回字串“success”的執行方法。

建立視圖
讓我們建立以下JSP檔案helloWorld.jsp,在eclipse項目在WebContent檔案夾。

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

建立頁面:
我們還需要在WebContent檔案夾中建立 index.jsp。該檔案將作為初始動作URL,使用者可以在其中點擊告訴Struts 2架構調用 HelloWorldAction類定義的方法呈現 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>

Hello 動作定義在上面的視圖檔案將被映射到HelloWorldAction類和其執行方法使用struts.xml檔案。

設定檔
現在,我們需要註冊我們的攔截器,然後調用它預設的攔截器在前面的例子中調用。要註冊一個新定義的攔截,直接放在的<interceptors>...</interceptors>標籤下<package>的標籤外掛程式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">  <interceptors>   <interceptor name="myinterceptor"   class="com.yiibai.struts2.MyInterceptor" />  </interceptors>  <action name="hello"    class="com.yiibai.struts2.HelloWorldAction"    method="execute">   <interceptor-ref name="params"/>   <interceptor-ref name="myinterceptor" />   <result name="success">/HelloWorld.jsp</result>  </action> </package></struts>

應該指出的是,可以註冊多個攔截器<package>標籤內,同一時間,可以調用多個攔截裡面的<action>標籤。可以調用相同的攔截器與不同的動作。

web.xml檔案需要在 WEB-INF檔案夾下建立 WebContent 如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>Struts 2</display-name> <welcome-file-list>  <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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></web-app>

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

現在,在給定的文字框中輸入任何單詞,並單擊“Say Hello“ 按鈕執行已定義的動作。現在,如果檢查產生的日誌,會發現下面的文本下方:

 

Pre-ProcessingInside action....Post-Processing

堆疊多個攔截器:
可以想像,配置多個攔截器每個動作很快就會變得非常難以控制。出於這個原因,攔截器與攔截器棧管理。下面是一個例子,直接從在struts-default.xml檔案:

 

<interceptor-stack name="basicStack"> <interceptor-ref name="exception"/> <interceptor-ref name="servlet-config"/> <interceptor-ref name="prepare"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="params"/> <interceptor-ref name="conversionError"/></interceptor-stack>

上面的棧被調用basicStack,可用於在配置中,如下所示。此配置節點放置在<package.../>節點下。每個<interceptor-ref.../>標記引用一個攔截器或攔截器棧已配置在當前的攔截器棧。因此,這是非常重要的,以確保該名稱是唯一的所有攔截器和攔截器棧配置配置初始的攔截器和攔截器棧時。

我們已經看到了如何應用攔截的動作,將攔截器棧是沒有什麼不同。事實上,我們完全使用相同的標籤:

 

<action name="hello" class="com.yiibai.struts2.MyAction"> <interceptor-ref name="basicStack"/> <result>view.jsp</result></action

上述註冊的“basicStack”所有6個攔截器完成註冊的棧 Hello 動作。應該指出的是,攔截器執行的順序在配置中。例如,在上述情況下,異常將被執行,servlet 配置等。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.