日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts2中 Result类型配置详解

發布時間:2025/6/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2中 Result类型配置详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個result代表了一個可能的輸出。當Action類的方法執行完成時,它返回一個字符串類型的結果碼,框架根據這個結果碼選擇對應的result,向用戶輸出。
com.opensymphony.xwork2.Action接口中定義了一組標準的結果代碼,可供開發人員使用,當然了只有我們的action繼承ActionSupport這個類才可以使用下面的結果代碼,如下所示:
public interface Action
{
??? publicstatic final String SUCCESS = “success”;
??? publicstatic final String NONE = “none”;
??? publicstatic final String ERROR = “error”;
??? publicstatic final String INPUT = “input”;
??? publicstatic final String LOGIN = “login”;
}
?? 其中
Struts2應用在運行過程中若發現addFieldError()中有信息或者類型轉換失敗或著輸入校驗失敗等情況
那么它會自動跳轉到name為input<result/>,然后轉到INPUT所對應的頁面
若JSP頁面中表單是用普通<form>編寫的,發生錯誤而返回該頁面時,則原數據將消失
若JSP頁面中表單是用<s:form/>編寫的,發生錯誤而返回該頁面時,則原數據仍存在
若沒有提供name值為input的<result/>,那么發生錯誤時,將直接在瀏覽器中提示404錯誤?
? 除了這些預定義的結果碼外,開發人員也可以定義其它的結果碼來滿足自身應用程序的需
要。
???Result配置由兩部分組成:一部分是result映射,另一部分是result類型。下面我們分別對
這兩部分進行介紹。
一、配置 result映射
??在result映射的配置中,在指定實際資源的位置時,可以使用絕對路徑,也可以使用相對路徑。
絕對路徑以斜杠(/)開頭,相對于當前的Web應用程序的上下文路徑;
相對路徑不以斜杠(/)開頭,相對于當前執行的action的路徑,也就是namespace指定的路徑。

例如:
? <package name="default"extends="struts-default" namespace="/admin">
???<action name="login"class="com.ibm.LoginAction">
?????????<result>success.jsp</result>
?????????<resultname="error">/error.jsp</result>
???</action>
?</package>
???如果當前Web應用程序的上下文路徑是/Shop,那么請求/Shop/admin/login.action,執行成功后,轉向的頁面路徑為:/Shop/admin/success.jsp;執行失敗后,轉向的頁面路徑為/Shop/error.jsp.


二、result結果類型詳解
?

type 所有類型 :(在struts2-core.jar/struts-default.xml中可以找到)

Type類型值

作用說明

對應類

chain

用來處理Action

com.opensymphony.xwork2.ActionChainResult

dispatcher(默認值)

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

org.apache.struts2.dispatcher.ServletDispatcherResult

redirect

重定向到一個URL

org.apache.struts2.dispatcher.ServletRedirectResult

redirectAction

重定向到一個 Action

org.apache.struts2.dispatcher.ServletActionRedirectResult

plainText

顯示源文件內容,如文件源碼

org.apache.struts2.dispatcher.PlainTextResult

freemarker

處理 FreeMarker 模板

org.apache.struts2.views.freemarker.FreemarkerResult

httpheader

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

org.apache.struts2.dispatcher.HttpHeaderResult

stream

?

向瀏覽器發送 InputSream 對象,通常用來處理文件下載,還可用于返回 AJAX 數據。

?

org.apache.struts2.dispatcher.StreamResult

?

velocity

處理 Velocity 模板

org.apache.struts2.dispatcher.VelocityResult

xslt??

??處理 XML/XLST 模板

?org.apache.struts2.views.xslt.XSLTResult

?
? 1、dispatcher結果類型
?? Struts2在后臺使用Servlet API的RequestDispatcher來轉發請求,因此在用戶的整個請求/響應過程中,目標Servlet/JSP接收到的request/response對象,與最初的Servlet/JSP相同。
?
?? Dispatcher結果類型的實現是org.apache.struts2.dispatcher.ServletDispatcherResult,該類的二個屬性(property):location和parse,這兩個屬性可以通過struts.xml配置文件中的result元素的param子元素來設置。param元素的name屬性指定結果類型實現類的屬性名,param元素的內容是屬性的值。例如:
? <resultname=“success”?type=“dispatcher”>
???<param name=“location”>/success.jsp</param>
????<param name=“parse”>true</param>
</result>

? 說明:
?? A、location參數用于指定action執行完畢后要轉向的目標資源,parse屬性是一個布爾類型的值,如果為true,則解析location參數中的OGNL表達式;如果為false,則不解析。parse屬性的默認值就是true.
location參數是默認的參數,在所有的Result實現類中,都定義了一個字符串類型的DEFAULT_PARAM靜態常量,專門用于指定默認的參數名。DEFAULT_PARAM常量的定義:public static final StringDEFAULT_PARAM=“location”;

