一個result代表了一個可能的輸出。當Action類的方法執(zhí)行完成時,它返回一個字符串類型的結(jié)果碼,框架根據(jù)這個結(jié)果碼選擇對應(yīng)的result,向用戶輸出。
在com.opensymphony.xwork2.Action接口中定義了一組標準的結(jié)果代碼,可供開發(fā)人員使用,當然了只有我們的action繼承ActionSupport這個類才可以使用下面的結(jié)果代碼,如下所示:
public interface Action
{
public static final String SUCCESS = “success”;
public static final String NONE = “none”;
public static final String ERROR = “error”;
public static final String INPUT = “input”;
public static final String LOGIN = “l(fā)ogin”;
}
其中Struts2應(yīng)用在運行過程中若發(fā)現(xiàn)addFieldError()中有信息或者類型轉(zhuǎn)換失敗或著輸入校驗失敗等情況那么它會自動跳轉(zhuǎn)到name為input的<result/>,然后轉(zhuǎn)到INPUT所對應(yīng)的頁面:
若JSP頁面中表單是用普通<form>編寫的,發(fā)生錯誤而返回該頁面時,則原數(shù)據(jù)將消失
若JSP頁面中表單是用<s:form/>編寫的,發(fā)生錯誤而返回該頁面時,則原數(shù)據(jù)仍存在
若沒有提供name值為input的<result/>,那么發(fā)生錯誤時,將直接在瀏覽器中提示404錯誤
除了這些預(yù)定義的結(jié)果碼外,開發(fā)人員也可以定義其它的結(jié)果碼來滿足自身應(yīng)用程序的需要。
Result配置由兩部分組成:一部分是result映射,另一部分是result類型。
一、配置 result映射
在result映射的配置中,在指定實際資源的位置時,可以使用絕對路徑,也可以使用相對路徑。絕對路徑以斜杠(/)開頭,相對于當前的Web應(yīng)用程序的上下文路徑;相對路徑不以斜杠(/)開頭,相對于當前執(zhí)行的action的路徑,也就是namespace指定的路徑。
例如:
<package name="default" extends="struts-default" namespace="/admin">
<action name="login" class="com.ibm.LoginAction">
<result>success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
如果當前Web應(yīng)用程序的上下文路徑是/Shop,那么請求/Shop/admin/login.action,執(zhí)行成功后,轉(zhuǎn)向的頁面路徑為:/Shop/admin/success.jsp;執(zhí)行失敗后,轉(zhuǎn)向的頁面路徑為/Shop/error.jsp
二、result結(jié)果類型詳解
type 所有類型 :(在struts2-core.jar/struts-default.xml中可以找到)
Type 類型值 | 作用說明 | 對應(yīng)類 |
---|---|---|
chain | 用來處理Action 鏈 | com.opensymphony.xwork2.ActionChainResult |
dispatcher(默認值) | 用來轉(zhuǎn)向頁面,通常處理 JSP | org.apache.struts2.dispatcher.ServletDispatcherResult |
redirect | 重定向到一個URL | org.apache.struts2.dispatcher.ServletRedirectResult |
redirectAction | 重定向到一個 Action | org.apache.struts2.dispatcher.ServletActionRedirectResult |
plainText | 顯示源文件內(nèi)容,如文件源碼 | org.apache.struts2.dispatcher.PlainTextResult |
freemarker | 處理 FreeMarker 模板 | org.apache.struts2.views.freemarker.FreemarkerResult |
httpheader | 控制特殊 http 行為的結(jié)果類型 | org.apache.struts2.dispatcher.HttpHeaderResult |
stream | 向瀏覽器發(fā)送 InputSream 對象,通常用來處理文件下載,還可用于返回 AJAX 數(shù)據(jù)。 | org.apache.struts2.dispatcher.StreamResult |
velocity | 處理 Velocity 模板 | org.apache.struts2.dispatcher.VelocityResult |
xslt | 處理 XML/XLST 模板 | org.apache.struts2.views.xslt.XSLTResult |
1、dispatcher結(jié)果類型
Struts2在后臺使用Servlet API的RequestDispatcher來轉(zhuǎn)發(fā)請求,因此在用戶的整個請求/響應(yīng)過程中,目標Servlet/JSP接收到的request/response對象,與最初的Servlet/JSP相同。
Dispatcher結(jié)果類型的實現(xiàn)是org.apache.struts2.dispatcher.ServletDispatcherResult,該類的二個屬性(property):location和parse,這兩個屬性可以通過struts.xml配置文件中的result元素的param子元素來設(shè)置。param元素的name屬性指定結(jié)果類型實現(xiàn)類的屬性名,param元素的內(nèi)容是屬性的值。例如:
<result name=“success” type=“dispatcher”>
<param name=“l(fā)ocation” >/success.jsp</param>
<param name=“parse” >true</param>
</result>
說明:
A、location參數(shù)用于指定action執(zhí)行完畢后要轉(zhuǎn)向的目標資源,parse屬性是一個布爾類型的值,如果為true,則解析location參數(shù)中的OGNL表達式;如果為false,則不解析。parse屬性的默認值就是true.
location參數(shù)是默認的參數(shù),在所有的Result實現(xiàn)類中,都定義了一個字符串類型的DEFAULT_PARAM靜態(tài)常量,專門用于指定默認的參數(shù)名。
DEFAULT_PARAM常量的定義:public static final String
DEFAULT_PARAM=“l(fā)ocation”;
B、在設(shè)置location參數(shù)時,可以在參數(shù)值中使用OGNL表達式。
<action name=“viewNews” class=“com.ibm.ViewNewsAction”
<result name=“success” type=“dispatcher”>
<!--如果參數(shù)是中文:請參看最底部例子-->
<param name=“l(fā)ocation” >/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結(jié)果類型(重定向到一個Url,也可以是Action或一個頁面)
Redirect結(jié)果類型在后臺使用HttpServletResponse的sendRedirect方法將請求重定向到指定的URL,它的實現(xiàn)類是org.apache.struts2.dispatcher.ServletRedirectResult.該類同樣有二個屬性(property):location和parse,在使用redirect結(jié)果類型的場景中,用戶要完成一次與服務(wù)器之間的交互,瀏覽器需要完成兩次請求,因此第一次請求中的數(shù)據(jù)在第二次請求中是不可用的,這意味在目標資源中是不能訪問action實例、action錯誤以及錯誤等。
如果有某些數(shù)據(jù)需要在目標資源中訪問:
i、一種方式是將數(shù)據(jù)保存到Session中
ii、另一種方式是通過請求參數(shù)來傳遞數(shù)據(jù)
示例(1)、
<result name="success" type="redirect">
<param name="location">foo.jsp</param>
<param name="parse">false</param><!--不解析OGNL-->
</result>
示例(2)、
<package name="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">
<-- Passparameters (reportType, width and height),重定向到Url并且傳參 ,如果參數(shù)是中文:請參看最底部例子-->
<!--
The redirect-action url generated will be :
/genReport/generateReport.jsp?reportType=pie&width=100&height=100
-->
<actionname="gatherReportInfo" class="...">
<resultname="showReportResult" type="redirect">
<param name="location">generateReport.jsp</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
</result>
</action>
</package>
3、redirectAction結(jié)果類型(重定向到一個Action)
常用于防止表單重復(fù)提交,比方說在增加完用戶之后要顯示列表
redirectAction結(jié)果類型的實現(xiàn)類是org.apache.struts2.dispatcher.ServletActionRedirectResult,該類是ServletRedirectResult的子類,因此我們也就可以判斷出redirectAction結(jié)果類型和redirect結(jié)果類型的后臺工作原理是一樣的,即都是利用HttpServletResponse的sendRedirect方法將請求重定向到指定的URL。
示例、
<package name="public"extends="struts-default">
<action name="login"class="...">
<!--Redirect to another namespace 重定向到不同命名空間下的 action -->
<result type="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 same namespace,重定向到同一命名空間下的action -->
<action name="dashboard" class="...">
<result>dashboard.jsp</result>
<result name="error"type="redirectAction">error</result>
</action>
<action name="error" class="...">
<result>error.jsp</result>
</action>
</package>
<package name="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">
<-- Passparameters (reportType, width and height),重定向到Action并且傳參,如果參數(shù)是中文:請參看最底部例子 -->
<!--
TheredirectAction url generated will be :
/genReport/generateReport.action?reportType=pie&width=100&height=100
-->
<action name="gatherReportInfo" class="...">
<result name="showReportResult" type="redirectAction">
<param name="actionName">generateReport</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
<param name="empty"></param>
<param name="supressEmptyParameters">true</param>
</result>
</action>
</package>
4、鏈接類型 result:chain(從一個Action轉(zhuǎn)發(fā)到另一個Action)
chain結(jié)果類型有4個屬性,分別是:
actionName (default) - the name of the action that will be chained to
namespace - used to determine which namespace the Action is in that we're chaining. If namespace is null, this defaults to the current namespace
method - used to specify another method on target action to be invoked. If null, this defaults to execute method
skipActions - (optional) the list of comma separated action names for the actions that could be chained to
這個Result使用ActionMapperFactory提供的ActionMapper來重定位瀏覽器的URL來調(diào)用指定的action和(可選的)namespace. 這個Result比ServletRedirectResult要好.因為你不需要把URL編碼成xwork.xml中配置的ActionMapper提供的模式. 這就是說你可以在任意點上改變URL模式而不會影響你的應(yīng)用程序.
redirctAction在轉(zhuǎn)發(fā)時無法攜帶原有的參數(shù),而使用chain類型則直接將參數(shù)等信息傳遞到下一個action
示例、
<package name="public"extends="struts-default">
<!-- Chain creatAccount to login, using the default parameter ,鏈接到同一命名空間下的Action,-->
<action name="createAccount" class="...">
<result type="chain">login</result>
</action>
<action name="login" class="...">
<!--Chain to another namespace -->
<result type="chain">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
</action>
</package>
首先是 刪除商品的控制器所對應(yīng)的xml配置文件如下
<!-- This action 負責(zé)將用戶不需的商品從購物車刪除 -->
<action name="deletefromcart" class="eshop.action.DeletefromCart">
<interceptor-ref name="completeStack"></interceptor-ref>
<result name="success" type="chain">displaycart</result>
<result name="error" >error.html</result>
</action>
刪除成功后,其中用type=chain的攔截器,指定讓其去執(zhí)行displaycart的控制器,
<action name="displaycart" class="eshop.action.DisplayCart">
<interceptor-ref name="completeStack"></interceptor-ref>
<result name="success" type="velocity>ShoppingCart.vm</result>
<result name="error" >error.html</result>
</action>
而這個displaycart的控制器其實就是刷新顯示了當前的購物車了
5、HttpHeader Result:HttpHeader(用來控制特殊的Http行為)
httpheader結(jié)果類型很少使用到,它實際上是返回一個HTTP響應(yīng)的頭信息
示例:
<result name="success"type="httpheader">
<paramname="status">204</param>
<paramname="headers.a">a custom header value</param> <paramname="headers.b">another custom header value</param>
</result>
<result name="proxyRequired"type="httpheader">
<paramname="error">305</param>
<paramname="errorMessage">this action must be accessed through aprozy</param>
</result>
6、Stream Result(向瀏覽器發(fā)送InputSream對象,通常用來處理文件下載)
<result name="success"type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">attachment;filename="document.pdf"</param>
<param name="bufferSize">1024</param>
</result>
7、PlainText Result(顯示原始文件內(nèi)容,例如文件源代碼)
<action name="displayJspRawContent">
<result type="plaintext">/myJspFile.jsp</result>
</action>
<action name="displayJspRawContent">
<result type="plaintext">
<param name="location">/myJspFile.jsp</param>
<param name="charSet">UTF-8</param>
</result>
</action>
若僅設(shè)置type="plainText"的話,頁面中顯示中文時會亂碼,這時就可以借助它的charSet屬性以解決中文顯示時的亂碼問題,如果不設(shè)置charSet屬性,反而去配置struts.i18n.encoding全局屬性,是不能解決問題的
設(shè)置charSet屬性的目的就是讓JSP頁面的編碼與明文顯示時的編碼一致
8、Velocity Result(處理Velocity模板)
<result name="success"type="velocity">
<paramname="location">foo.vm</param>
</result>
9、XLS Result(處理XML/XLST模板)
<result name="success" type="xslt">
<paramname="location">foo.xslt</param>
<paramname="matchingPattern">^/result/[^/*]$</param>
<paramname="excludingPattern">.*(hugeCollection).*</param>
</result>
10、 FreeMarkerResult (處理FreeMarker模板)
<result name="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:importAttribute name="title"scope="request"/>
<html>
<head><title><tiles:getAsStringname="title"/></title></head>
<body>
<tiles:insertAttribute name="header"/>
<pid="body">
<tiles:insertAttributename="body"/>
</p>
<p>Noticethat this is a layout made in JSP</p>
</body>
</html>
注意!!!!.傳遞中文
記住永遠不要在瀏覽器的地址欄中傳遞中文。在傳遞中文前先進行編碼
A.action中
public class User extends ActionSupport{
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 method stub
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}//如果要傳遞兩個參數(shù),中間用&代替& </result>
</action>
在這里使用了類似于el表達式的方式傳值,${username}其中username為action中定義的
C.redirect.jsp中
<body>
重定向
<%String s=request.getParameter("username");
s=new String(s.getBytes("iso8859-1"),"utf-8");
s=URLDecoder.decode(s,"utf-8");
out.println(s);
%>
</body>
重定向中傳遞中文先進行編碼,在jsp頁面中先接受參數(shù),然后對其進行字節(jié)分解,然后進行解碼。