// 编写处理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"); } }
/** * Returns a container with "add category" UI * * @return WFContainer */ private WFContainer getAddCategoryContainer() { WFContainer container = new WFContainer(); WFResourceUtil localizer = WFResourceUtil.getResourceUtilContent(); // Text HtmlOutputText addText = localizer.getTextVB("new_category"); container.add(addText); // Input HtmlInputText newCategoryInput = new HtmlInputText(); newCategoryInput.setSize(40); newCategoryInput.setId(getAddCategoryInputId()); container.add(newCategoryInput); // Button HtmlCommandButton addCategoryButton = localizer.getButtonVB(getAddButtonId(), "add", this); container.add(addCategoryButton); return container; }
// 编写处理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"); } }
/** * Takes a parent UIComponent and adds all UIComponents which are required for configuring a * metric. i.e. metric name, math expression, value, etc. * * @param metric * @param parent */ private void helperCreateUICompsForMetricConfig(MetricBean metric, UIComponent parent) { // 1)remove all existing child elements that may have been created previously from ALL // pConfigVeryGood, pConfigGood, etc. removeAllInputHelpersForMetricConfig(); // 2) now build the gui elements for building a metric configuration // 2a) Add an output text with the metric's name HtmlOutputText mnameText = new HtmlOutputText(); mnameText.setId("metricName" + metric.getInternalID()); mnameText.setValue(metric.getName()); // add on parent parent.getChildren().add(mnameText); // 2b) Select a math expression HtmlSelectOneMenu select2 = new HtmlSelectOneMenu(); select2.setId("mathSelect" + metric.getInternalID()); UISelectItems items2 = new UISelectItems(); items2.setId("mathvals" + metric.getInternalID()); items2.setValue(metric.getAllAvailableTypes()); Class[] parms2 = new Class[] {ValueChangeEvent.class}; ExpressionFactory ef = FacesContext.getCurrentInstance().getApplication().getExpressionFactory(); MethodExpression mb = ef.createMethodExpression( FacesContext.getCurrentInstance().getELContext(), "#{AutoEvalSerUserConfigBean.processMathExprChange}", null, parms2); MethodExpressionValueChangeListener vcl = new MethodExpressionValueChangeListener(mb); select2.addValueChangeListener(vcl); select2.getChildren().add(items2); select2.setImmediate(true); // place an ajax support on the selectonemenu field HtmlAjaxSupport ajaxSupport2 = new HtmlAjaxSupport(); // Class[] parms = new Class[]{ActionEvent.class}; // ajaxSupport.setAction(FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{AutoEvalSerUserConfigBean.sliderValue}", parms)); ajaxSupport2.setEvent("onchange"); // to add multiple ids for rerendering, separate them with a "," ajaxSupport2.setReRender(select2.getId()); ajaxSupport2.setEventsQueue("foo"); select2.getFacets().put("a4jsupport2", ajaxSupport2); // add to parent parent.getChildren().add(select2); // 2c) For all input types except boolean values: if (metric.isHtmlInputTextUsed()) { // enter a boundary value for a specific added metric HtmlInputText inputText = new HtmlInputText(); inputText.setId("metricBoundary" + metric.getInternalID()); inputText.setValue(metric.getEvalBoundary()); inputText.setSize(10); Class[] parms = new Class[] {ValueChangeEvent.class}; MethodExpression mb2 = ef.createMethodExpression( FacesContext.getCurrentInstance().getELContext(), "#{AutoEvalSerUserConfigBean.processMetricBoundaryValueChange}", null, parms); MethodExpressionValueChangeListener vcl2 = new MethodExpressionValueChangeListener(mb2); inputText.addValueChangeListener(vcl2); inputText.setImmediate(true); // place an ajax support on the InputText field HtmlAjaxSupport ajaxSupport = new HtmlAjaxSupport(); // Class[] parms = new Class[]{ActionEvent.class}; // ajaxSupport.setAction(FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{AutoEvalSerUserConfigBean.sliderValue}", parms)); ajaxSupport.setEvent("onchange"); // to add multiple ids for rerendering, separate them with a "," ajaxSupport.setReRender(inputText.getId()); ajaxSupport.setEventsQueue("foo"); inputText.getFacets().put("a4jsupport", ajaxSupport); // add to parent parent.getChildren().add(inputText); } else { // add a drop-down box for boolean values HtmlSelectOneMenu select = new HtmlSelectOneMenu(); select.setId("booleanSelect" + metric.getInternalID()); UISelectItems items = new UISelectItems(); items.setId("vals" + metric.getInternalID()); List<SelectItem> l = new ArrayList<SelectItem>(); l.add(new SelectItem("true")); l.add(new SelectItem("false")); items.setValue(l); Class[] parms = new Class[] {ValueChangeEvent.class}; MethodExpression mb3 = ef.createMethodExpression( FacesContext.getCurrentInstance().getELContext(), "#{AutoEvalSerUserConfigBean.processMetricBoundaryValueChange}", null, parms); MethodExpressionValueChangeListener vcl3 = new MethodExpressionValueChangeListener(mb3); select.addValueChangeListener(vcl3); select.getChildren().add(items); select.setImmediate(true); // place an ajax support on the selectonemenu field HtmlAjaxSupport ajaxSupport = new HtmlAjaxSupport(); // Class[] parms = new Class[]{ActionEvent.class}; // ajaxSupport.setAction(FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{AutoEvalSerUserConfigBean.sliderValue}", parms)); ajaxSupport.setEvent("onchange"); // to add multiple ids for rerendering, separate them with a "," ajaxSupport.setReRender(select.getId()); ajaxSupport.setEventsQueue("foo"); select.getFacets().put("a4jsupport", ajaxSupport); // add to parent parent.getChildren().add(select); } // 2d) finally the submit button for saving the configuration HtmlCommandButton button_save = new HtmlCommandButton(); button_save.setId("buttonSave" + metric.getInternalID()); button_save.setValue("add config"); Class[] parms3 = new Class[] {ActionEvent.class}; MethodExpression mb4 = ef.createMethodExpression( FacesContext.getCurrentInstance().getELContext(), "#{AutoEvalSerUserConfigBean.command_saveMetricConfiguration}", null, parms3); MethodExpressionActionListener vcl4 = new MethodExpressionActionListener(mb4); button_save.addActionListener(vcl4); UIParameter p = new UIParameter(); p.setId("param_save_button" + metric.getInternalID()); p.setName("pConfigPanel"); p.setValue(parent.getId()); button_save.getChildren().add(p); parent.getChildren().add(button_save); HtmlOutputText message = new HtmlOutputText(); message.setId("message" + metric.getInternalID()); message.setStyle("color:red;"); parent.getChildren().add(message); }