You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shale.apache.org by David Delbecq <de...@oma.be> on 2006/07/13 16:01:15 UTC

Custom component with childrens

Hello all,

Starting to learn JSF, i tried to create my custom UI component. Because
it will be made of several fields and button, i decided to extends
HTMLPanelGrid. The constructor creates the various components part of
the Panel. After getting "funny" errors in console, i worked on a step
by step and noticed in restore view, myfaces does inject childs in my
component (so the number of child is double: those created at
intanciation of my component and those saved from previous instance and
injected as part of restore view).

My question is, considering the way i do it for now (which is bad ;) ),
what is the best way to avoid this restore view problem. How can my
component know if it is in a state where it need to create itself it's
children or in a state where the controller will restore the children
tree itself?

Thank for help.

public class HTMLAddressComponent extends HtmlPanelGrid implements
        NamingContainer {
.....

    public HTMLAddressComponent(){
        street = new HtmlInputText();
        number = new HtmlInputText();
        town = new HtmlInputText();
        postCode = new HtmlInputText();
        command = new HtmlCommandButton();
        command.setValue("enable/disable");
        street.setId("street");
        number.setId("number");
        town.setId("town");
        postCode.setId("postCode");
        command.setId("command");
        List childs = getChildren();
        HtmlPanelGroup group = new HtmlPanelGroup();
        List l = group.getChildren();
        l.add(getText("Street: "));
        l.add(street);
        l.add(getText("Number: "));
        l.add(number);
        childs.add(group);
        group = new HtmlPanelGroup();
        l = group.getChildren();
        l.add(getText("postcode: "));
        l.add(postCode);
        l.add(getText("Town: "));
        l.add(town);
        childs.add(group);
        childs.add(command);
    }
    private UIComponent getText(String value){
        HtmlOutputText text = new HtmlOutputText();
        text.setValue(value);
        return text;
    }




Re: Custom component with childrens

Posted by Mike Kienenberger <mk...@gmail.com>.
On 7/13/06, James Mitchell <jm...@apache.org> wrote:
> There's a few things going on under the
> covers with respect to how JSF deals with components.  You might also
> think about getting a good book or referencing a tutorial while
> building your app.  There's a lot more to JSF than meets the eye.

David,

As I mentioned before, there's an old but working example at this url
that you can use as a reference.

http://issues.apache.org/jira/secure/attachment/12311217/RowAndColumnRelationshipComponent.zip

Re: Custom component with childrens

Posted by delbd <de...@oma.be>.
Am playing with jsf and  learning ^^ (got example doing new HtmlXXX from
sun's forum)
for the tutorials, if you have good references i'll be happy to read
them, i couldn't find anything that went further than just creating html
elements inside the renderer (nothing about creating composite elements
for example). And i still need a bit of time to read the full 300 pages
jsf specs  ^^ :)


Btw am still  trying to find how to connect the 'command' to the bean
'someAction' method, because the bean is the result of evaluation of my
custom component 'value' and i bet if my custom component is invoked
like this
<customLib:customComponent value="#{myBean.myProperty}"/> and i try to
generate a method binding for command action using such an expression
'#{#{myBean.myProperty}.someAction}' this will probably fail :)

Thanks for your time,
David Delbecq

James Mitchell a écrit :

