使用PhaseListener 来打印JSF组件树 (示例)

使用PhaseListener 来打印JSF组件树 (示例)

作者: cschalk http://jroller.com/page/cschalk?entry=a_jsf_phaselistener_to_print

翻译: icess http://blog.matrix.org.cn/page/icess

 你是否想知道在你的页面中到底有什么标签在组件树中.

我也想知道,所以我写了一个简单的 PhaseListener,用她把组件树输出到控制台.

使用的算法是递归打印组件树.

下面是代码:

package componentstuff;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

public class PrintComponentTree implements PhaseListener {
  public PrintComponentTree() {
  }

  public int indent = 0;
  public static final int INDENTSIZE = 2;

  public void beforePhase(PhaseEvent PhaseEvent) {
  }

  public void afterPhase(PhaseEvent PhaseEvent) {
    System.out.println("");
    System.out.println("(Rendering Component Tree)");
    printComponentTree(FacesContext.getCurrentInstance().getViewRoot());
  }

  public PhaseId getPhaseId() {
    return PhaseId.RENDER_RESPONSE;    
  }
  
  public void printComponentTree(UIComponent comp){
    printComponentInfo(comp);
    
    List complist = (ArrayList)comp.getChildren();
    if (complist.size()>0)
      indent++;
    for   (int i = 0; i < complist.size(); i++) {
      UIComponent uicom = (UIComponent) complist.get(i);
      printComponentTree(uicom);
      if (i+1 == complist.size())
        indent--;
    }
    
  }

  public void printComponentInfo(UIComponent comp){
  
   if (comp.getId() == null){
     System.out.println("UIViewRoot" + " " + "(" + comp.getClass().getName() + ")");
   } else {
       printIndent();
       System.out.println("|");
       printIndent();
       System.out.println(comp.getId() + " " + "(" + comp.getClass().getName() + ")");
     }  
  }
 
  public void printIndent(){
    for (int i=0; i<indent; i++ )  
      for (int j=0;j<INDENTSIZE; j++)  
        System.out.print(" ");          
  }  
}

还有,不要忘了在 faces-config中注册listener..

  <lifecycle>
    <phase-listener>componentstuff.PrintComponentTree</phase-listener>
  </lifecycle>

下面是一个示例输出:

06/03/03 17:26:39 (Rendering Component Tree)
06/03/03 17:26:39 UIViewRoot (javax.faces.component.UIViewRoot)
06/03/03 17:26:39   |
06/03/03 17:26:39   form1 (javax.faces.component.html.HtmlForm)
06/03/03 17:26:39     |
06/03/03 17:26:39     panelGrid1 (javax.faces.component.html.HtmlPanelGrid)
06/03/03 17:26:39       |
06/03/03 17:26:39       outputLabel1 (javax.faces.component.html.HtmlOutputLabel)
06/03/03 17:26:39       |
06/03/03 17:26:39       inputText1 (javax.faces.component.html.HtmlInputText)
06/03/03 17:26:39       |
06/03/03 17:26:39       outputLabel2 (javax.faces.component.html.HtmlOutputLabel)
06/03/03 17:26:39       |
06/03/03 17:26:39       inputSecret1 (javax.faces.component.html.HtmlInputSecret)
06/03/03 17:26:39       |
06/03/03 17:26:39       commandButton1 (javax.faces.component.html.HtmlCommandButton)
讨论

Feel free to tweak the code however you like!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值