public void testComponentAnnotatations() throws Exception { getFacesContext() .getAttributes() .put("javax.faces.IS_POSTBACK_AND_RESTORE_VIEW", Boolean.FALSE); Application application = getFacesContext().getApplication(); application.addComponent("CustomInput", CustomOutput.class.getName()); CustomOutput c = (CustomOutput) application.createComponent("CustomInput"); CustomOutput c2 = (CustomOutput) application.createComponent("CustomInput"); UIViewRoot root = getFacesContext().getViewRoot(); root.getChildren().add(c); root.getChildren().add(c2); assertTrue(c.getEvent() instanceof AfterAddToParentEvent); assertTrue(c2.getEvent() instanceof AfterAddToParentEvent); List<UIComponent> headComponents = root.getComponentResources(getFacesContext(), "head"); System.out.println(headComponents.toString()); assertTrue(headComponents.size() == 1); assertTrue(headComponents.get(0) instanceof UIOutput); assertTrue("test".equals(headComponents.get(0).getAttributes().get("library"))); List<UIComponent> bodyComponents = root.getComponentResources(getFacesContext(), "body"); assertTrue(bodyComponents.size() == 1); assertTrue(bodyComponents.get(0) instanceof UIOutput); assertTrue("test.js".equals(bodyComponents.get(0).getAttributes().get("name"))); assertTrue("body".equals(bodyComponents.get(0).getAttributes().get("target"))); application.addComponent("CustomInput2", CustomOutput2.class.getName()); CustomOutput2 c3 = (CustomOutput2) application.createComponent("CustomInput2"); root.getChildren().add(c3); assertTrue(c3.getEvent() instanceof AfterAddToParentEvent); c3.reset(); c3.encodeAll(getFacesContext()); assertTrue(c3.getEvent() instanceof BeforeRenderEvent); }
@Override public void encodeEnd(FacesContext context, UIComponent component) throws IOException { ResponseWriter writer = context.getResponseWriter(); UIViewRoot viewRoot = context.getViewRoot(); ListIterator iter = (viewRoot.getComponentResources(context, "body")).listIterator(); while (iter.hasNext()) { UIComponent resource = (UIComponent) iter.next(); resource.encodeAll(context); } RenderKitUtils.renderUnhandledMessages(context); writer.endElement("body"); }
@Override public void encodeBegin(FacesContext context, UIComponent component) throws IOException { ResponseWriter writer = context.getResponseWriter(); ConfigContainer cc = RequestContext.getCurrentInstance().getApplicationContext().getConfig(); ProjectStage projectStage = context.getApplication().getProjectStage(); writer.startElement("head", component); // First facet UIComponent first = component.getFacet("first"); if (first != null) { first.encodeAll(context); } writer.write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>"); String theme = resolveTheme(context); if (theme == null) { renderCSS(context, "mobile/jquery-mobile.css", "primefaces"); } else { renderCSS(context, "theme.css", "primefaces-" + theme); renderCSS(context, "mobile/jquery-mobile-icons.css", "primefaces"); renderCSS(context, "mobile/jquery-mobile-structure.css", "primefaces"); } renderCSS(context, "mobile/primefaces-mobile.css", "primefaces"); if (cc.isFontAwesomeEnabled()) { renderCSS(context, "fa/font-awesome.css", "primefaces"); } renderJS(context, "jquery/jquery.js", "primefaces"); writer.startElement("script", null); writer.writeAttribute("type", "text/javascript", null); writer.write("$(document).on('mobileinit', function(){"); writer.write("$.mobile.ajaxEnabled = false;"); writer.write("$.mobile.pushStateEnabled = false;"); writer.write("$.mobile.page.prototype.options.domCache = true;"); UIComponent init = component.getFacet("init"); if (init != null) { init.encodeAll(context); } writer.write("});"); writer.endElement("script"); renderJS(context, "mobile/jquery-mobile.js", "primefaces"); renderJS(context, "primefaces-mobile.js", "primefaces"); // Registered Resources UIViewRoot viewRoot = context.getViewRoot(); for (UIComponent resource : viewRoot.getComponentResources(context, "head")) { boolean shouldRender = true; Map<String, Object> attrs = resource.getAttributes(); String library = (String) attrs.get("library"); if (library != null && library.equals("primefaces")) { String resourceName = (String) attrs.get("name"); if (resourceName.startsWith("jquery") || resourceName.startsWith("primefaces")) { shouldRender = false; } } if (shouldRender) { resource.encodeAll(context); } } if (cc.isLegacyWidgetNamespace()) { writer.startElement("script", null); writer.writeAttribute("type", "text/javascript", null); writer.write("PrimeFaces.settings.legacyWidgetNamespace = true;"); writer.endElement("script"); } if (!projectStage.equals(ProjectStage.Production)) { writer.write("PrimeFaces.settings.projectStage='" + projectStage.toString() + "';"); } }