> I see, sorry I wasn't paying attention to your constructor.
>
>
> Try creating your components with something like this...
>
>
> ...
> ...
>
>         street = createHtmlInputText("street");
>         number = createHtmlInputText("number");
>         town = createHtmlInputText("town");
>         postCode = createHtmlInputText("postCode");
>         command = createHtmlCommandButton("command");
>
> ...
> ...
> }
>
> protected HtmlInputText createHtmlInputText(String id) {
>     Application application = FacesContext.getCurrentInstance
> ().getApplication();
>
>     HtmlInputText intput =
>         (HtmlInputText)application.createComponent
> (HtmlInputText.COMPONENT_TYPE);
>     input.setId(id);
>     return input;
> }
> protected HtmlInputText createHtmlCommandButton(String id) {
>     Application application = FacesContext.getCurrentInstance
> ().getApplication();
> ...
> ...
>
>
>
>
> The problem is in how you are creating new components.  It isn't 
> enough just to do new HtmlXXX(), you must create them with the 
> createComponent method.  There's a few things going on under the 
> covers with respect to how JSF deals with components.  You might also 
> think about getting a good book or referencing a tutorial while 
> building your app.  There's a lot more to JSF than meets the eye.
>
>
> -- 
> James Mitchell
>
>
>
>
> On Jul 13, 2006, at 2:44 PM, delbd wrote:
>
>> No, am creating a JSF component on it's own (with it's own tag), 
>> like this
>> <mytaglib:mycomponent id="someId" value="#{myBean}"/>
>>
>>
>> James Mitchell a écrit :
>>
>>> Assuming you are using JSP, how are using this on your page?  With
>>> your own taglib or binding to an existing instance?  If you are
>>> simply binding a standard HtmlPanelGrid and then declaring child
>>> components, you will need to bind any nested components as well.
>>>
>>>
>>> -- 
>>> James Mitchell
>>>
>>>
>>>
>>>
>>> On Jul 13, 2006, at 10:01 AM, David Delbecq wrote:
>>>
>>>> Hello all,
>>>>
>>>> Starting to learn JSF, i tried to create my custom UI component.
>>>> Because
>>>> it will be made of several fields and button, i decided to extends
>>>> HTMLPanelGrid. The constructor creates the various components  part of
>>>> the Panel. After getting "funny" errors in console, i worked on a 
>>>> step
>>>> by step and noticed in restore view, myfaces does inject childs  in my
>>>> component (so the number of child is double: those created at
>>>> intanciation of my component and those saved from previous 
>>>> instance  and
>>>> injected as part of restore view).
>>>>
>>>> My question is, considering the way i do it for now (which is   bad
>>>> ;) ),
>>>> what is the best way to avoid this restore view problem. How can my
>>>> component know if it is in a state where it need to create itself 
>>>> it's
>>>> children or in a state where the controller will restore the  children
>>>> tree itself?
>>>>
>>>> Thank for help.
>>>>
>>>> public class HTMLAddressComponent extends HtmlPanelGrid implements
>>>>         NamingContainer {
>>>> .....
>>>>
>>>>     public HTMLAddressComponent(){
>>>>         street = new HtmlInputText();
>>>>         number = new HtmlInputText();
>>>>         town = new HtmlInputText();
>>>>         postCode = new HtmlInputText();
>>>>         command = new HtmlCommandButton();
>>>>         command.setValue("enable/disable");
>>>>         street.setId("street");
>>>>         number.setId("number");
>>>>         town.setId("town");
>>>>         postCode.setId("postCode");
>>>>         command.setId("command");
>>>>         List childs = getChildren();
>>>>         HtmlPanelGroup group = new HtmlPanelGroup();
>>>>         List l = group.getChildren();
>>>>         l.add(getText("Street: "));
>>>>         l.add(street);
>>>>         l.add(getText("Number: "));
>>>>         l.add(number);
>>>>         childs.add(group);
>>>>         group = new HtmlPanelGroup();
>>>>         l = group.getChildren();
>>>>         l.add(getText("postcode: "));
>>>>         l.add(postCode);
>>>>         l.add(getText("Town: "));
>>>>         l.add(town);
>>>>         childs.add(group);
>>>>         childs.add(command);
>>>>     }
>>>>     private UIComponent getText(String value){
>>>>         HtmlOutputText text = new HtmlOutputText();
>>>>         text.setValue(value);
>>>>         return text;
>>>>     }
>>>>
>>>>
>>>>
>>>
>>
>
>


Re: Custom component with childrens

Posted by James Mitchell <jm...@apache.org>.
I see, sorry I wasn't paying attention to your constructor.


Try creating your components with something like this...


...
...

         street = createHtmlInputText("street");
         number = createHtmlInputText("number");
         town = createHtmlInputText("town");
         postCode = createHtmlInputText("postCode");
         command = createHtmlCommandButton("command");

...
...
}

