struct2的xml文件中result的配置(轉)


 一個result代表了一個可能的輸出。當Action類的方法執行完成時,它返回一個字符串類型的結果碼,框架根據這個結果碼選擇對應的result,向用戶輸出。
com.opensymphony.xwork2.Action接口中定義了一組標准的結果代碼,可供開發人員使用,當然了只有我們的action繼承ActionSupport 這個類才可以使用下面的結果代碼,如下所示:
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 = “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>
            <result name="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元素的內容是屬性的值。例如:
  <result name=“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 String DEFAULT_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">  
             <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並且傳參 ,如果參數是中文:請參看最底部例子-->  

   <!--  
     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結果類型(重定向到一個Action)
他經常用於防止表單重復提交,比方說在增加完用戶之后要顯示列表 
 redirectAction結果類型的實現類是org.apache.struts2.dispatcher.ServletActionRedirectResult,該類是ServletRedirectResult的子類,因此我們也就可以判斷出redirectAction結果類型和redirect結果類型的后台工作原理是一樣的,即都是利用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並且傳參,如果參數是中文:請參看最底部例子 --> 

   <!--  
   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轉發到另一個Action)
  chain結果類型有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


示例、
 <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> 

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

        <!--Chain to another namespace --> 

        <result type="chain">  

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

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

       </result> 

   </action> 

</package> 

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

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

       <result>dashboard.jsp</result> 

   </action> 

</package>
5、HttpHeader Result:HttpHeader(用來控制特殊的Http行為)
  httpheader結果類型很少使用到,它實際上是返回一個HTTP響應的頭信息
 示例:<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(向瀏覽器發送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(顯示原始文件內容,例如文件源代碼)
  <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> 
   若僅設置type="plainText"的話,頁面中顯示中文時會亂碼,這時就可以借助它的charSet屬性以解決中文顯示時的亂碼問題,如果不設置charSet屬性,反而去配置struts.i18n.encoding全局屬性,是不能解決問題的
設置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}//如果要傳遞兩個參數,中間用&amp;代替&</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頁面中先接受參數,然后對其進行字節分解,然后進行解碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM