• Struts2.3.16日志(中)


    Result Configuration --Result 配置

      当一个操作类方法完成后,它将返回一个字符串。字符串的值是用来选择一个元素的结果。一个操作映射的结果往往会有一组代表不同的可能的结果。一组标准的结果令牌由ActionSupport定义基类。

    1 String SUCCESS = "success";
    2 String NONE    = "none";
    3 String ERROR   = "error";
    4 String INPUT   = "input";
    5 String LOGIN   = "login";
     1 <!--Setting a default Result Type-->
     2 
     3 <result-types>
     4    <result-type name="dispatcher" default="true"
     5                 class="org.apache.struts2.dispatcher.ServletDispatcherResult" />
     6 </result-types>
     7 
     8 
     9 
    10 <!--
    11 
    12 If a type attribute is not specified, the framework will use the default dispatcher type, which forwards to another web resource. If the resource is a JavaServer Page, then the container will render it, using its JSP engine.
    13 
    14 Likewise if the name attribute is not specified, the framework will give it the name "success".
    15
    16 Using these intelligent defaults, the most often used result types also become the simplest.
    17 -->
    1 <!--Result element without defaults-->
    2 
    3 <result name="success" type="dispatcher">
    4     <param name="location">/ThankYou.jsp</param>
    5 </result>
    <!--A Result element using some defaults-->
    
    <result>
        <param name="location">/ThankYou.jsp</param>
    </result>
    1 <!--Result element using more defaults-->
    2 
    3 
    4 <result>/ThankYou.jsp</result>
    1 <!--Multiple Results-->
    2 
    3 <action name="Hello">
    4     <result>/hello/Result.jsp</result>
    5     <result name="error">/hello/Error.jsp</result>
    6     <result name="input">/hello/Input.jsp</result>
    7 </action>
    1 <!--'*' Other Result-->
    2 
    3 <action name="Hello">
    4     <result>/hello/Result.jsp</result>
    5     <result name="error">/hello/Error.jsp</result>
    6     <result name="input">/hello/Input.jsp</result>
    7     <result name="*">/hello/Other.jsp</result>
    8 </action>

    <!-- 名称= " * "不是一个通配符模式,它仅是一个特别的名字,如果没有找到完全匹配选择。 -->

    Global Results

      Most often, results are nested with the action element. But some results apply to multiple actions. In a secure application, a client might try to access a page without being authorized, and many actions may need access to a "logon" result.

      If actions need to share results, a set of global results can be defined for each package. The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.

    1 <!--Defining global results-->
    2 
    3 <global-results>
    4     <result name="error">/Error.jsp</result>
    5     <result name="invalid.token">/Error.jsp</result>
    6     <result name="login" type="redirectAction">Logon!input</result>
    7 </global-results>

    Dynamic Results

      A result may not be known until execution time. Consider the implementation of a state-machine-based execution flow; the next state might depend on any combination of form input elements, session attributes, user roles, moon phase, etc. In other words, determining the next action, input page, etc. may not be known at configuration time.

      Result values may be retrieved from its corresponding Action implementation by using EL expressions that access the Action's properties, just like the Struts 2 tag libraries. So given the following Action fragment:

    1 //FragmentAction implementation
    2 
    3 private String nextAction;
    4  
    5 public String getNextAction() {
    6     return nextAction;
    7 }

    you might define a result like this:

    1 <!--FragmentAction configuration-->
    2 
    3 <action name="fragment" class="FragmentAction">
    4     <result name="next" type="redirectAction">${nextAction}</result>
    5 </action>

    <!--

    If a FragmentAction method returns "next" the actual value of that result will be whatever is in FragmentAction's nextAction property. So nextAction may be computed based on whatever state information necessary then passed at runtime to "next"'s redirectAction.

    -->

    ==============================================================

    Result Types

    Chain Result

    Used for Action Chaining

    Dispatcher Result

    Used for web resource integration, including JSP integration

    FreeMarker Result

    Used for FreeMarker integration

    HttpHeader Result

    Used to control special HTTP behaviors

    Redirect Result

    Used to redirect to another URL (web resource)

    Redirect Action Result

    Used to redirect to another action mapping

    Stream Result

    Used to stream an InputStream back to the browser (usually for file downloads)

    Velocity Result

    Used for Velocity integration

    XSL Result

    Used for XML/XSLT integration

    PlainText Result

    Used to display the raw content of a particular page (i.e jsp, HTML)

    Tiles 2 Result

    Used to provide Tiles 2 integration

    Tiles 3 Result

    Used to provide Tiles 3 integration

    Postback Result

    Used to postback request parameters as a form to the specified destination

    Parameters in configuration results

     1 <!--Parameters in action result definitions-->
     2 
     3 <struts>
     4 ...
     5    <package name="somePackage" namespace="/myNamespace" extends="struts-default">
     6       <action name="myAction" class="com.project.MyAction">
     7          <result name="success" type="redirectAction">otherAction?id=${id}</result>
     8          <result name="back" type="redirect">${redirectURL}</result>
     9       </action>
    10  
    11       <action name="otherAction" class="com.project.MyOtherAction">
    12          ...
    13       </action>      
    14    </package>
    15 ...
    16 </struts>

      The only requirement is to declare the necessary properties in your action, in this case com.project.MyAction should define properties id and redirectURL with standard accessor methods.

     1 public class MyAction extends ActionSupport {
     2    private int id;
     3    private String redirectURL;
     4    ...
     5  
     6  
     7    public String execute() {
     8        ...
     9       if (someCondition) {
    10          this.redirectURL = "/the/target/page.action";
    11          return "back";
    12       }
    13  
    14       this.id = 123;
    15       return SUCCESS; 
    16    }
    17  
    18    public int getId() { return this.id; }
    19    public void setId(int id) { this.id = id; }
    20    public String getRedirectURL() { return this.redirectURL; }
    21    public void setRedirectURL(String redirectURL) { this.redirectURL= redirectURL; }
    22    ...
    23 }
    24 
    25 /**
    26 In the above code if it returns SUCCESS then the browser will be forwarded to
    27 /<app-prefix>/myNamespace/otherAction.action?id=123
    28 **/
    走在一起是缘分,在一起走是幸福; 在一起走是缘分,走在一起是幸福
  • 相关阅读:
    利用docker搭建rtmp服务器(1)
    ES6转换为ES5
    一些乱七八糟的东西
    xss攻击和sq注入
    python asyncio笔记
    图解密码技术一些笔记
    做网页前端遇到的一些问题
    错误处理的一些想法
    吐槽下国内的云笔记
    python的编码问题
  • 原文地址:https://www.cnblogs.com/tarena/p/3642009.html
Copyright © 2020-2023  润新知