struts2 action中result引數詳解

2021-08-26 00:16:27 字數 2775 閱讀 6486

chain

用來處理action鏈

com.opensymphony.xwork2.actionchainresult

dispatcher

用來轉向頁面,通常處理jsp

org.apache.struts2.dispatcher.servletdispatcherresult

freemaker

處理freemarker模板

控制特殊http行為的結果型別

重定向到乙個url

org.apache.struts2.dispatcher.servletredirectresult

redirectaction

重定向到乙個action

org.apache.struts2.dispatcher.servletactionredirectresult

stream

org.apache.struts2.dispatcher.streamresult

velocity

處理velocity模板

org.apache.struts2.dispatcher.velocityresult

xslt

處理xml/xlst模板

org.apache.struts2.views.xslt.xsltresult

plaintext

顯示原始檔案內容,例如檔案源**

org.apache.struts2.dispatcher.plaintextresult

redirect-action

重定向到乙個action

org.apache.struts2.dispatcher.servletactionredirectresult

plaintext

顯示原始檔案內容,例如檔案源**

org.apache.struts2.dispatcher.plaintextresult

簡單說明一下result的name屬性和type屬性:

success:action正確的執行完成,返回相應的檢視,success是name屬性的預設值;

none:表示action正確的執行完成,但並不返回任何檢視;

error:表示action執行失敗,返回到錯誤處理檢視;

input:action的執行,需要從前端介面獲取引數,input就是代表這個引數輸入的介面,一般在應用中,會對這些引數進行驗證,如果驗證沒有通過,將自動返回到該檢視;

login:action因為使用者沒有登陸的原因沒有正確執行,將返回該登陸檢視,要求使用者進行登陸驗證。

dispatcher:請求**,底層呼叫requestdispatcher的forward()或include()方法,dispatcher是 type屬性的預設值,通常用於轉向乙個jsp,localtion指定jsp的位置,parse如果為false表示location的值不會被當作 ognl解析,預設為true;

redirect:重定向,新頁面無法顯示action中的資料,因為底層呼叫response.sendredirect("")方法,無法共享請求範圍內的資料,引數與dispatcher用法相同;

redirect-action:重定向到另乙個action,引數與chain用法相同,允許將原action中的屬性指定新名稱帶入新action 中,可以在result標籤中新增 $ ,這表示原action中的變數a的值被轉給b,下乙個action可以在值棧中使用b來操作,注意如果值是中文,需要做一些編碼處理,因為tomcat預設是不支援url直接傳遞中文的!

velocity:使用velocity模板輸出結果,location指定模板的位置(*.vm),parse如果為false,location不被ognl解析,預設為true;

xslt:使用xslt將結果轉換為xml輸出,location指定*.xslt檔案的位置,parse如果為false,location不被 ognl解析,預設為true,matchingpattern指定想要的元素模式,excludepattern指定拒絕的元素模式,支援正規表示式,預設為接受所有元素;

httpheader:根據值棧返回自定義的httpheader,status指定響應狀態(就是指response.senderror(int i)重定向到500等伺服器的狀態頁),parse如果為false,header的值不會被ognl解析,headers,加入到header中的值,例如: helloworld ,可以加多個,這些鍵-值組成hashmap;

freemaker:用freemaker模板引擎呈現檢視,location指定模板(*.ftl)的位置,parse如果為false,location的值不會被ognl解析,contenttype指定以何中型別解析,預設為text/html;

chain:將action的帶著原來的狀態請求**到新的action,兩個action共享乙個actioncontext,actionname指定轉向的新的action的名字,method指定轉向哪個方法,namespace指定新的action的命名空間,不寫表示與原action在相同的命名空間;skipactions指定乙個使用 , 連線的action的name組成的集合,一般不建議使用這種型別的結果;

plaintext:以原始文字顯示jsp或者html,location指定檔案的位置,charset指定字符集;

struts2 action中result引數詳解

chain 用來處理action鏈 com.opensymphony.xwork2.actionchainresult dispatcher 用來轉向頁面,通常處理jsp org.apache.struts2.dispatcher.servletdispatcherresult freemaker ...

struts2 action中result引數詳解

none 表示action正確的執行完成,但並不返回任何檢視 error 表示action執行失敗,返回到錯誤處理檢視 input action的執行,需要從前端介面獲取引數,input就是代表這個引數輸入的介面,一般在應用中,會對這些引數進行驗證,如果驗證沒有通過,將自動返回到該檢視 login ...

struts2 action 配置方法

struts2 action幾乎完全吸收了xwork 的精華,近日學習struts 2,總結一下action的配製方法。首先看一下com.opensymphony.xwork2.action的介面宣告,action提供execute 方法,子類必須實現execute 方法。public inte c...