// 编写处理Action事件的方法 public void processAction(ActionEvent event) { // 获取当前的FacesContext对象 FacesContext context = FacesContext.getCurrentInstance(); // 获取JSF页面中<f:view.../>元素 UIViewRoot viewRoot = context.getViewRoot(); // 通过ID获取<f:view.../>内的<h:form.../>子元素。 UIComponent comp = viewRoot.findComponent("addForm"); // 通过ID获取<h:form.../>内的第一个<h:inputText.../>子元素。 UIInput input = (UIInput) comp.findComponent("name"); // 通过ID获取<h:form.../>内的第二个<h:inputText.../>子元素。 HtmlInputText price = (HtmlInputText) comp.findComponent("price"); if (input.getValue().equals("疯狂Java讲义")) { price.setSize(60); price.setValue("99.0元"); price.setStyle("background-color:#9999ff;" + "font-weight:bold"); } }
@SuppressWarnings({"UnusedDeclaration"}) protected UIComponent findReparentedComponent( FaceletContext ctx, UIComponent parent, String tagId) { UIComponent facet = parent.getFacets().get(UIComponent.COMPOSITE_FACET_NAME); if (facet != null) { UIComponent newParent = facet.findComponent((String) parent.getAttributes().get(tagId)); if (newParent != null) return ComponentSupport.findChildByTagId(newParent, tagId); } return null; }
public static String findClientIds(String ids, UIComponent component, FacesContext context) throws IOException { if (ids == null) return null; StringBuilder clientIds = new StringBuilder(); String[] idlist = ids.split("[\\s]"); for (String id : idlist) { if (!id.startsWith("@")) { UIComponent found = component.findComponent(id); if (found != null) { if (clientIds.length() > 0) clientIds.append(" "); clientIds.append(found.getClientId(context)); } else { throw new IOException("Cannot find id " + id + " within components NamingContainer"); } } } return clientIds.toString(); }
@SuppressWarnings("unchecked") public void showCake(ActionEvent event) { UIComponent root = FacesContext.getCurrentInstance().getViewRoot(); UIPanel cake = (UIPanel) root.findComponent("cake"); cake.setRendered(true); List cakeParts = cake.getChildren(); cakeParts.clear(); HtmlGraphicImage cakeLeftSide = new HtmlGraphicImage(); cakeLeftSide.setUrl("images/cake_left.gif"); cakeParts.add(cakeLeftSide); for (int i = 0; i < userAge; i++) { HtmlGraphicImage candle = new HtmlGraphicImage(); candle.setUrl("images/candle_on.gif"); cakeParts.add(candle); } HtmlGraphicImage cakeRightSide = new HtmlGraphicImage(); cakeRightSide.setUrl("images/cake_right.gif"); cakeParts.add(cakeRightSide); }
public void clearCake() { UIComponent root = FacesContext.getCurrentInstance().getViewRoot(); UIPanel cake = (UIPanel) root.findComponent("cake"); cake.setRendered(false); cake.getChildren().clear(); }
public static String findClientIds(FacesContext context, UIComponent component, String list) { if (list == null) return "@none"; // System.out.println("ComponentUtils.findClientIds() component.clientId: " + // component.getClientId(context) + " list: " + list); String[] ids = list.split("[,\\s]+"); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < ids.length; i++) { if (i != 0) buffer.append(" "); String id = ids[i].trim(); // System.out.println("ComponentUtils.findClientIds() ["+i+"] id: " + id); if (id.equals("@all") || id.equals("@none")) { // System.out.println("ComponentUtils.findClientIds() ["+i+"] " + id); buffer.append(id); } else if (id.equals("@this")) { // System.out.println("ComponentUtils.findClientIds() ["+i+"] @this : " + // component.getClientId(context)); buffer.append(component.getClientId(context)); } else if (id.equals("@parent")) { // System.out.println("ComponentUtils.findClientIds() ["+i+"] @parent: " + // component.getParent().getClientId(context)); buffer.append(component.getParent().getClientId(context)); } else if (id.equals("@form")) { UIComponent form = ComponentUtils.findParentForm(context, component); if (form == null) throw new FacesException( "Component " + component.getClientId(context) + " needs to be enclosed in a form"); buffer.append(form.getClientId(context)); } else { UIComponent comp = component.findComponent(id); // For portlets, if the standard search doesn't work, it may be necessary to do an absolute // search // which requires including the portlet's namespace. So the resulting encoded id looks // something // like portletNamespace:container:componentId. We make the search absolute by pre-pending // a leading colon (:). if (comp == null) { String encodedId = encodeNameSpace(context, id); if (!encodedId.startsWith(":")) { encodedId = ":" + encodedId; } comp = component.findComponent(encodedId); // System.out.println("ComponentUtils.findClientIds() ["+i+"] comp // : " + (comp == null ? "null" : comp.getClientId(context)) + " id: " + encodedId); } if (comp != null) { buffer.append(comp.getClientId(context)); } else { if (context.getApplication().getProjectStage().equals(ProjectStage.Development)) { logger.log(Level.INFO, "Cannot find component with identifier \"{0}\" in view.", id); } buffer.append(id); } } } return buffer.toString(); }