0

我正在开发一个使用Sitemesh作为模板引擎的Struts2应用程序。我需要的是请求使用的所有模板(JSP)的列表。

在我使用Django Framework的其他项目中,我有这个惊人的调试工具栏,除了许多其他有用的信息外,它还为我提供了用于显示页面的请求的模板列表。

Django 调试工具栏 - 模板部分

当有 600 多个模板构成复杂的模板 Web 并且我需要将其中一个中的 a 更改<br />为 a时,此列表非常有用<p></p>

好吧,我不希望 Struts2 有这么好的东西,只是一个原始列表LOG.debug(<template>);将使我的工作变得容易得多。

4

1 回答 1

0

好的,我读了一些东西并做了以下课程:

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();
    }
}

将此添加到 struts.xml:

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

它已经完成了!:

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}

如果我发现一些额外有用的输出,我会更新这篇文章。

于 2010-09-07T13:01:28.267 回答