2010-09-03 71 views
0

我正在開發一個使用Sitemesh作爲模板引擎的Struts2應用程序。我需要的是請求使用的所有模板(JSP)的列表。Struts2的Django調試工具欄模板列表功能

在其他項目中,我使用Django Framework,我有這個驚人的Debug Toolbar,除了許多其他有用的信息,爲我提供了用於顯示頁面的請求的模板列表。

Django Debug Toolbar - Template Section

這份名單是令人驚訝的有幫助的,當具有形成複雜的網頁模板600個多模板,我需要更改<br /><p></p>在其中的一個。

我不希望Struts2有這麼好的效果,只是LOG.debug(<template>);的原始列表會讓我的工作變得更加簡單。

回答

0

好吧,我讀了一些stuff,並提出以下類:

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} 

我將更新這篇文章,如果我找到一些額外的有用輸出。