使用PhaseListener 来打印JSF组件树 (示例)
作者: cschalk http://jroller.com/page/cschalk?entry=a_jsf_phaselistener_to_print
翻译: icess http://blog.matrix.org.cn/page/icess
你是否想知道在你的页面中到底有什么标签在组件树中.
我也想知道,所以我写了一个简单的 PhaseListener,用她把组件树输出到控制台.
使用的算法是递归打印组件树.
下面是代码:
还有,不要忘了在 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)
作者: 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)