Struts2 result type(型別)總結

2021-08-31 14:03:27 字數 2173 閱讀 4670

在預設時,標籤的type屬性值是「dispatcher」(實際上就是**,forward)。開發人員可以根據自己的需要指定不同的型別,如redirect、stream等。如下面**所示:

/result.jsp

這此result-type可以在struts2-core-2.0.11.1.jar包或struts2源**中的struts-default.xml檔案中找到,在這個檔案中找到標籤,所有的result-type都在裡面定義了。**如下:

chain    

用來處理action鏈,被跳轉的action中仍能獲取上個頁面的值,如request資訊。    

com.opensymphony.xwork2.actionchainresult    

dispatcher    

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

org.apache.struts2.dispatcher.servletdispatcherresult    

freemaker    

處理freemarker模板    

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

重定向到乙個url ,被跳轉的頁面中丟失傳遞的資訊,如request   

org.apache.struts2.dispatcher.servletredirectresult    

redirectaction    

重定向到乙個action ,跳轉的頁面中丟失傳遞的資訊,如request      

org.apache.struts2.dispatcher.servletactionredirectresult    

redirect-action    

重定向到乙個action ,跳轉的頁面中丟失傳遞的資訊,如request      

org.apache.struts2.dispatcher.servletactionredirectresult

注:redirect與redirect-action區別

一、使用redirect需要字尾名 使用redirect-action不需要字尾名 

二、type="redirect" 的值可以轉到其它命名空間下的action,而redirect-action只能轉到同一命名空下的 action,因此它可以省略.action的字尾直接寫action的名稱。

如:viewtask.action

viewtask

附:redirect-action 傳遞引數

xml**

< action   name = "enterpreinfo"   class = "preinfobusinessaction"      method = "enterpreinfosub" >    

< result   name = "success"   type = "redirect -action" >    

showpreinfo? preinfo.order_number =$& preinfo.company_name =$    

< result   name = "error"   type = "redirect " >    

< param   name = "location" > /error.jsp     

因為使用了redirect -action,所以要注意不能將 showpreinf?preinfo.order_number=$寫成 showpreinf.action?preinfo.order_number=$

其中${}為el表示式,獲取action:enterpreinfo中屬性的值 ; 在這個配置檔案裡,多個引數的連線符使用了"&",但xml的語法規範,應該使用"&"代替"&",原理和html中的轉義相同,開始沒有注意,在struts 分析配置檔案時,總是報出這樣的錯誤:

the reference to entity "preinfo" must end with the ';' delimiter.   

the reference to entity "preinfo" must end with the ';' delimiter.

Struts 2 Struts2 詳細配置

全域性配置 配置在packe 中,所有 action 的上面。頁面 頁面 區域性結果優先順序高於全域性。異常資訊配置 404異常 500異常 web.xml 配置 404 error.jsp 500 error.jsp 包 package 配置 所有的action 配置,都必須在包中。jsp頁面路徑...

Struts2字尾 深入Struts2

一 將action字尾變成html字尾 xmlversion 1.0 encoding utf 8 doctype struts public apache software foundation dtd struts configuration 2.1 en struts include file...

Struts2入門(一)Struts2簡介

本章簡要介紹一下struts2框架 1.概念 我們知道,springmvc框架是為了整合servlet設計的控制層框架,那麼還有其他的框架也實現了這個功能,那麼就是struts2。struts2是乙個基於mvc設計模式的web應用框架,它本質上相當於乙個servlet,在mvc設計模式中,strut...