views:

26

answers:

0

Hi!

Recently I needed to implement an interceptor that got the result of executing an action, and if that result was INPUT then execute a method that fetched some lists from the db. I just need to execute that method if the result is INPUT, because it's the only time when I'll need to populate some lists on the jsp. So I wanted to avoid the prepare interceptor because it executes regardless of the method's result.

So I implemented the following interceptor:

public String intercept(ActionInvocation invocation) throws Exception { 
    String resultat = invocation.invoke();
    if(resultat.equals(com.opensymphony.xwork2.ActionSupport.INPUT)){  
        Object action = invocation.getAction ();  
        if (action instanceof InputAware) {  
            InputAware inputAware = (InputAware) action;  
            inputAware.prepareInput();  
        }  
    } 
} 

This just invokes the action and if the resulut is INPUT, executes the method. I've made an interface (InputAware) that just makes sure the action implements the prepareInput() method.

This interceptor works as expected, so everything should be fine. But it isn't, because when I get to the jsp and try to access the lists I just fetched, they're not there. So I guess it has something to do with the fact that the lists are not on the valueStack. But I don't know why.

After struggling with this, I found out about the beforeResult interceptor, which basesically thas the same as mine:

public String intercept(ActionInvocation invocation) throws Exception {  
    invocation.addPreResultListener(new PreResultListener() {  
        public void beforeResult(ActionInvocation invocation, String resultCode) {  
            if(resultCode.equals(com.opensymphony.xwork2.ActionSupport.INPUT)){  
                Object action = invocation.getAction ();  
                ((InputAware) action).prepareInput();  
            }  
        }  
    });  
    return invocation.invoke();  
}

The difference is that when I use this interceptor everything just works fine (the lists can be accessed from the jsp). So I would like to know why one puts the values on the valueStack and the other doesn't. At first sight they are doing exactly the same. But one is working and the other is not, so there must be a reason and I would love to know it.

Thanks for your help!

PD: I also posted this here, but there's no answers there, and I haven't got any there for a while...