views:

29

answers:

1

I'm developing a Struts2 application that uses Sitemesh as template engine. What I need is a list of all the templates (JSP) that are used by request.

In other projects I use Django Framework, with it I've this amazing Debug Toolbar that, besides many other useful info, provides me with the list of templates the request used for displaying the page.

Django Debug Toolbar - Template Section

This list is surprisingly helpful when in have more than 600 templates that forms a intricate template web and I need to change a <br /> to a <p></p> in one of them.

Well I don't expect anything as nice as this for Struts2, just a raw list of LOG.debug(<template>); will make my work so much easier.

A: 

Ok, I read some stuff and made the following class:

package x;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class TemplatesDebugInterceptor extends AbstractInterceptor {

    private static final long serialVersionUID = 4030044344066761593L;
    Log log = LogFactory.getLog(TemplatesDebugInterceptor.class);

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        try {
            if (ServletActionContext.getActionMapping() != null) {
                String className = invocation.getAction().getClass().getCanonicalName();
                String methodName = ServletActionContext.getActionMapping().getMethod();
                log.info("===========================");
                log.info(className+"."+methodName);
            }
            invocation.addPreResultListener(new PreResultListener() {
                public void beforeResult(ActionInvocation invocation,String resultCode) {
                    Map<String, ResultConfig> resultsMap = invocation.getProxy().getConfig().getResults();
                    ResultConfig finalResultConfig = resultsMap.get(resultCode);
                    log.info(finalResultConfig.getParams());
                }
            });
        } catch (Exception e) {
            log.error("[ERROR] Could not list templates: ", e);
        }
        return invocation.invoke();
    }
}

Added this to struts.xml:

<interceptors>
    <interceptor name="templates" class="x.TemplatesDebugInterceptor" />
    (...)
    <interceptor-stack name="defaultStackBizgov">
        <interceptor-ref name="templates"/>
        (...)

And it's done!:

13:52:00,279 INFO  [STDOUT] [INFO] (http-0.0.0.0-8080-7) TemplatesDebugInterceptor - x.ProcedureDetailsAction.validateSubmitPublication
13:52:00,357 INFO  [STDOUT] [INFO] (http-0.0.0.0-8080-7) TemplatesDebugInterceptor - {location=/WEB-INF/jsp/indexPage.jsp}
13:52:00,763 INFO  [STDOUT] [INFO] (http-0.0.0.0-8080-7) TemplatesDebugInterceptor - {location=/WEB-INF/jsp/publicationView.jsp}

I will update this post if I find out some extra useful output.

rsilva