即,mvc模型的控制器模型,用於接收數據,傳遞給視圖層,和模型層 默認使用execute方法html
查看com.opensymphony.xwork2下的Action接口 文件以下express
/*
* 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 <b>may</b> implement this interface, which exposes the <code>execute()</code> method.
* <p>
* However, as of XWork 1.1, this is <b>not</b> 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.
* </p>
*/
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";
/**
* <p>
* 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.
* </p>
*
* <p>
* This result is also used if the given input
* params are invalid, meaning the user
* should try providing input again.
* </p>
*/
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.
* <b>Note:</b> Application level exceptions should be handled by returning
* an error value, such as <code>Action.ERROR</code>.
*/
public String execute() throws Exception;
}
複製代碼
大概翻譯一下apache
*
*得到Apache軟件基金會(ASF)的許可
*或更多貢獻者許可協議。請參閱NOTICE文件
*與此工做一塊兒分發以獲取更多信息
*關於版權全部權。 ASF許可此文件
*根據Apache許可證2.0版(
* 「執照」);除非符合規定,不然您不得使用此文件
*使用許可證。您能夠在如下位置獲取許可證副本
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非適用法律要求或書面贊成,
*根據許可證分發的軟件分發在
*「按原樣」基礎,不提供任何保證或條件
* KIND,不管是明示的仍是暗示的。請參閱許可證
*管理權限和限制的特定語言
*根據許可證。
* /
package com.opensymphony.xwork2;
/ **
*全部動做<b>可能</ b>實現此接口,該接口公開<code> execute()</ code>方法。
* <p>
*可是,從XWork 1.1開始,這<b>不</ 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>
*
* <p>
*若是給定輸入,也會使用此結果
*參數無效,意味着用戶
*應該嘗試再次提供輸入。
* </ p>
* /
public static final String INPUT =「input」;
/ **
*行動沒法執行,由於
*用戶最多未登陸。登陸視圖
*應該顯示。
* /
public static final String LOGIN =「login」;
/ **
*執行動做的邏輯。
*
* @return表示執行邏輯結果的字符串。
*有關標準結果值的列表,請參閱此界面中的常量。
* @throws若是發生系統級異常,則拋出異常。
* <b>注意:</ b>應經過返回來處理應用程序級異常
*錯誤值,例如<code> Action.ERROR </ code>。
* /
public String execute()拋出異常;
}
複製代碼
能夠看到,定義了幾個常量一個接口,其中默認執行execute方法,其中幾個常量爲執行結果的常量bash
/**
* 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
複製代碼
大概翻譯一下mvc
*爲最多見的操做提供默認實現。
*有關更多詳細信息,請參閱此類實現的全部接口的文檔。
*/
複製代碼
因此直接擴展該類便可app
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中書寫業務邏輯 從新更改以下less
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 否jsp
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 定義調試 -->
<constant name="struts.devMode" value="true" />
<!-- 定義數據包 -->
<package name="helloworld" extends="struts-default">
<!-- 定義處理邏輯 name爲指定處理的名稱 class 處理的包文件 method 處理將會調用的方法-->
<action name="hello"
class="com.ming.HelloWorldAction"
method="execute">
<!-- 成功返回頁面 -->
<result name="success">/HelloWorld.jsp</result>
<result name="error">/error.html</result>
</action>
</package>
</struts>
複製代碼