Help on JSF 2.0 Custom Components and Primefaces?

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

Up vote 2 down vote favorite share g+ share fb share tw.

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 tag writer. StartElement(Constants.

STR_TABLE, this); //start the tag writer. StartElement(Constants. STR_TR, this); //start the tag writer.

StartElement(Constants. STR_TD, this); //encode the button 1 component inside this encodeAllComponent(context, getMyPrimePanel()); // end the tag writer. EndElement(Constants.

STR_TD); //end the tag writer. EndElement(Constants. STR_TR); //end the 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() 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 java jsf jsf-2.0 primefaces link|improve this question edited Sep 7 '11 at 7:02Andrey1,214115 asked Sep 7 '11 at 5:04NsDeep247 89% accept rate.

1 Check the stack trace of the exception (in server log). There must be a line number for the NPE, such as "NullPointerException at line xy". Tell us which line it is in your code.

This would make it easier for us to help you. – Matt Handy Sep 7 '11 at 7:28 Thanks for the reply. But it is not showing any line number.

– NsDeep Sep 7 '11 at 9:12.

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: 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 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.

Thanks for the reply. Can you tell me more clearly as to how to create? I'm very new to custom components.

Should I create another class which extends the UIComponentTag class? And in setProperties I should do valueBinding using the EL is it? Can you give a sample snippet for the same?

– NsDeep Sep 7 '11 at 9:11 @Deepika - you do not need any additional tags using the approach I outlined; you just need to make use of the component binding feature. See the JSF specification for details on behaviour. I have added some sample code.

– McDowell Sep 7 '11 at 10:41 Ya this is right. But Custom component is not used here is it. But my requirement is to use custom component.

:( I was able to render the controls in the way you have specified but I should do the same using custom components. And in jsf 2.0 it is not allowing me to include a tld file as it allows only facelets. (.xhtml) pages – NsDeep Sep 7 '11 at 10:55 @Deepika - I thought it was obvious, but use the binding attribute with your custom control.

How tags are defined in Facelets is detailed in the JSF specification. Facelets has its own tag library descriptor format - see this example – McDowell Sep 7 '11 at 11:05 1 @Deepika - I do not know what your error is; the posted code works as is on Mojarra 2. X (running on Glassfish 3.1) and MyFaces 2.

X (running on Jetty 7) – McDowell Sep 7 '117 at 18:07.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions