鍍金池/ 教程/ Java/ 使用Struts2動作
設(shè)置復(fù)選框的默認(rèn)值
<s:if>, <s:elseIf>, <s:else> 標(biāo)簽示例
下拉框示例
Struts2 &lt;s:bean&gt;標(biāo)簽示例
Struts2 &lt;s:date&gt;標(biāo)簽示例
<s:text>標(biāo)簽示例
Struts2的ActionError&ActionMessage示例
Struts2 MySQL數(shù)據(jù)庫訪問
Struts2上傳多個文件例子
Struts2+Log4j集成
Struts 2 hello world (XML版本)
Struts2和Spring集成
Struts2 控件標(biāo)簽
Struts2 和 JSON 實例
Struts2 環(huán)境配置
Struts2 &lt;s:checkboxlist&gt;多個復(fù)選框例子
Struts2 注釋類型
在Struts2中配置靜態(tài)參數(shù)
Struts2 驗證框架
Struts2 param 標(biāo)簽
Struts2 &lt;s:hidden&gt;隱藏值例子
Struts2下載文件實例
Struts2 &lt;sx:autocompleter&gt;自動提示例子
Struts2 action標(biāo)簽
autocompleter自動完成示例
Struts2 merge標(biāo)簽
Struts2 &lt;s:action&gt;標(biāo)簽示例
<s:property>標(biāo)簽示例
Struts2 &lt;s:sort&gt;標(biāo)簽示例
Struts2和Hibernate集成
自動選擇下拉框示例
Struts2 結(jié)果和結(jié)果類型
Struts2 是什么?
如何刪除Struts2動作的后綴擴展名
Struts2 Hello World 實例
<s:action>標(biāo)簽示例
<s:bean>標(biāo)簽示例
Struts2 + Quartz調(diào)度集成實例
如何獲取ServletContext對象
Struts2 &lt;s:i18n&gt;標(biāo)簽示例
Struts2 append標(biāo)簽
i18n 或本地化示例
autocompleter + JSON 示例
Struts2獲取HttpServletRequest實例
updownselect 示例
Struts2 &lt;s:debug&gt;標(biāo)簽示例
Struts2重寫攔截器參數(shù)
多個Struts配置文件示例
<s:i18n>標(biāo)簽示例
Struts2中文本地化問題
文件上傳示例
Struts2 &lt;s:property&gt;標(biāo)簽實例
Struts2攔截器棧的例子
<s:merge>標(biāo)簽示例
Struts2 數(shù)據(jù)標(biāo)簽
Struts2 &lt;s:merge&gt;標(biāo)簽示例
復(fù)選框checkboxes 示例
Struts2 generator標(biāo)簽
如何配置Struts2全局資源包
key 屬性示例
Struts2 注釋
FilterDispatcher 和 StrutsPrepareAndExecuteFilter區(qū)別?
Struts 2 Hello World (注解版本)
Struts 2 + Log4j 集成實例
Struts2 值棧/OGNL
Struts2配置Action類的靜態(tài)參數(shù)
Struts2 本地化/國際化(i18n)
Chinese 本地化問題
Struts2單選預(yù)選按鈕值
Struts2 include標(biāo)簽
Struts2 + Hibernate 集成實例
配置全局資源包
Struts2 異常處理
Struts2 &lt;s:set&gt;標(biāo)簽示例
Struts2 &lt;s:doubleselect&gt;示例
Struts2 iterator標(biāo)簽
optiontransferselect 示例
Struts2映射攔截動作
head 示例
Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2命名空間配置和解釋
<s:param>標(biāo)簽示例
Struts2+Hibernate集成實例
Struts2 &lt;s:head&gt;示例
Struts2 &lt;s:push&gt;標(biāo)簽示例
Struts2文件上傳例子
Struts2 &lt;s:param&gt;標(biāo)簽示例
@ResultPath 注釋示例
Struts2 體系結(jié)構(gòu)
Struts2 execAndWait攔截器例子
Struts2 攔截器
Textarea - 文本域
Struts2教程
<s:push>標(biāo)簽示例
Struts2 文件上傳
Struts2 date 標(biāo)簽
Struts2 &lt;s:texttag&gt;標(biāo)簽示例
Struts2+Quartz2調(diào)度集成實例
Struts2開發(fā)模式
Struts2 autocompleter+JSON例子
Struts2 類型轉(zhuǎn)換
<s:url>標(biāo)簽示例
Struts2 Ajax 標(biāo)簽
Struts2 bean 標(biāo)簽
<s:iterator>標(biāo)簽迭代示例
Hidden隱藏值示例
在Struts2獲取HttpServletResponse對象
Struts2 主題和模板
Struts2自動選擇下拉框的值
Struts2下載文件實例
在Struts2中獲取 HttpServletRequest
級聯(lián)選擇示例
Struts2 &lt;sx:datetimepicker&gt;示例
Struts2 include(包含)多個配置文件
Struts2 &lt;s:select&gt;下拉框例子
Struts2 Action/動作
Struts2 &lt;s:textarea&gt;文本域?qū)嵗?/span>
Struts2+Spring集成實例
Struts2 &lt;s:updownselect&gt;示例
Struts2 property 標(biāo)簽
Struts2 &lt;s:url&gt;標(biāo)簽示例
Struts2 表單標(biāo)簽
Struts2資源包使用示例
subset tag example
Struts 2 ActionError &amp; ActionMessage Example
Struts2 MVC架構(gòu)
Struts2 if/else標(biāo)簽
Struts2 &lt;s:generator&gt;標(biāo)簽示例
<s:include>標(biāo)簽示例
Struts2開發(fā)者模式
<s:generator> 標(biāo)簽示例
復(fù)選框heckbox 示例
Struts2 text 標(biāo)簽
Radio 單選按鍵示例
Struts2 &lt;s:a&gt;標(biāo)簽示例
Struts2 key鍵屬性示例
Struts2設(shè)置多個復(fù)選框缺省值
Struts2 基于XML校驗
Struts2 配置文件
Struts2 &lt;s:radio&gt;單選按鈕例子
Struts2 發(fā)送郵件Email
Struts2獲取HttpServletResponse實例
<s:sort>標(biāo)簽示例
Struts2 &lt;s:optiontransferselect&gt;示例
Struts2和Tiles集成
Struts2模型驅(qū)動實例
Struts2的FilterDispatcher和StrutsPrepareAndExecuteFilter有什么不同?
<s:append>標(biāo)簽示例
組合框示例
Struts2 &lt;s:password&gt;密碼示例
datetimepicker 日期選擇
Struts2 &lt;s:iterator&gt;迭代器標(biāo)簽示例
TextBox文檔示例
Struts2 url標(biāo)簽
資源包使用示例
Struts2本地化和國際化
Struts2 &lt;s:subset&gt;標(biāo)簽示例
Struts2 + Hibernate使用“Full Hibernate Plugin"插件集成
Struts2和JSON實例
Struts2注解示例
Struts2 &lt;s:checkbox&gt;復(fù)選框例子
Struts2快速入門
<s:date>標(biāo)簽示例
Struts2 set 標(biāo)簽
<s:set>標(biāo)簽示例
Struts2 命名空間配置和解釋
Struts 2 hello world (XML版本)
&nbsp;預(yù)選單選按鈕
<s:a>標(biāo)簽示例
Struts2+Spring+Hibernate集成實例
Struts2 &lt;s:combobox&gt;組合框的例子
Struts2 if,elseif,else標(biāo)簽示例
Struts2 @ResultPath注釋示例
struts2 &lt;s:append&gt;標(biāo)簽示例
Struts2 &lt;s:textfield&gt;文本示例
多文件上傳示例
使用Struts2動作
<s:debug>標(biāo)簽示例
在Struts2中如何獲取ServletContext對象
Struts2 &lt;s:include&gt;標(biāo)簽示例
Password 示例