protected HtmlInputText createHtmlInputText(String id) {
	Application application = FacesContext.getCurrentInstance 
().getApplication();

	HtmlInputText intput =
		(HtmlInputText)application.createComponent 
(HtmlInputText.COMPONENT_TYPE);
	input.setId(id);
	return input;
}
protected HtmlInputText createHtmlCommandButton(String id) {
	Application application = FacesContext.getCurrentInstance 
().getApplication();
...
...




The problem is in how you are creating new components.  It isn't  
enough just to do new HtmlXXX(), you must create them with the  
createComponent method.  There's a few things going on under the  
covers with respect to how JSF deals with components.  You might also  
think about getting a good book or referencing a tutorial while  
building your app.  There's a lot more to JSF than meets the eye.


--
James Mitchell




On Jul 13, 2006, at 2:44 PM, delbd wrote:

> No, am creating a JSF component on it's own (with it's own tag),  
> like this
> <mytaglib:mycomponent id="someId" value="#{myBean}"/>
>
>
> James Mitchell a écrit :
>
>> Assuming you are using JSP, how are using this on your page?  With
>> your own taglib or binding to an existing instance?  If you are
>> simply binding a standard HtmlPanelGrid and then declaring child
>> components, you will need to bind any nested components as well.
>>
>>
>> -- 
>> James Mitchell
>>
>>
>>
>>
>> On Jul 13, 2006, at 10:01 AM, David Delbecq wrote:
>>
>>> Hello all,
>>>
>>> Starting to learn JSF, i tried to create my custom UI component.
>>> Because
>>> it will be made of several fields and button, i decided to extends
>>> HTMLPanelGrid. The constructor creates the various components  
>>> part of
>>> the Panel. After getting "funny" errors in console, i worked on a  
>>> step
>>> by step and noticed in restore view, myfaces does inject childs  
>>> in my
>>> component (so the number of child is double: those created at
>>> intanciation of my component and those saved from previous  
>>> instance  and
>>> injected as part of restore view).
>>>
>>> My question is, considering the way i do it for now (which is   
>>> bad ;) ),
>>> what is the best way to avoid this restore view problem. How can my
>>> component know if it is in a state where it need to create itself  
>>> it's
>>> children or in a state where the controller will restore the  
>>> children
>>> tree itself?
>>>
>>> Thank for help.
>>>
>>> public class HTMLAddressComponent extends HtmlPanelGrid implements
>>>         NamingContainer {
>>> .....
>>>
>>>     public HTMLAddressComponent(){
>>>         street = new HtmlInputText();
>>>         number = new HtmlInputText();
>>>         town = new HtmlInputText();
>>>         postCode = new HtmlInputText();
>>>         command = new HtmlCommandButton();
>>>         command.setValue("enable/disable");
>>>         street.setId("street");
>>>         number.setId("number");
>>>         town.setId("town");
>>>         postCode.setId("postCode");
>>>         command.setId("command");
>>>         List childs = getChildren();
>>>         HtmlPanelGroup group = new HtmlPanelGroup();
>>>         List l = group.getChildren();
>>>         l.add(getText("Street: "));
>>>         l.add(street);
>>>         l.add(getText("Number: "));
>>>         l.add(number);
>>>         childs.add(group);
>>>         group = new HtmlPanelGroup();
>>>         l = group.getChildren();
>>>         l.add(getText("postcode: "));
>>>         l.add(postCode);
>>>         l.add(getText("Town: "));
>>>         l.add(town);
>>>         childs.add(group);
>>>         childs.add(command);
>>>     }
>>>     private UIComponent getText(String value){
>>>         HtmlOutputText text = new HtmlOutputText();
>>>         text.setValue(value);
>>>         return text;
>>>     }
>>>
>>>
>>>
>>
>


Re: Custom component with childrens

Posted by delbd <de...@oma.be>.
No, am creating a JSF component on it's own (with it's own tag), like this
<mytaglib:mycomponent id="someId" value="#{myBean}"/>


James Mitchell a écrit :

