Struts Action 控制器

控制器

即,mvc模型的控制器模型,用於接收數據,傳遞給視圖層,和模型層

默認使用execute方法

查看相關接口

查看com.opensymphony.xwork2下的Action接口

文件如下

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.opensymphony.xwork2;
/**
* All actions may implement this interface, which exposes the <code>execute()/<code> method.
*


* However, as of XWork 1.1, this is not required and is only here to assist users. You are free to create POJOs
* that honor the same contract defined by this interface without actually implementing the interface.
*


*/
public interface Action {

/**
* The action execution was successful. Show result
* view to the end user.
*/
public static final String SUCCESS = "success";

/**
* The action execution was successful but do not
* show a view. This is useful for actions that are
* handling the view in another fashion like redirect.
*/
public static final String NONE = "none";

/**
* The action execution was a failure.
* Show an error view, possibly asking the
* user to retry entering data.
*/
public static final String ERROR = "error";
/**
*


* The action execution require more input
* in order to succeed.
* This result is typically used if a form
* handling action has been executed so as
* to provide defaults for a form. The
* form associated with the handler should be
* shown to the end user.
*


*
*


* This result is also used if the given input
* params are invalid, meaning the user
* should try providing input again.
*


*/
public static final String INPUT = "input";

/**
* The action could not execute, since the
* user most was not logged in. The login view
* should be shown.
*/
public static final String LOGIN = "login";
/**
* Where the logic of the action is executed.
*
* @return a string representing the logical result of the execution.
* See constants in this interface for a list of standard result values.
* @throws Exception thrown if a system level exception occurs.
* Note:
Application level exceptions should be handled by returning
* an error value, such as <code>Action.ERROR/<code>.
*/
public String execute() throws Exception;
}

大概翻譯一下

 *
*獲得Apache軟件基金會(ASF)的許可
*或更多貢獻者許可協議。請參閱NOTICE文件
*與此工作一起分發以獲取更多信息
*關於版權所有權。 ASF許可此文件
*根據Apache許可證2.0版(
* “執照”);除非符合規定,否則您不得使用此文件
*使用許可證。您可以在以下位置獲取許可證副本
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非適用法律要求或書面同意,
*根據許可證分發的軟件分發在
*“按原樣”基礎,不提供任何保證或條件
* KIND,無論是明示的還是暗示的。請參閱許可證
*管理權限和限制的特定語言
*根據許可證。
* /
package com.opensymphony.xwork2;
/ **
*所有動作可能 b>實現此接口,該接口公開<code> execute() code>方法。

*


*但是,從XWork 1.1開始,這不 b>是必需的,僅用於幫助用戶。您可以自由創建POJO
*遵守此接口定義的相同合同而不實際實現接口。
* p>
* /
public interface Action {

/ **
*行動執行成功。顯示結果
*查看最終用戶。
* /
public static final String SUCCESS =“success”;

/ **
*行動執行成功但沒有
*顯示一個視圖。這對於有效的操作很有用
*以重定向等其他方式處理視圖。
* /
public static final String NONE =“none”;
/ **
*行動執行失敗。
*顯示錯誤視圖,可能會詢問
*用戶重試輸入數據。
* /
public static final String ERROR =“error”;
/ **
*


*動作執行需要更多輸入
*為了成功。
*此結果通常用於表格


*處理行動已經執行
*提供表單的默認值。該
*與處理程序關聯的表單應該是
*向最終用戶顯示。
* p>
*
*


*如果給定輸入,也會使用此結果
*參數無效,意味著用戶
*應該嘗試再次提供輸入。
* p>
* /
public static final String INPUT =“input”;
/ **
*行動無法執行,因為
*用戶最多未登錄。登錄視圖
*應該顯示。
* /
public static final String LOGIN =“login”;


/ **
*執行動作的邏輯。
*
* @return表示執行邏輯結果的字符串。
*有關標準結果值的列表,請參閱此界面中的常量。
* @throws如果發生系統級異常,則拋出異常。
* 注意: b>應通過返回來處理應用程序級異常
*錯誤值,例如<code> Action.ERROR code>。


* /
public String execute()拋出異常;
}
/<code>

/<code>

可以看到,定義了幾個常量一個接口,其中默認執行execute方法,其中幾個常量為執行結果的常量

擴展實現Action接口的ActionSupport類

/**
* Provides a default implementation for the most common actions.
* See the documentation for all the interfaces this class implements for more detailed information.
*/
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable

大概翻譯一下

*為最常見的操作提供默認實現。
*有關更多詳細信息,請參閱此類實現的所有接口的文檔。
*/

所以直接擴展該類即可

重新擴展HelloWorldAction

package com.ming;

import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String name;

@Override
public String execute() throws Exception {
return "success";

}

public String getName() {
return name;
}

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

可以在execute中書寫業務邏輯

重新更改如下

package com.ming;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String name;

@Override
public String execute() throws Exception {
if(SUCCESS.equals(name)){
// 此時返回SUCCESS
return SUCCESS;
}else{
// 其餘內容返回error
return ERROR;
}
}
public String getName() {
return name;
}

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

在上方,根據name的值,完成了一個業務邏輯,返回是 or 否

編寫配置文件



br> "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

<constant>

<package>

<action> class="com.ming.HelloWorldAction"
method="execute">

<result>/HelloWorld.jsp/<result>
<result>/error.html/<result>
/<action>
/<package>
/<struts>

效果如下

Struts Action 控制器

Struts Action 控制器


分享到:


相關文章: