KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > WebWorkResultSupport


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import com.opensymphony.webwork.WebWorkStatics;
8 import com.opensymphony.xwork.ActionContext;
9 import com.opensymphony.xwork.ActionInvocation;
10 import com.opensymphony.xwork.Result;
11 import com.opensymphony.xwork.util.OgnlValueStack;
12 import com.opensymphony.xwork.util.TextParseUtil;
13
14
15 /**
16  * A base class for all WebWork action execution results.
17  * The "location" param is the default parameter, meaning the most common usage of this result would be:
18  * <p/>
19  * This class provides two common parameters for any subclass:
20  * <ul>
21  * <li>location - the location to go to after execution (could be a jsp page or another action).
22  * It can be parsed as per the rules definied in the
23  * {@link TextParseUtil#translateVariables(java.lang.String, com.opensymphony.xwork.util.OgnlValueStack) translateVariables}
24  * method</li>
25  * <li>parse - true by default. If set to false, the location param will not be parsed for expressions</li>
26  * </ul>
27  * <p/>
28  * In the xwork.xml configuration file, these would be included as:
29  * <p/>
30  * <pre>
31  * &lt;result name="success" type="redirect"&gt;
32  * &lt;param name="<b>location</b>"&gt;foo.jsp&lt;/param&gt;
33  * &lt;/result&gt;</pre>
34  * <p/>
35  * or
36  * <p/>
37  * <pre>
38  * &lt;result name="success" type="redirect" &gt;
39  * &lt;param name="<b>location</b>"&gt;foo.jsp&lt;/param&gt;
40  * &lt;param name="<b>parse</b>"&gt;false&lt;/param&gt;
41  * &lt;/result&gt;</pre>
42  * <p/>
43  * or when using the default parameter feature
44  * <p/>
45  * <pre>
46  * &lt;result name="success" type="redirect"&gt;<b>foo.jsp</b>&lt;/result&gt;</pre>
47  * <p/>
48  * You should subclass this class if you're interested in adding more parameters or functionality
49  * to your Result. If you do subclass this class you will need to
50  * override {@link #doExecute(String, ActionInvocation)}.<p>
51  * <p/>
52  * Any custom result can be defined in xwork.xml as:
53  * <p/>
54  * <pre>
55  * &lt;result-types&gt;
56  * ...
57  * &lt;result-type name="myresult" class="com.foo.MyResult" /&gt;
58  * &lt;/result-types&gt;</pre>
59  * <p/>
60  * Please see the {@link com.opensymphony.xwork.Result} class for more info on Results in general.
61  *
62  * @author Jason Carreira
63  * @author Bill Lynch (docs)
64  * @see com.opensymphony.xwork.Result
65  */

66 public abstract class WebWorkResultSupport implements Result, WebWorkStatics {
67     //~ Static fields/initializers /////////////////////////////////////////////
68

69     public static final String JavaDoc DEFAULT_PARAM = "location";
70
71     //~ Instance fields ////////////////////////////////////////////////////////
72

73     protected boolean parse = true;
74     private String JavaDoc location;
75
76     //~ Methods ////////////////////////////////////////////////////////////////
77

78     /**
79      * The location to go to after action execution. This could be a JSP page or another action.
80      * The location can contain OGNL expressions which will be evaulated if the <tt>parse</tt>
81      * parameter is set to <tt>true</tt>.
82      *
83      * @param location the location to go to after action execution.
84      * @see #setParse(boolean)
85      */

86     public void setLocation(String JavaDoc location) {
87         this.location = location;
88     }
89
90     /**
91      * Set parse to <tt>true</tt> to indicate that the location should be parsed as an OGNL expression. This
92      * is set to <tt>true</tt> by default.
93      *
94      * @param parse <tt>true</tt> if the location parameter is an OGNL expression, <tt>false</tt> otherwise.
95      */

96     public void setParse(boolean parse) {
97         this.parse = parse;
98     }
99
100     /**
101      * Implementation of the <tt>execute</tt> method from the <tt>Result</tt> interface. This will call
102      * the abstract method {@link #doExecute(String, ActionInvocation)} after optionally evaluating the
103      * location as an OGNL evaluation.
104      *
105      * @param invocation the execution state of the action.
106      * @throws Exception if an error occurs while executing the result.
107      */

108     public void execute(ActionInvocation invocation) throws Exception JavaDoc {
109         doExecute(conditionalParse(location, invocation), invocation);
110     }
111
112     protected String JavaDoc conditionalParse(String JavaDoc param, ActionInvocation invocation) {
113         if (parse && param != null && invocation != null) {
114             return TextParseUtil.translateVariables(param, invocation.getStack());
115         } else {
116             return param;
117         }
118     }
119
120     /**
121      * Executes the result given a final location (jsp page, action, etc) and the action invocation
122      * (the state in which the action was executed). Subclasses must implement this class to handle
123      * custom logic for result handling.
124      *
125      * @param finalLocation the location (jsp page, action, etc) to go to.
126      * @param invocation the execution state of the action.
127      * @throws Exception if an error occurs while executing the result.
128      */

129     protected abstract void doExecute(String JavaDoc finalLocation, ActionInvocation invocation) throws Exception JavaDoc;
130 }
131
Popular Tags