开发者

Help on JSF 2.0 Custom Components and Primefaces

I have a requirement to create a panel dynamically using jsf 2.0 custom components. The controls within the panel will be dynamically read from an xml and rendered on the selection of the corresponding object (Eg : If Person is selected, I should render a panel which will have controls related to person like : Person age field(inputtext), Person DOB(calendar) and so on).

I am trying to render it from the component class which extends UIComponentBase.

ResponseWriter writer = Util.getResponseWriter(context);
//start the <table> tag
writer.startElement(Constants.STR_TABLE, this);

//start the <tr> tag
writer.startElement(Constants.STR_TR, this);

//start the <td> tag
writer.startElement(Constants.STR_TD, this);

//encode the button 1 component inside this <td>
encodeAllComponent(context, getMyPrimePanel());

// end the <td> tag
writer.endElement(Constants.STR_TD);

//end the <tr> tag
writer.endElement(Constants.STR_TR);


//end the <table> tag
writer.endElement(Constants.STR_TABLE);

  //private variable to render a panel
  private Panel myPrimePanel;

  /**
  * @return the myPrimePanel
  */
  public Panel getMyPrimePanel() {
        System.out.println("inside the panel get method------");
        if (myPrimePanel.getChildCount() <= 1) {
              System.out.println("inside the panel creation function");
              InputText input = new InputText();
              myPrimePanel.getChildren().add(input);

        }
        System.out.println("inside the panel get method-------------");
        return myPrimePanel;
  }

  /**
  * @param myPrimePanel the myPrimePanel to set
  */
  public void setMyPrimePanel(Panel myPrimePanel) {
        System.out.println("inside the panel get method-------------");
        //initialize the button 1 component
        this.myPrimePanel = myPrimePanel;
  }

I have done this way. But I am getting a null pointer exception. How can the panel with the defined controls be rendered dynamically?

This is what I'm getting -

====Will Start Rendering===开发者_运维问答= inside the panel get method------ Sep 7, 2011 10:01:42 AM com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback visit SEVERE: java.lang.NullPointerException


myPrimePanel is null. You would need to assign an instance of it for it not to be null.

However, I think you are going about this task the wrong way. You should not be adding components to the tree in the Render phase. Presumably, you want to read, validate and that input data into the model some time.

Emit your start elements in encodeBegin. Emit your end elements in encodeEnd. Do not override encodeChildren and ensure getRendersChildren returns false (this is the default).

Use the binding attribute to provision the dynamic component during the Restore View phase. Add a request scope managed bean with a property of type UIComponent and bind it to the element in your view using EL. In the getter, if the property is null, create a new instance of your custom control and add any children.


Consider this view:

<h:form>
  <h:panelGroup id="myPanel" binding="#{componentMakerBean.panel}" />
  <h:commandButton value="go" action="#{componentMakerBean.dumpValuesAction}" />
</h:form>

The panel is created and populated in a bean:

/** Request scope bean defined in faces-config.xml */
public class ComponentMakerBean {
  private UIPanel panel;

  public UIPanel getPanel() {
    if(panel == null) {
      panel = new HtmlPanelGroup();
      for(int i=0; i<3; i++) {
        panel.getChildren().add(new HtmlInputText());
      }
    }
    return panel;
  }

  public void setPanel(UIPanel panel) { this.panel = panel; }

  public String dumpValuesAction() {
    for(Object kid : panel.getChildren()) {
      if(kid instanceof ValueHolder) {
        ValueHolder valueHolder = (ValueHolder) kid;
        System.out.println(valueHolder.getValue());
      }
    }
    return null; //no navigation
  }
}

At runtime, this will emit three editable text fields whose values will be printed to the log when the button is clicked.

This code was tested in a JSP using Java 5 & JSF 1.1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