日期:2014-05-16 浏览次数:20310 次
public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; }由于每次阶段变化都执行它,所以返回ANY_PHASE。在beforePhase中只接受生命周期的第六个阶段所以使用PhaseId.RENDER_RESPONSE,而在在afterPhase中只接受生命周期的第一个阶段所以使用PhaseId.RESTORE_VIEW
package phaselistener; import java.io.BufferedReader; import java.util.Map; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import javax.faces.event.PhaseListener; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import javax.faces.FactoryFinder; import javax.faces.context.ResponseWriter; import javax.faces.el.EvaluationException; import javax.faces.render.RenderKit; import javax.faces.render.RenderKitFactory; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import util.JSONGenerator; /** * * @author love */ public class DataProviderPhListener implements PhaseListener { public static final String RENDER_SCRIPT_VIEW_ID = "SpellCheckTextArea.ajaxScript"; private static final String SCRIPT_RESOURCE_NAME = "/com/jsfcompref/components/component/gridajax.js"; public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; } /** * <p>Go through each component in the AJAX_VALIDATOR_COMPONENTS * list in request scope. */ public void afterPhase(PhaseEvent event) { // If this is restoreView phase and the viewId is the script view id if(event.getPhaseId() == PhaseId.RESTORE_VIEW){ if (-1 != event.getFacesContext().getViewRoot().getViewId().indexOf(RENDER_SCRIPT_VIEW_ID) && PhaseId.RESTORE_VIEW == event.getPhaseId()) { // render the script writeScript(event); event.getFacesContext().responseComplete(); } } } public void beforePhase(PhaseEvent event) { if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) { FacesContext context = event.getFacesContext(); Map requestMap = context.getExternalContext().getRequestParameterMap(); if (requestMap.containsKey("ajaxreq")) { HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest(); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); try { ResponseWriter writer = FacesContext.getCurrentInstance().getResponseWriter(); if (null == writer) { RenderKitFactory rkf = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); RenderKit renderKit =