? B、在設置location參數時,可以在參數值中使用OGNL表達式。
<action name=“viewNews”class=“com.ibm.ViewNewsAction”
<result name=“success”?type=“dispatcher”>
???<!--
如果參數是中文:請參看最底部例子-->
??? <param name=“location”>/viewNews.jsp?id=${id}</param>
????<param name=“parse”>true</param>

</result>
</action>???
考慮到默認值的使用(dispatcher和location都是默認值),上述可以簡化為:
<action name=“viewNews”class=“com.ibm.ViewNewsAction”>
<result name=“success”>viewNews.jsp?id=${id}</result>
</action>
?2、redirect結果類型(重定向到一個Url,也可以是Action或一個頁面)
??Redirect結果類型在后臺使用HttpServletResponse的sendRedirect方法將請求重定向到指定的URL,它的實現類是org.apache.struts2.dispatcher.ServletRedirectResult.該
同樣有二個屬性(property):location和parse,在使用redirect結果類型的場景中,用戶要完成一次與服務器之間的交互,瀏覽器需要完成兩次請求,因此第一次請求中的數據在第二次請求中是不可用的,這意味在目標資源中是不能訪問action實例、action錯誤以及錯誤等。
如果有某些數據需要在目標資源中訪問,
? i、一種方式是將數據保存到Session中,
? ii、另一種方式是通過請求參數來傳遞數據。

示例(1)、<result name="success" type="redirect">??
????????????<paramname="location">foo.jsp</param>
????????????<paramname="parse">false</param><!--不解析OGNL--
>
?????????</result>
示例(2)、
?? <packagename="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">??

?? <-- Passparameters (reportType, widthand height),重定向到Url并且傳參 ,如果參數是中文:請參看最底部例子-->??

??<!--??
? ??The redirect-action url generated will be:?
/genReport/generateReport.jsp?reportType=pie&width=100&height=100?
?
??-->??

??<actionname="gatherReportInfo"class="...">??

?????<resultname="showReportResult"type="redirect">??

????????<paramname="location">generateReport.jsp</param>??

????????<paramname="namespace">/genReport</param>??

????????<paramname="reportType">pie</param>??

????????<paramname="width">100</param>?

????????<paramname="height">100</param>?

????</result>??


?</action>??

</package>?

3、redirectAction結果類型(重定向到一個Action)
他經常用于防止表單重復提交,比方說在增加完用戶之后要顯示列表?
?redirectAction結果類型的實現類是org.apache.struts2.dispatcher.ServletActionRedirectResult,該類是ServletRedirectResult的子類,因此我們也就可以判斷出redirectAction結果類型和redirect結果類型的后臺工作原理是一樣的,即都是利用HttpServletResponse的sendRedirect方法將請求重定向到指定的URL。
示例、
? <packagename="public"extends="struts-default">??

???<actionname="login"class="...">??

???????<!--Redirect to anothernamespace重定向到不同命名空間下的???action -->?
???????<resulttype="redirectAction">??

??????????<param name="actionName">dashboard</param>?

??????????<param name="namespace">/secure</param>?

???????</result>?


??</action>?

</package>?

?

<package name="secure"extends="struts-default"namespace="/secure">??

??? <-- Redirectto an action in the samenamespace,重定向到同一命名空間下的action -->

???<action name="dashboard"class="...">??

??????<result>dashboard.jsp</result>?

???????<resultname="error"type="redirectAction">error</result>?

??</action>?

????<action name="error"class="...">??

??????<result>error.jsp</result>

???</action>?
</package>?

?

<packagename="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">??

??<-- Passparameters (reportType, width andheight),重定向到Action并且傳參
如果參數是中文:請參看最底部例子-->?

??<!--??
?? TheredirectAction urlgenerated will be:???
/genReport/generateReport.action?reportType=pie&width=100&height=100?
? ?-->?

?? <actionname="gatherReportInfo"class="...">??

?????<resultname="showReportResult" type="redirectAction">??

????????<paramname="actionName">generateReport</param>?

????????<paramname="namespace">/genReport</param>?

????????<paramname="reportType">pie</param>?

????????<paramname="width">100</param>

????????<paramname="height">100</param>

????????<paramname="empty"></param>

????????<paramname="supressEmptyParameters">true</param>?

????</result>?


?</action>?

</package>

