// 编写处理Action事件的方法 public void process(ActionEvent ae) { if (name.equals("疯狂Java讲义")) { price.setValue(ae.getComponent()); price.setSize(60); price.setStyle("background-color:#1111ff;" + "font-weight:bold"); } }
// 编写处理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"); } }
public UIComponent buildWidget( String elementName, Map<String, String> attributes, UIMetawidget metawidget) { FacesContext context = FacesContext.getCurrentInstance(); Application application = context.getApplication(); Class<?> clazz = WidgetBuilderUtils.getActualClassOrType(attributes, null); if (clazz == null) { return null; } // Colors (as of RichFaces 3.3.1) if (Color.class.equals(clazz)) { if (WidgetBuilderUtils.isReadOnly(attributes)) { return FacesContext.getCurrentInstance() .getApplication() .createComponent(HtmlOutputText.COMPONENT_TYPE); } return application.createComponent("org.richfaces.ColorPicker"); } // Suggestion box // // Note: for suggestion box to work in table column footer facets, you need // https://jira.jboss.org/jira/browse/RF-7700 if (String.class.equals(clazz)) { String facesSuggest = attributes.get(FACES_SUGGEST); if (facesSuggest != null) { UIComponent stubComponent = application.createComponent(UIStub.COMPONENT_TYPE); List<UIComponent> children = stubComponent.getChildren(); // Standard text box HtmlInputText inputText = (HtmlInputText) application.createComponent(HtmlInputText.COMPONENT_TYPE); inputText.setStyle(((HtmlMetawidget) metawidget).getStyle()); inputText.setStyleClass(((HtmlMetawidget) metawidget).getStyleClass()); children.add(inputText); UISuggestionBox suggestionBox = (UISuggestionBox) application.createComponent(HtmlSuggestionBox.COMPONENT_TYPE); // Lock the 'id's so they don't get changed. This is important for the // JavaScript getElementById that RichFaces generates. Also, do not just use // 'viewRoot.createUniqueId' because, as per the RenderKit specification: // // "If the value returned from component.getId() is non-null and does not start // with UIViewRoot.UNIQUE_ID_PREFIX, call component.getClientId() and render // the result as the value of the id attribute in the markup for the component." // // Therefore the 'id' attribute is never rendered, therefore the JavaScript // getElementById doesn't work. Add our own prefix instead inputText.setId("suggestionText_" + FacesUtils.createUniqueId()); suggestionBox.setId("suggestionBox_" + FacesUtils.createUniqueId()); // Suggestion box suggestionBox.setFor(inputText.getId()); suggestionBox.setVar("_internal"); children.add(suggestionBox); try { // RichFaces 3.2/JSF 1.2 mode // // Note: we wrap the MethodExpression as an Object[] to stop link-time // dependencies on javax.el.MethodExpression, so that we still work with // JSF 1.1 // // Note: according to JavaDocs returnType is only important when literal // (i.e. without #{...}) expression is used, otherwise Object.class is fine // (http://community.jboss.org/message/516830#516830) Object[] methodExpression = new Object[] { application .getExpressionFactory() .createMethodExpression( context.getELContext(), facesSuggest, Object.class, new Class[] {Object.class}) }; ClassUtils.setProperty(suggestionBox, "suggestionAction", methodExpression[0]); } catch (Exception e) { // RichFaces 3.1/JSF 1.1 mode MethodBinding methodBinding = application.createMethodBinding(facesSuggest, new Class[] {Object.class}); suggestionBox.setSuggestionAction(methodBinding); } // Column // // Note: this must be javax.faces.component.html.HtmlColumn, not // org.richfaces.component.html.HtmlColumn. The latter displayed okay, but when // a value was selected it did not populate back to the HtmlInputText UIColumn column = (UIColumn) application.createComponent(javax.faces.component.html.HtmlColumn.COMPONENT_TYPE); column.setId(FacesUtils.createUniqueId()); suggestionBox.getChildren().add(column); // Output text box UIComponent columnText = application.createComponent(HtmlOutputText.COMPONENT_TYPE); columnText.setId(FacesUtils.createUniqueId()); ValueBinding valueBinding = application.createValueBinding("#{_internal}"); columnText.setValueBinding("value", valueBinding); column.getChildren().add(columnText); return stubComponent; } } return null; }