六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 129|回复: 0

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

[复制链接]

升级  44%

30

主题

30

主题

30

主题

秀才

Rank: 2

积分
116
 楼主| 发表于 2013-2-7 21:13:46 | 显示全部楼层 |阅读模式
使用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)
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表