動作是 Struts 2 框架的核心,因為它們是服務(wù)于任何 MVC(模型-視圖-控制器)的框架。每個 URL 被映射到一個指定的動作中,它提供了必要的處理邏輯來服務(wù)用戶的請求。
但是動作也在其他兩個重要的能力上起作用。首先,動作在從請求到視圖傳輸數(shù)據(jù)中起著重要的作用,無論它是一個 JSP 還是其它的結(jié)果類型。第二,動作必須協(xié)助框架來確定哪些結(jié)果應(yīng)該呈現(xiàn)給視圖,該視圖在響應(yīng)中返回給請求。
對 Struts 2 中動作的唯一要求是必須有一個無參數(shù)方法,該方法返回 String 或結(jié)果對象,而且必須是一個 POJO。如果無參數(shù)方法沒有被指定,那么默認(rèn)的動作是使用 execute() 方法。
你可以選擇擴展 ActionSupport 類,它實現(xiàn)了包括動作接口的 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.tutorialspoint.Struts 2;
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;
}
}
為了舉例說明動作方法控制著視圖,讓我們對 execute 方法做下面的修改,并且擴展 ActionSupport 類,如下所示:
package com.tutorialspoint.Struts 2;
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 作為結(jié)果,否則我們就返回 ERROR 作為結(jié)果。因為我們已經(jīng)擴展了 ActionSupport,所以我們可以使用字符串常量 SUCCESS 和 ERROR?,F(xiàn)在,讓我們修改我們的 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.Struts 2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
<result name="error">/AccessDenied.jsp</result>
</action>
</package>
</struts>
讓我們在 eclipse 項目的 WebContent 文件夾下創(chuàng)建下面的 jsp 文件 HelloWorld.jsp。為了做到這個,在項目資源管理器的 WebContent 文件夾上點擊右鍵,并選擇 New >JSP File。假如返回的結(jié)果是 SUCCESS,則這個文件將被調(diào)用,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>
假如返回的結(jié)果是 ERROR,則下面的文件將被框架調(diào)用,ERROR等于一個字符串常量 “error”。下面是 AccessDenied.jsp 的內(nèi)容。
<%@ 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 文件夾中創(chuàng)建 index.jsp。這個文件將作為初始動作 URL,用戶可以點擊它來告訴 Struts 2 框架調(diào)用 HelloWorldAction 類的 execute 方法,并且呈現(xiàn) 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 Struts 2</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 文件,所以我們使用 Examples 章節(jié)中已經(jīng)創(chuàng)建的同一個 web.xml?,F(xiàn)在,我們已經(jīng)準(zhǔn)備好使用 Struts 2 框架來運行我們的 Hello World 應(yīng)用程序。
在項目名稱上點擊右鍵,并且單擊 Export > WAR File 來創(chuàng)建一個 War 文件。然后在 Tomcat 的 webapps 目錄中部署這個 WAR。最后,啟動 Tomcat 服務(wù)器,并且嘗試訪問 URL http://localhost:8080/HelloWorldStruts 2/index.jsp
. 它將會給出下面的畫面:
http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts4.jpg" alt="" />
讓我們輸入一個單詞 “SECRET”,你應(yīng)該可以看到下面的頁面:
http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts51.jpg" alt="" />
現(xiàn)在輸入除了 “SECRET” 以外的任何單詞,你應(yīng)該可以看到下面的頁面:
http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts6.jpg" alt="" />
你將經(jīng)常定義一個以上的動作來處理不同的請求,并且給用戶提供不同的 URL,因此你可以定義不同的類,定義如下所示:
package com.tutorialspoint.Struts 2;
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.tutorialspoint.Struts 2.HelloWorld"
method="execute">
<result name="success">/HelloWorld.jsp</result>
<result name="error">/AccessDenied.jsp</result>
</action>
<action name="something"
class="com.tutorialspoint.Struts 2.SomeOtherClass"
method="execute">
<result name="success">/Something.jsp</result>
<result name="error">/AccessDenied.jsp</result>
</action>
</package>
</struts>
正如你在上述假設(shè)的例子中看到的,動作的結(jié)果 SUCCESS 和 ERROR 是重復(fù)的。為了解決這個問題,建議你創(chuàng)建一個包含輸出結(jié)果的類。