Oh, that's easy.  In JSF, you don't explictly mark a SelectItem as being 
selected (unlike OPTION tags in HTML).  Instead, you set the parent 
SelectOne(Menu|Radio|Checkbox) element's "value" field (or, more specifically, 
the value of the managed bean property to which "value" is bound).  As long as 
the value of the SelectOne matches the value of one of the SelectItems, that 
SelectItem will be rendered as "selected".

I believe what you need to do in your case is something like this:
AttributeBean valAttr = new AttributeBean();
valAttr.setName("value");
valAttr.setValue("#{someBean.someProperty}");
selectElement.addAttribute(valAttr);

and then make sure that "someBean" initializes "someProperty" to a default 
value that corresponds to one of your SelectItems.


Rich Eggert
Member of Technical Staff
Proteus Technologies, LLC
http://www.proteus-technologies.com



-----Original Message-----
From: Mike Otto [mailto:[EMAIL PROTECTED]
Sent: Tue 9/11/2007 8:29 AM
To: user@shale.apache.org
Subject: RE: HtmlSelectOneMenu dynamically
 
Hello Richard,

thanks for answering. Now I have a clue about what I should do ..

The code below works like a charm, except that I don't know how
to declare a item as selected. I looked up
javax.faces.component.UISelectItem.class

and found

 private String _itemDescription = null;
    private Boolean _itemDisabled = null;
    private String _itemLabel = null;
    private Object _itemValue = null;
    private Object _value = null;

(no private Object _selected)

Besides I still do not have a solution for component type
javax.faces.SelectItem_s_ But I think with Items in singular
I have more control, right?

Regards Mike


// HTMLSelectOneMenu

        ElementBean selectElement = new ElementBean();
        selectElement.setComponentType("javax.faces.HtmlSelectOneMenu");
        selectElement.setJsfid("securitycheck");
        selectElement.setId("securitycheck");

        ElementBean itemElement = new ElementBean();
        itemElement.setComponentType("javax.faces.SelectItem");
        itemElement.setJsfid("selectItem");
        itemElement.setRenderId(generateId());

                AttributeBean attr = new AttributeBean();
                attr.setName("itemValue");
                attr.setValue("value1");
                itemElement.addAttribute(attr);

                attr = new AttributeBean();
                attr.setName("itemLabel");
                attr.setValue("Label");
                itemElement.addAttribute(attr);

        selectElement.addChild(itemElement);

        ElementBean itemElement2 = new ElementBean();
        itemElement2.setComponentType("javax.faces.SelectItem");
        itemElement2.setJsfid("selectItem2");
        itemElement2.setRenderId(generateId());

                attr = new AttributeBean();
                attr.setName("itemValue");
                attr.setValue("value2");
                itemElement2.addAttribute(attr);

                attr = new AttributeBean();
                attr.setName("disabled");
                attr.setValue("true");
                itemElement2.addAttribute(attr);

                attr = new AttributeBean();
                attr.setName("selected");       // fixme
                attr.setValue("true");
                itemElement2.addAttribute(attr);

        selectElement.addChild(itemElement2);

        root.addChild(selectElement);







Reply via email to