You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ken McWilliams <ke...@gmail.com> on 2013/09/06 07:46:19 UTC

Understanding Struts2 Internals : Result Configuration

The following is a direct copy of a question at SO which has been repeated
here for completeness but can be found here with nice syntax highlighting :
http://stackoverflow.com/questions/18650377/understanding-struts2-internals-result-configuration


In an effort to understand how struts2 loads its configuration I wanted to
display the path to the JSP which would be rendered. Given the following
very minimal struts.xml:

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />

    <package name="base" namespace="/">
        <result-types>
            <result-type name="dispatcher"
class="org.apache.struts2.dispatcher.ServletDispatcherResult"
default="true"/>
        </result-types>
        <action name="test" class="com.kenmcwilliams.badwebapp.action.Test">
            <result>/WEB-INF/content/test.jsp</result>
        </action>
    </package>
</struts>

I want to be able to log "/WEB-INF/content/test.jsp" from within the
action. Given the following action:

package com.quaternion.badwebapp.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test extends ActionSupport {
    //used for a sanity test on JSP
    public String getMessage() {
        return "From test";
    }

    @Override
    public String execute() throws Exception {
        System.out.println("ActionContext.getContext().getActionInvocation().getResultCode():
" + ActionContext.getContext().getActionInvocation().getResultCode());
        ActionInvocation ai = ActionContext.getContext().getActionInvocation();
        ai.addPreResultListener(new PreResultListener() {
            @Override
            public void beforeResult(ActionInvocation invocation,
String resultCode) {
                try {
                    System.out.println("PreResultListener resultCode:
" + resultCode);
                    System.out.println("PreResultListener result: " +
invocation.getResult());
                } catch (Exception ex) {

Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        return SUCCESS;
    }
}

There are three print statements which produce the following output on my
console:

INFO:   ActionContext.getContext().getActionInvocation().getResultCode(): null
INFO:   PreResultListener resultCode: success
INFO:   PreResultListener result: null

>From testing both the result "invocation.getResult()" and the resultcode is
null *before* the PreResultListener is called but *within* the
PreResultListener the resultcode is set, yet the result still returns null!

>From the JavaDoc of the getResult() method:

If the ActionInvocation has been executed before and the Result is an
instance of {@link ActionChainResult}, this method will walk down the chain
of ActionChainResult's until it finds a non-chain result, which will be
returned. *If the ActionInvocation's result has not been executed before,
the Result instance will be created and populated with the result params.*

Seems pretty clear that a result instance is *not* being created.

So how do I display "/WEB-INF/content/test.jsp" within this action? This is
not for typical struts2 use, I'm want to test a configuration provider for
which there is something wrong with the construction of the result for the
action, hopefully understanding why this isn't working will let me fix that
too.

Re: Understanding Struts2 Internals : Result Configuration

Posted by Lukasz Lenart <lu...@apache.org>.
Hi,

Just to clarify my comment on SO - action can directly return instance
of Result instead of simple String. Then ActionInvocation.getResult
will contain that result then. So actions are not limited just to
Strings but can be used to dynamically build response.


Regards
-- 
Ɓukasz
+ 48 606 323 122 http://www.lenart.org.pl/

2013/9/6 Ken McWilliams <ke...@gmail.com>:
> The following is a direct copy of a question at SO which has been repeated
> here for completeness but can be found here with nice syntax highlighting :
> http://stackoverflow.com/questions/18650377/understanding-struts2-internals-result-configuration
>
>
> In an effort to understand how struts2 loads its configuration I wanted to
> display the path to the JSP which would be rendered. Given the following
> very minimal struts.xml:
>
> <struts>
>     <constant name="struts.devMode" value="true" />
>     <constant name="struts.ui.theme" value="simple" />
>
>     <package name="base" namespace="/">
>         <result-types>
>             <result-type name="dispatcher"
> class="org.apache.struts2.dispatcher.ServletDispatcherResult"
> default="true"/>
>         </result-types>
>         <action name="test" class="com.kenmcwilliams.badwebapp.action.Test">
>             <result>/WEB-INF/content/test.jsp</result>
>         </action>
>     </package>
> </struts>
>
> I want to be able to log "/WEB-INF/content/test.jsp" from within the
> action. Given the following action:
>
> package com.quaternion.badwebapp.action;
>
> import com.opensymphony.xwork2.ActionContext;
> import com.opensymphony.xwork2.ActionInvocation;
> import com.opensymphony.xwork2.ActionSupport;
> import com.opensymphony.xwork2.interceptor.PreResultListener;
> import java.util.logging.Level;
> import java.util.logging.Logger;
>
> public class Test extends ActionSupport {
>     //used for a sanity test on JSP
>     public String getMessage() {
>         return "From test";
>     }
>
>     @Override
>     public String execute() throws Exception {
>         System.out.println("ActionContext.getContext().getActionInvocation().getResultCode():
> " + ActionContext.getContext().getActionInvocation().getResultCode());
>         ActionInvocation ai = ActionContext.getContext().getActionInvocation();
>         ai.addPreResultListener(new PreResultListener() {
>             @Override
>             public void beforeResult(ActionInvocation invocation,
> String resultCode) {
>                 try {
>                     System.out.println("PreResultListener resultCode:
> " + resultCode);
>                     System.out.println("PreResultListener result: " +
> invocation.getResult());
>                 } catch (Exception ex) {
>
> Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
>                 }
>             }
>         });
>         return SUCCESS;
>     }
> }
>
> There are three print statements which produce the following output on my
> console:
>
> INFO:   ActionContext.getContext().getActionInvocation().getResultCode(): null
> INFO:   PreResultListener resultCode: success
> INFO:   PreResultListener result: null
>
> From testing both the result "invocation.getResult()" and the resultcode is
> null *before* the PreResultListener is called but *within* the
> PreResultListener the resultcode is set, yet the result still returns null!
>
> From the JavaDoc of the getResult() method:
>
> If the ActionInvocation has been executed before and the Result is an
> instance of {@link ActionChainResult}, this method will walk down the chain
> of ActionChainResult's until it finds a non-chain result, which will be
> returned. *If the ActionInvocation's result has not been executed before,
> the Result instance will be created and populated with the result params.*
>
> Seems pretty clear that a result instance is *not* being created.
>
> So how do I display "/WEB-INF/content/test.jsp" within this action? This is
> not for typical struts2 use, I'm want to test a configuration provider for
> which there is something wrong with the construction of the result for the
> action, hopefully understanding why this isn't working will let me fix that
> too.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org