views:

522

answers:

4

I was wondering if I can capture the result of an action after the result returns and the JSP is rendered. I want to be able to take the entire result (generated HTML) and push it into memcached so I can bring it via Nginx with-out hitting the application server. Any ideas?

PS: I know I can run the interceptor after the action executes but before the result returns and the JSP is rendered, but not after the JSP is rendered.

A: 

Read this article - http://struts.apache.org/2.0.6/docs/interceptors.html

SUMMARY:When you request a resource that maps to an "action", the framework invokes the Action object. But, before the Action is executed, the invocation can be intercepted by another object. After the Action executes, the invocation could be intercepted again. Unsurprisingly, we call these objects "Interceptors."

adatapost
What you've pointed at lets me intercept(), ie, before its executed, or add a PreResultListener, which, according to the docs, says:"If you need to work with the final Result object before it is executed..."which means the JSP has not been rendered. I need to have access to the entire *generated* HTML after the action's result picks up a JSP and renders it, not before.Consider this:Client -> Request -> Dispatch -> Action -> Result -> JSP render -> HTML -> ClientI want:Client -> Request -> Dispatch -> Action -> Result -> JSP render -> HTML -> ***MY CODE*** -> Client
Hisham
A: 

I haven't found a way to do this inside of struts2, your best bet it to create a servlet Filter and have it modify the OutputStream.

http://onjava.com/pub/a/onjava/2003/11/19/filters.html

Andreas Schobel
A: 

Question: How do you determine if the view has been generated? Do you set a request header or an some sort of a flag to determine if the view has been generated?

You could try throwing a MemCachedException to indicate that it is time to load into a mem cache. Your interceptor code could read

try {
   return invocation.invoke();
} catch (MemCachedException mce) {
   // Your code to upload to MemCache.
} finally {
  // blah blah clean up.
}
Kartik
A: 

Within your interceptor's intercept() method, the ActionInvocation parameter has a getResult() method which returns null before Action execution (i. e. before you call invocation.invoke() in your intercept() method) and contains an implementation of Result afterwards. That object should give you some way to access the data you need, but how this is done probably depends on the class that is actually used.

See also my somewhat related question and the answer I posted after figuring it out.

Hanno Fietz