Struts2 Interceptor * after * JSP is displayed - how?

I was wondering if I could capture the result of the action after the result returns and the JSP is visualized. I want to be able to take the whole result (generated HTML) and paste it into memcached so that I can bring it through Nginx without entering the application server. Any ideas?

PS: I know that I can launch the interceptor after the action is completed, but before the result returns and the JSP is processed, but not after rendering the JSP.

+3
source share
5 answers

I have not found a way to do this inside struts2, it is best to create it to create a servlet filter and change its OutputStream.

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

+1

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

: "", framework Action. , , . , . , "".

0

: , ? - , , ?

MemCachedException, , -.

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

intercept() ActionInvocation getResult(), null Action (.. , invocation.invoke() intercept()) Result . , , , , , .

. , .

0

, , , , , , . , , , sitemesh. , Struts2, . struts JSP/freemarker/speed html, . JSP - , , org.apache.struts2.views.freemarker.FreemarkerResult class, html-, template.process(model, writer);. ServletActionContext.getResponse().getWriter();

, html , ,    ServletActionContext.getResponse().getWriter().toString() //This does not work out of box. toString() , ResponseWrapper - html . See- .

html struts 2. , , . , , struts2

public class DecoratorInterceptor implements Interceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
       final ActionContext context = invocation.getInvocationContext ();
       HttpServletResponse responseParent = (HttpServletResponse) 
                               context.get(ServletActionContext.HTTP_RESPONSE);
       CharResponseWrapper wrapper = new CharResponseWrapper(responseParent);

       ServletActionContext.setResponse(wrapper);

       //Actual Action called
       String result =  invocation.invoke();

       String htmlReturned = wrapper.toString();
       //play with htmlReturned ...
       String modifiedhtml = pushintoMemCache(htmlReturned );           

       CharArrayWriter car = new CharArrayWriter();           
       car.write(modifiedhtml );

       PrintWriter out = responseParent.getWriter();
        out.write(car.toString());
        out.flush();
   }

  @Override
    public void destroy() {
    // TODO Auto-generated method stub

    }

  @Override
    public void init() {
    // TODO Auto-generated method stub

    }

}         
0

Source: https://habr.com/ru/post/1715792/


All Articles