JSF調試利器—phase listener

來源:互聯網
上載者:User
import java.util.ArrayList;
import java.util.List;

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

public class MyPhaseListener implements PhaseListener ...{        
    public int indent = 0;  
    public static final int INDENTSIZE = 2;
    
    public void beforePhase(PhaseEvent pe)...{
        if (pe.getPhaseId() == PhaseId.RESTORE_VIEW)...{
            System.out.println("Processing new  Request!"); 
        }
        System.out.println("before - " + pe.getPhaseId().toString());
    }
    
    public void afterPhase(PhaseEvent pe)...{   
        System.out.println("after - " + pe.getPhaseId().toString());    
        if (pe.getPhaseId() == PhaseId.RENDER_RESPONSE)...{
            System.out.println("Done with Request! ");  
        }
        
        if(pe.getPhaseId() == PhaseId.PROCESS_VALIDATIONS) ...{
//            try{
//                FacesContextImpl context = (FacesContextImpl)pe.getFacesContext();
//                UIViewRoot viewRoot = context.getViewRoot();
//                HtmlGridView saGrid = (HtmlGridView)viewRoot.findComponent("salesAssistant:saGridView");
//                HtmlInputText saNo = (HtmlInputText)viewRoot.findComponent("salesAssistant:SaNo");
//                
//            } catch(Exception ex){
//                ex.printStackTrace();
//            }
            
        }
        
        
    //    printComponentTree(FacesContext.getCurrentInstance().getViewRoot());
    }  
    
    public PhaseId getPhaseId()...{      
        return PhaseId.ANY_PHASE;  
    }
    
    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.xml, 配置phase listener.

<lifecycle>
    <phase-listener>com.web.listener.MyPhaseListener </phase-listener>
</lifecycle>

     

 

這樣就可以通過該監聽器監聽jsf的每個階段,方便調試。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.