使用Struts2動作

在Struts2中,會花大部分的時間用在用動作來處理工作。動作類包含業(yè)務(wù)邏輯,獲取資源包,保存數(shù)據(jù),驗證,并選擇應(yīng)發(fā)回給用戶的視圖的結(jié)果頁面。這是Struts2的核心,所以必須要了解動作的基本概念。

1. 動作 - Action

Struts 2動作不強迫你實現(xiàn)任何接口或擴展類,它只是需要你實現(xiàn)一個 execute()方法返回一個字符串來表示其應(yīng)該返回的結(jié)果頁面。

package com.yiibai.user.action; 
public class LoginAction{
    //business logic
    public String execute() {
        return "success";
    }
}

在struts.xml中,配置使用Action類動作(action)標(biāo)記和類屬性。定義結(jié)果頁面用結(jié)果的標(biāo)簽和動作名稱返回給用戶,可以用它來訪問動作類的名稱屬性。

<package name="user" namespace="/User" extends="struts-default">
  <action name="validateUser" class="com.yiibai.user.action.LoginAction">
    <result name="success">pages/welcome.jsp</result>
  </action>
<package>
現(xiàn)在,可以通過 .action 擴展名后綴訪問動作。
http://localhost:8080/Struts2Example/User/validateUser.action 
默認(rèn).action是可配置的,只需要設(shè)置“struts.action.extension”的值,以滿足您的需要。

