Struts2 的 Django 调试工具栏模板列表功能

发布于 2024-09-17 17:32:23 字数 664 浏览 6 评论 0原文

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

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

Django 调试工具栏 - 模板部分

当我有超过 600 个模板形成复杂的模板 Web 时,此列表非常有用需要将其中一个中的
更改为

好吧,我并不期望 Struts2 有这么好的东西,仅仅一个 LOG.debug(

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-09-24 17:32:23

好吧,我读了一些东西 并创建了以下类:

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}

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文