4、鏈接類型?result:chain(從一個Action轉發到另一個Action
? chain結果類型有4個屬性,分別是:

???actionName (default) - the name of the action that will bechained to

???namespace - used to determine which namespace the Action isin that we're chaining. If namespace is null, this defaults to thecurrent namespace

???method - used to specify another method on target action tobe invoked. If null, this defaults to execute method

???skipActions - (optional) the list of comma separated actionnames for the actions that could be chained to


示例、
?<packagename="public"extends="struts-default">??

??? <!-- ChaincreatAccount to login, using the default parameter,鏈接到同一命名空間下的Action,-->?

???<action name="createAccount"class="...">??

???????<resulttype="chain">login</result>

??</action>?

???<actionname="login"class="...">??

???????<!--Chain to anothernamespace -->?

???????<resulttype="chain">??

??????????<paramname="actionName">dashboard</param>?

??????????<paramname="namespace">/secure</param>?

??????</result>?


??</action>?

</package>?

<package name="secure"extends="struts-default"namespace="/secure">??

???<actionname="dashboard"class="...">??

??????<result>dashboard.jsp</result>?

??</action>?

</package>
5、HttpHeaderResult:
HttpHeader(用來控制特殊的Http行為)
? httpheader結果類型很少使用到,它實際上是返回一個HTTP響應的頭信息
?示例:<resultname="success"type="httpheader">??
????<paramname="status">204</param>
????<paramname="headers.a">a customheadervalue</param>?????<paramname="headers.b">another customheadervalue</param>?
???</result>?

<resultname="proxyRequired"type="httpheader">??
??<paramname="error">305</param>
??<paramname="errorMessage">this actionmust be accessed throughaprozy</param>?

</result>?
6、StreamResult(向瀏覽器發送InputSream對象,通常用來處理文件下載)
? <resultname="success"type="stream">??

? <paramname="contentType">image/jpeg</param>?

? <paramname="inputName">imageStream</param>?

? <paramname="contentDisposition">attachment;filename="document.pdf"</param>??

? <paramname="bufferSize">1024</param>?
</result>
7、PlainTextResult(
顯示原始文件內容,例如文件源代碼)
? <actionname="displayJspRawContent">?

? <resulttype="plaintext">/myJspFile.jsp</result>?
?</action>??

<actionname="displayJspRawContent">?

? <resulttype="plaintext">??

????<paramname="location">/myJspFile.jsp</param>?

????<paramname="charSet">UTF-8</param>

?</result>?
</action>?
??若僅設置type="plainText"的話,頁面中顯示中文時會亂碼,這時就可以借助它的charSet屬性以解決中文顯示時的亂碼問題,如果不設置charSet屬性,反而去配置struts.i18n.encoding全局屬性,是不能解決問題的
設置charSet屬性的目的就是讓JSP頁面的編碼與明文顯示時的編碼一致
8、VelocityResult(處理Velocity模板)
? <resultname="success"type="velocity">??
???<paramname="location">foo.vm</param>?
?</result>?

9、XLSResult(處理XML/XLST模板)
? <resultname="success"type="xslt">??

?<paramname="location">foo.xslt</param>?

?<paramname="matchingPattern">^/result/[^/*]$</param>?

?<paramname="excludingPattern">.*(hugeCollection).*</param>?
</result>?
10、FreeMarkerResult?(處理FreeMarker模板)
?
<resultname="success"type="freemarker">foo.ftl</result>

?
附、另外第三方的Result類型還包括JasperReportsPlugin,專門用來處理JasperReport類型的報表輸出。

<%@ tagliburi="http://tiles.apache.org/tags-tiles"prefix="tiles"%>?

<%@ taglib prefix="s"uri="/struts-tags"%>?

? <%-- Show usage; Used in Header--%>?

<tiles:importAttributename="title"scope="request"/>??

<html>?

??<head><title><tiles:getAsStringname="title"/></title></head>??

<body>?

??<tiles:insertAttributename="header"/>??

?????<pid="body">??

???????<tiles:insertAttributename="body"/>??

???</p>?

???<p>Noticethat this is a layout madein JSP</p>

</body>?

</html>



注意!!!!.傳遞中文

記住永遠不要在瀏覽器的地址欄中傳遞中文。在傳遞中文前先進行編碼


A.action中

public class User extendsActionSupport{
?private String username;
?public String getUsername() {
??return username;
?}
?public void setUsername(String username) {
??this.username = username;
?}
?@Override
?public String execute() throws Exception {
??// TODO Auto-generated methodstub
??username=URLEncoder.encode("郭蕾","utf-8");//先進行編碼
??System.out.println(username);
??return "redirect";
?}
}


B.struts.xml中
?<action name="redirect"class="action.User">
?<result type="redirect"name="redirect">
?/redirect.jsp?username=${username}
//如果要傳遞兩個參數,中間用&amp;代替&</result>
?</action>
在這里使用了類似于el表達式的方式傳值,${username}其中username為action中定義的
C.redirect.jsp中

?<body>
?? 重定向
?? <%Strings=request.getParameter("username");
?? ?s=newString(s.getBytes("iso8859-1"),"utf-8");
???s=URLDecoder.decode(s,"utf-8");
???out.println(s);
???%>
? </body>


重定向中傳遞中文先進行編碼,在jsp頁面中先接受參數,然后對其進行字節分解,然后進行解碼。


總結

以上是生活随笔為你收集整理的Struts2中 Result类型配置详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。