> Assuming you are using JSP, how are using this on your page?  With 
> your own taglib or binding to an existing instance?  If you are 
> simply binding a standard HtmlPanelGrid and then declaring child 
> components, you will need to bind any nested components as well.
>
>
> -- 
> James Mitchell
>
>
>
>
> On Jul 13, 2006, at 10:01 AM, David Delbecq wrote:
>
>> Hello all,
>>
>> Starting to learn JSF, i tried to create my custom UI component. 
>> Because
>> it will be made of several fields and button, i decided to extends
>> HTMLPanelGrid. The constructor creates the various components part of
>> the Panel. After getting "funny" errors in console, i worked on a step
>> by step and noticed in restore view, myfaces does inject childs in my
>> component (so the number of child is double: those created at
>> intanciation of my component and those saved from previous instance  and
>> injected as part of restore view).
>>
>> My question is, considering the way i do it for now (which is  bad ;) ),
>> what is the best way to avoid this restore view problem. How can my
>> component know if it is in a state where it need to create itself it's
>> children or in a state where the controller will restore the children
>> tree itself?
>>
>> Thank for help.
>>
>> public class HTMLAddressComponent extends HtmlPanelGrid implements
>>         NamingContainer {
>> .....
>>
>>     public HTMLAddressComponent(){
>>         street = new HtmlInputText();
>>         number = new HtmlInputText();
>>         town = new HtmlInputText();
>>         postCode = new HtmlInputText();
>>         command = new HtmlCommandButton();
>>         command.setValue("enable/disable");
>>         street.setId("street");
>>         number.setId("number");
>>         town.setId("town");
>>         postCode.setId("postCode");
>>         command.setId("command");
>>         List childs = getChildren();
>>         HtmlPanelGroup group = new HtmlPanelGroup();
>>         List l = group.getChildren();
>>         l.add(getText("Street: "));
>>         l.add(street);
>>         l.add(getText("Number: "));
>>         l.add(number);
>>         childs.add(group);
>>         group = new HtmlPanelGroup();
>>         l = group.getChildren();
>>         l.add(getText("postcode: "));
>>         l.add(postCode);
>>         l.add(getText("Town: "));
>>         l.add(town);
>>         childs.add(group);
>>         childs.add(command);
>>     }
>>     private UIComponent getText(String value){
>>         HtmlOutputText text = new HtmlOutputText();
>>         text.setValue(value);
>>         return text;
>>     }
>>
>>
>>
>


Re: Custom component with childrens

Posted by James Mitchell <jm...@apache.org>.
Assuming you are using JSP, how are using this on your page?  With  
your own taglib or binding to an existing instance?  If you are  
simply binding a standard HtmlPanelGrid and then declaring child  
components, you will need to bind any nested components as well.


--
James Mitchell




On Jul 13, 2006, at 10:01 AM, David Delbecq wrote:

> Hello all,
>
> Starting to learn JSF, i tried to create my custom UI component.  
> Because
> it will be made of several fields and button, i decided to extends
> HTMLPanelGrid. The constructor creates the various components part of
> the Panel. After getting "funny" errors in console, i worked on a step
> by step and noticed in restore view, myfaces does inject childs in my
> component (so the number of child is double: those created at
> intanciation of my component and those saved from previous instance  
> and
> injected as part of restore view).
>
> My question is, considering the way i do it for now (which is  
> bad ;) ),
> what is the best way to avoid this restore view problem. How can my
> component know if it is in a state where it need to create itself it's
> children or in a state where the controller will restore the children
> tree itself?
>
> Thank for help.
>
> public class HTMLAddressComponent extends HtmlPanelGrid implements
>         NamingContainer {
> .....
>
>     public HTMLAddressComponent(){
>         street = new HtmlInputText();
>         number = new HtmlInputText();
>         town = new HtmlInputText();
>         postCode = new HtmlInputText();
>         command = new HtmlCommandButton();
>         command.setValue("enable/disable");
>         street.setId("street");
>         number.setId("number");
>         town.setId("town");
>         postCode.setId("postCode");
>         command.setId("command");
>         List childs = getChildren();
>         HtmlPanelGroup group = new HtmlPanelGroup();
>         List l = group.getChildren();
>         l.add(getText("Street: "));
>         l.add(street);
>         l.add(getText("Number: "));
>         l.add(number);
>         childs.add(group);
>         group = new HtmlPanelGroup();
>         l = group.getChildren();
>         l.add(getText("postcode: "));
>         l.add(postCode);
>         l.add(getText("Town: "));
>         l.add(town);
>         childs.add(group);
>         childs.add(command);
>     }
>     private UIComponent getText(String value){
>         HtmlOutputText text = new HtmlOutputText();
>         text.setValue(value);
>         return text;
>     }
>
>
>