如何使用primefaces update或jsf render刷新自定义组件?

时间:2015-01-14 07:15:39

标签: jsf jsf-2 primefaces

我尝试使用jsf2.1定义自定义组件。单击按钮后必须更新组件。该组件具有呈现属性,默认为false,即按钮单击时更新。这是我的代码。

<cu:ActionBar id="actionBar" rendered="#{bean.booleanCondition}">
    <!-- some code inside !-->
</cu:ActionBar>

触发更新的代码。

<p:commandButton id="myButton" actionListener="#{bean.foo} update=":actionBar"/>

使用此代码,bean.booleanCondition设置为true,但不会显示actionBar组件。如果我把组件包装在里面就可以了。

这是我的ActionBar组件类:

@FacesComponent(value = "ACTION_BAR")
public class ActionBarComponent extends UIPanel {

       @Override
       public void encodeBegin(FacesContext ctx) throws IOException {
             if (ctx == null) {
                    throw new NullPointerException();
             }
             ResponseWriter writer = ctx.getResponseWriter();

             writer.startElement("div", this);
             if (this.getAttributes().get("id") != null) {
                    writer.writeAttribute("id", this.getAttributes().get("id"), "id");
             }
             String styleClass = this.getAttributes().get("styleClass") != null ? "" : " " + this.getAttributes().get("styleClass");
             writer.writeAttribute("class", "actionBar" + styleClass, null);
             writer.startElement("form", this);
             writer.writeAttribute("enctype", "application/x-www-form-urlencoded", null);
             writer.writeAttribute("method", "post", null);
             writer.writeAttribute("name", "actionBarForm", null);
             if (this.getAttributes().get("id") != null) {
                    writer.writeAttribute("id", this.getAttributes().get("id") + ":" + this.getAttributes().get("id") + "Form", "id");
             }
             writer.writeAttribute("action", ctx.getExternalContext().getRequestContextPath() + ctx.getExternalContext().getRequestServletPath(), null);
             writer.startElement("div", this);
             writer.writeAttribute("class", "actionBarGroup", "class");
             writer.startElement("span", this);
             writer.append("Actions :");
             writer.endElement("span");

       }

       @Override
       public void encodeEnd(FacesContext ctx) throws IOException {
             if (ctx == null) {
                    throw new NullPointerException();
             }
             ResponseWriter writer = ctx.getResponseWriter();

             writer.endElement("div");
             writer.endElement("form");
             writer.endElement("div");
       }
}

我怎么能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

您无法使用rendered属性更新组件。您可以在此处获得解释和解决方案:update attribute does not update component通常,在rendered中打包<h:panelGroup>组件并进行更新。