2. 可選動作接口

Struts 2帶有一個可選的動作接口(com.opensymphony.xwork2.Action)。通過實現(xiàn)這個接口,它帶來了一些方便和好處,看下面的源代碼:

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

這個接口是非常簡單的,配有5常用常數(shù)值: success, error, none, input and logic?,F(xiàn)在的動作類可以直接使用常量。

package com.yiibai.user.action;
import com.opensymphony.xwork2.Action;
public class LoginAction{
    //business logic
    public String execute() {
        return SUCCESS;
    }
}
不明白為什么很多Struts開發(fā)人員喜歡實現(xiàn)此動作接口,它更好地擴展了ActionSupport。

3. ActionSupport

Support 類,通常的做法是提供接口的默認(rèn)實現(xiàn)。

 ActionSupport (com.opensymphony.xwork2.ActionSupport), 一個非常強大和方便的類,它提供了幾個重要接口的缺省實現(xiàn):

public class ActionSupport implements Action, Validateable, 
   ValidationAware, TextProvider, LocaleProvider, Serializable {
 ...
}

ActionSupport 類提供一些功能:

1. 驗證 – 聲明一個validate()方法,并在里面實現(xiàn)驗證代碼。

2. 文字本地化 – 使用gettext()方法來獲得資源包的消息。

package com.yiibai.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
    private String username;
    private String password;
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    //business logic
    public String execute() {
        return "SUCCESS";
    }
        //simple validation
    public void validate(){
        if("".equals(getUsername())){
            addFieldError("username", getText("username.required"));
        }
        if("".equals(getPassword())){
            addFieldError("password", getText("password.required"));
        }
    }
}
在大多數(shù)情況下,應(yīng)該擴展此類妥當(dāng),方便提供功能,除非你有理由不這樣做。這也是一個很不錯的學(xué)習(xí)類,以了解如何做一些重要的Struts2接口的實現(xiàn)。

4. 動作注釋

Struts 2對注解有很好的支持,你可以擺脫XML文件,并使用@action在動作類上替換。

package com.yiibai.user.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/User")
@ResultPath(value="/")
public class ValidateUserAction extends ActionSupport{
    @Action(value="Welcome", results={
        @Result(name="success",location="pages/welcome_user.jsp")
    })
    public String execute() {
        return SUCCESS;
    }
}

總結(jié)

擴展ActionSupport類,它適合在大多數(shù)情況下。