FAQ
Hi!

I'm new to this list and to JSF in general. I have a problem with the
t:selectItems component and would be grateful for any advice. I'm using
MyFaces 1.2.5, Tomahawk 1.1.8 and Facelets.

I have this in my file:

<h:selectManyCheckbox id="selectedRoles" value="#{myBean.selectedRoles}"
layout="pageDirection">
<t:selectItems value="#{myBean.allRoles}" var="role"
itemLabel="#{role}" itemValue="#{role}" />
</h:selectManyCheckbox>

selectedRoles is a Long[] array, allRoles is a Map<String, Long>,
mapping the role name to their ID. What I want is the ID as value of the
checkbox and the name as label text. Above code generates the ID as both
the value and the label text though.

Now page [1] has a more detailed explanation of the possibilities of the
value attribute than [2], but I did check the source code of
org.apache.myfaces.custom.selectitems.AbstractUISelectItems and it seems
to be the same for both the JSF 1.1 and 1.2 version of the component.

[1]
http://myfaces.apache.org/tomahawk-project/tomahawk/tagdoc/t_selectItems.html
[2]
http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_selectItems.html

The documentation under [1] says:

value: An EL expression that specifies the contents of the selection
list. The expression can refer to one of the following:

1. A single SelectItem
2. An array or Collection of SelectItem instances
3. A Map. The contents of the Map are used to create SelectItem
instances, where the SelectItem's label is the map's key value, and the
SelectItem's value is the map's value. When using a map, it is
recommended that an ordered implementation such as java.util.TreeMap is
used.

Number 3 is exactly what I want, I'm even using a TreeMap. I would
expect to get the keys as labels and the values as checkbox values
automatically with just using <t:selectItems
value="#{myBean.allRoles}"/>. But looking in the source code, I don't
see anything to provide this functionality. Unfortunately I also don't
see a way to configure the component with the other attributes to do
this, since only the value of the Map.Entry is set as request attribute.
Meaning, I can't just do something like:

<t:selectItems value="#{myBean.allRoles}" var="mapEntry"
itemLabel="#{mapEntry.key}" itemValue="#{mapEntry.value}" />

So, am I missing something here? Is there a way to do this? Or is the
documentation simply wrong?

Regards,
Johannes Ruthenberg

Search Discussions

  • Simon Kitching at Mar 14, 2009 at 3:40 pm

    On Fri, 2009-03-13 at 20:06 +0000, Johannes Ruthenberg wrote:
    Hi!

    I'm new to this list and to JSF in general. I have a problem with the
    t:selectItems component and would be grateful for any advice. I'm using
    MyFaces 1.2.5, Tomahawk 1.1.8 and Facelets.

    I have this in my file:

    <h:selectManyCheckbox id="selectedRoles" value="#{myBean.selectedRoles}"
    layout="pageDirection">
    <t:selectItems value="#{myBean.allRoles}" var="role"
    itemLabel="#{role}" itemValue="#{role}" />
    </h:selectManyCheckbox>

    selectedRoles is a Long[] array, allRoles is a Map<String, Long>,
    mapping the role name to their ID. What I want is the ID as value of the
    checkbox and the name as label text. Above code generates the ID as both
    the value and the label text though.

    Now page [1] has a more detailed explanation of the possibilities of the
    value attribute than [2], but I did check the source code of
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems and it seems
    to be the same for both the JSF 1.1 and 1.2 version of the component.

    [1]
    http://myfaces.apache.org/tomahawk-project/tomahawk/tagdoc/t_selectItems.html
    [2]
    http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_selectItems.html

    The documentation under [1] says:

    value: An EL expression that specifies the contents of the selection
    list. The expression can refer to one of the following:

    1. A single SelectItem
    2. An array or Collection of SelectItem instances
    3. A Map. The contents of the Map are used to create SelectItem
    instances, where the SelectItem's label is the map's key value, and the
    SelectItem's value is the map's value. When using a map, it is
    recommended that an ordered implementation such as java.util.TreeMap is
    used.

    Number 3 is exactly what I want, I'm even using a TreeMap. I would
    expect to get the keys as labels and the values as checkbox values
    automatically with just using <t:selectItems
    value="#{myBean.allRoles}"/>. But looking in the source code, I don't
    see anything to provide this functionality. Unfortunately I also don't
    see a way to configure the component with the other attributes to do
    this, since only the value of the Map.Entry is set as request attribute.
    Meaning, I can't just do something like:

    <t:selectItems value="#{myBean.allRoles}" var="mapEntry"
    itemLabel="#{mapEntry.key}" itemValue="#{mapEntry.value}" />

    So, am I missing something here? Is there a way to do this? Or is the
    documentation simply wrong?
    Have you tried this?
    <t:selectItems value="#{myBean.allRoles}"/>

    Regards,
    Simon
  • Johannes Ruthenberg at Mar 14, 2009 at 7:10 pm
    Hello!

    Thanks for your reply!

    Simon Kitching wrote on 14.03.2009 15:39:
    Have you tried this? <t:selectItems value="#{myBean.allRoles}"/>
    I have, but I get a NullPointerException if I do.

    Caused by: java.lang.NullPointerException
    at org.apache.catalina.connector.Request.getAttribute(Request.java:877)
    at
    org.apache.catalina.connector.RequestFacade.getAttribute(RequestFacade.java:263)
    at
    javax.servlet.ServletRequestWrapper.getAttribute(ServletRequestWrapper.java:82)
    at
    org.apache.myfaces.context.servlet.RequestMap.getAttribute(RequestMap.java:47)
    at
    org.apache.myfaces.util.AbstractAttributeMap.put(AbstractAttributeMap.java:104)
    at
    org.apache.myfaces.util.AbstractAttributeMap.put(AbstractAttributeMap.java:38)
    at
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems.putIteratorToRequestParam(AbstractUISelectItems.java:131)
    at
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems.createSelectItems(AbstractUISelectItems.java:107)
    at
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems.getValue(AbstractUISelectItems.java:75)
    at
    org.apache.myfaces.shared_impl.util.SelectItemsIterator.hasNext(SelectItemsIterator.java:128)
    at
    org.apache.myfaces.shared_impl.renderkit.RendererUtils.internalGetSelectItemList(RendererUtils.java:557)
    at
    org.apache.myfaces.shared_impl.renderkit.RendererUtils.getSelectItemList(RendererUtils.java:542)
    at
    org.apache.myfaces.shared_impl.renderkit.html.HtmlCheckboxRendererBase.renderCheckboxList(HtmlCheckboxRendererBase.java:117)
    at
    org.apache.myfaces.shared_impl.renderkit.html.HtmlCheckboxRendererBase.encodeEnd(HtmlCheckboxRendererBase.java:65)
    at
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:624)
    ... 76 more

    Looking at the actual implementation of the t:selectItems tag, I think
    this is consistent (it sets the current map value to the request under
    the provided variable name and then calls a method which retrieves this
    and tries to get the label and value for the select item -- this fails
    if no variable name is given). It just doesn't fit what the tag is
    supposed to do as per the documentation, at least as I understand it.

    I could simply built the list of SelectItem instances myself and set
    this in my bean. But I actually wouldn't want to handle more of the
    internal UI elements than necessary, so if the tag could do this, I
    think that would be the better approach. I also wonder if the
    documentation is wrong here or the tag class (a matter of perspective, I
    guess). ;-)

    Regards,
    Johannes Ruthenberg
  • Simon Kitching at Mar 14, 2009 at 8:42 pm
    Hi Johannes,
    On Sat, 2009-03-14 at 19:10 +0000, Johannes Ruthenberg wrote:

    Simon Kitching wrote on 14.03.2009 15:39:
    Have you tried this? <t:selectItems value="#{myBean.allRoles}"/>
    I have, but I get a NullPointerException if I do.

    Caused by: java.lang.NullPointerException
    at org.apache.catalina.connector.Request.getAttribute(Request.java:877)
    at
    org.apache.catalina.connector.RequestFacade.getAttribute(RequestFacade.java:263)
    at
    javax.servlet.ServletRequestWrapper.getAttribute(ServletRequestWrapper.java:82)
    at
    org.apache.myfaces.context.servlet.RequestMap.getAttribute(RequestMap.java:47)
    at
    org.apache.myfaces.util.AbstractAttributeMap.put(AbstractAttributeMap.java:104)
    at
    org.apache.myfaces.util.AbstractAttributeMap.put(AbstractAttributeMap.java:38)
    at
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems.putIteratorToRequestParam(AbstractUISelectItems.java:131)
    at
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems.createSelectItems(AbstractUISelectItems.java:107)
    at
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems.getValue(AbstractUISelectItems.java:75)
    at
    org.apache.myfaces.shared_impl.util.SelectItemsIterator.hasNext(SelectItemsIterator.java:128)
    at
    org.apache.myfaces.shared_impl.renderkit.RendererUtils.internalGetSelectItemList(RendererUtils.java:557)
    at
    org.apache.myfaces.shared_impl.renderkit.RendererUtils.getSelectItemList(RendererUtils.java:542)
    at
    org.apache.myfaces.shared_impl.renderkit.html.HtmlCheckboxRendererBase.renderCheckboxList(HtmlCheckboxRendererBase.java:117)
    at
    org.apache.myfaces.shared_impl.renderkit.html.HtmlCheckboxRendererBase.encodeEnd(HtmlCheckboxRendererBase.java:65)
    at
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:624)
    ... 76 more

    Looking at the actual implementation of the t:selectItems tag, I think
    this is consistent (it sets the current map value to the request under
    the provided variable name and then calls a method which retrieves this
    and tries to get the label and value for the select item -- this fails
    if no variable name is given). It just doesn't fit what the tag is
    supposed to do as per the documentation, at least as I understand it.

    I could simply built the list of SelectItem instances myself and set
    this in my bean. But I actually wouldn't want to handle more of the
    internal UI elements than necessary, so if the tag could do this, I
    think that would be the better approach. I also wonder if the
    documentation is wrong here or the tag class (a matter of perspective, I
    guess). ;-)
    I'm looking at the code now, and it seems clear enough to me.

    Class SelectItemsTag has this comment:
    // Generated from class
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems.

    This is part of our automatic-tag-generation: generally, tag classes are
    generated from annotations on the component class. So the component
    class is AbstractUISelectItems.

    And AbstractUISelectItems has:
    public Object getValue() {
    Object value = super.getValue();
    return createSelectItems(value);
    }

    Method createSelectItems contains:
    List items = new ArrayList();
    if (value instanceof SelectItem[]) {
    return (SelectItem[]) value;
    }
    else if (value instanceof Collection) {
    ....
    }
    else if (value instanceof Map) {
    Map map = (Map) value;
    for (Iterator iter = map.entrySet().iterator(); iter.hasNext();)
    {
    Entry currentItem = (Entry) iter.next();
    putIteratorToRequestParam(currentItem.getValue());
    SelectItem selectItem = createSelectItem();
    removeIteratorFromRequestParam();
    items.add(selectItem);
    }
    }
    return (SelectItem[]) items.toArray(new SelectItem[0]);


    So clearly, when evaluating the value expression returns a Map, then the
    component returns an array of SelectItem objects that have been built
    from the map values.

    This code is from the current trunk. Unfortunately due to svn
    rearrangements in Sept 2008 when the new tomahawk build framework was
    introduced, it isn't easy to track svn history for components anymore.
    However I'm pretty sure this is not a new feature. It certainly was
    there in september 2008, and you say you are using 1.1.8 which was
    released in November 2008.

    You say you are using tomahawk-1.1.8, which is the latest release. Are
    you sure about that?

    However from memory I'm pretty sure this is not a new feature...

    Regards,
    Simon
  • Cagatay Civici at Mar 14, 2009 at 11:37 pm
    I remember adding this feature in 2006 :)

    If you have a map you actually don't need t:selectItems much.
    f:selectItems would suffice where labels are the keys and values are
    the map values.

    t:selectItems is handy to autogenerate selectItems from collections
    like lists.

    Cagatay
    On Mar 14, 2009, at 10:41 PM, Simon Kitching wrote:

    Hi Johannes,
    On Sat, 2009-03-14 at 19:10 +0000, Johannes Ruthenberg wrote:

    Simon Kitching wrote on 14.03.2009 15:39:
    Have you tried this? <t:selectItems value="#{myBean.allRoles}"/>
    I have, but I get a NullPointerException if I do.

    Caused by: java.lang.NullPointerException
    at
    org.apache.catalina.connector.Request.getAttribute(Request.java:877)
    at
    org
    .apache
    .catalina.connector.RequestFacade.getAttribute(RequestFacade.java:
    263)
    at
    javax
    .servlet
    .ServletRequestWrapper.getAttribute(ServletRequestWrapper.java:82)
    at
    org
    .apache
    .myfaces.context.servlet.RequestMap.getAttribute(RequestMap.java:47)
    at
    org
    .apache
    .myfaces.util.AbstractAttributeMap.put(AbstractAttributeMap.java:104)
    at
    org
    .apache
    .myfaces.util.AbstractAttributeMap.put(AbstractAttributeMap.java:38)
    at
    org
    .apache
    .myfaces
    .custom
    .selectitems
    .AbstractUISelectItems
    .putIteratorToRequestParam(AbstractUISelectItems.java:131)
    at
    org
    .apache
    .myfaces
    .custom
    .selectitems
    .AbstractUISelectItems.createSelectItems(AbstractUISelectItems.java:
    107)
    at
    org
    .apache
    .myfaces
    .custom
    .selectitems
    .AbstractUISelectItems.getValue(AbstractUISelectItems.java:75)
    at
    org
    .apache
    .myfaces
    .shared_impl
    .util.SelectItemsIterator.hasNext(SelectItemsIterator.java:128)
    at
    org
    .apache
    .myfaces
    .shared_impl
    .renderkit
    .RendererUtils.internalGetSelectItemList(RendererUtils.java:557)
    at
    org
    .apache
    .myfaces
    .shared_impl
    .renderkit.RendererUtils.getSelectItemList(RendererUtils.java:542)
    at
    org
    .apache
    .myfaces
    .shared_impl
    .renderkit
    .html
    .HtmlCheckboxRendererBase
    .renderCheckboxList(HtmlCheckboxRendererBase.java:117)
    at
    org
    .apache
    .myfaces
    .shared_impl
    .renderkit
    .html
    .HtmlCheckboxRendererBase.encodeEnd(HtmlCheckboxRendererBase.java:65)
    at
    javax
    .faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:624)
    ... 76 more

    Looking at the actual implementation of the t:selectItems tag, I
    think
    this is consistent (it sets the current map value to the request
    under
    the provided variable name and then calls a method which retrieves
    this
    and tries to get the label and value for the select item -- this
    fails
    if no variable name is given). It just doesn't fit what the tag is
    supposed to do as per the documentation, at least as I understand it.

    I could simply built the list of SelectItem instances myself and set
    this in my bean. But I actually wouldn't want to handle more of the
    internal UI elements than necessary, so if the tag could do this, I
    think that would be the better approach. I also wonder if the
    documentation is wrong here or the tag class (a matter of
    perspective, I
    guess). ;-)
    I'm looking at the code now, and it seems clear enough to me.

    Class SelectItemsTag has this comment:
    // Generated from class
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems.

    This is part of our automatic-tag-generation: generally, tag classes
    are
    generated from annotations on the component class. So the component
    class is AbstractUISelectItems.

    And AbstractUISelectItems has:
    public Object getValue() {
    Object value = super.getValue();
    return createSelectItems(value);
    }

    Method createSelectItems contains:
    List items = new ArrayList();
    if (value instanceof SelectItem[]) {
    return (SelectItem[]) value;
    }
    else if (value instanceof Collection) {
    ....
    }
    else if (value instanceof Map) {
    Map map = (Map) value;
    for (Iterator iter = map.entrySet().iterator();
    iter.hasNext();)
    {
    Entry currentItem = (Entry) iter.next();
    putIteratorToRequestParam(currentItem.getValue());
    SelectItem selectItem = createSelectItem();
    removeIteratorFromRequestParam();
    items.add(selectItem);
    }
    }
    return (SelectItem[]) items.toArray(new SelectItem[0]);


    So clearly, when evaluating the value expression returns a Map, then
    the
    component returns an array of SelectItem objects that have been built
    from the map values.

    This code is from the current trunk. Unfortunately due to svn
    rearrangements in Sept 2008 when the new tomahawk build framework was
    introduced, it isn't easy to track svn history for components anymore.
    However I'm pretty sure this is not a new feature. It certainly was
    there in september 2008, and you say you are using 1.1.8 which was
    released in November 2008.

    You say you are using tomahawk-1.1.8, which is the latest release. Are
    you sure about that?

    However from memory I'm pretty sure this is not a new feature...

    Regards,
    Simon
  • Simon Kitching at Mar 15, 2009 at 10:58 am

    On Sun, 2009-03-15 at 01:37 +0200, Cagatay Civici wrote:
    I remember adding this feature in 2006 :)

    If you have a map you actually don't need t:selectItems much.
    f:selectItems would suffice where labels are the keys and values are
    the map values.
    How would you do that?

    The f:selectItems docs is clear that it expects an array or list of
    SelectItem objects. So there appears no way to generate SelectItems
    automatically.

    Do you mean that the backing bean would provide a special hand-written
    method to create a SelectItem element for each (key,value) in the map?
    That certainly works, but is not quite as nice as having t:selectItems
    do that for you.

    Regards,
    Simon
  • Johannes Ruthenberg at Mar 15, 2009 at 1:28 pm
    Hi everyone!

    First, a big thank you to Cagatay! I was about to agree with Simon that
    f:selectItems couldn't do this (judging from the documentation), but it
    seems that I didn't even try it without the other attributes. I just
    tried it out and this actually works like expected (selectedRoles being
    a Long[] and allRoles a Map<String, Long>):

    <h:selectManyCheckbox id="selectedRoles" value="#{myBean.selectedRoles}">
    <f:selectItems value="#{myBean.allRoles}"/>
    </h:selectManyCheckbox>

    The same with t:selectItems does not work, even if the documentation
    states that it should. ;-)

    Simon Kitching wrote on 14.03.2009 20:41:
    Method createSelectItems contains:
    List items = new ArrayList();
    if (value instanceof SelectItem[]) {
    return (SelectItem[]) value;
    }
    else if (value instanceof Collection) {
    ....
    }
    else if (value instanceof Map) {
    Map map = (Map) value;
    for (Iterator iter = map.entrySet().iterator(); iter.hasNext();)
    {
    Entry currentItem = (Entry) iter.next();
    putIteratorToRequestParam(currentItem.getValue());
    SelectItem selectItem = createSelectItem();
    removeIteratorFromRequestParam();
    items.add(selectItem);
    }
    }
    return (SelectItem[]) items.toArray(new SelectItem[0]);


    So clearly, when evaluating the value expression returns a Map, then the
    component returns an array of SelectItem objects that have been built
    from the map values.
    It should do that, but I think building the select items from the map is
    either broken or the documentation states too much. As you can see in
    above code, only the value of the map entry is used at all, the key
    isn't. This:

    putIteratorToRequestParam(currentItem.getValue());
    SelectItem selectItem = createSelectItem();
    removeIteratorFromRequestParam();


    only works if a variable name is set (var attribute), otherwise you get
    the NullPointerException I included in my last mail. If you set a var
    name, then the createSelectItem method creates a select item. It only
    has the map value to work with, though. You could put an object in the
    map as value and do something like label="#{myObject.name}"
    value="#{myObject.value}". But I don't see a way to use the map's key
    explicitely, and there's no mechanisms that detects the missing var
    attribute and just uses the keys and values of the map as default
    strategy (as far as I can see).

    If f:selectItems can do this and the documentation of t:selectItems says
    this is possible, I think t:selectItems should be able to do this as
    well. Personally, I'm happy to use f:selectItems, but if you agree I
    could create a bug report for this. I just did try this with the latest
    nightly snapshot of tomahawk12-1.1.9 and see the same behavior there,
    too (the NullPointerException if used withut other attributes).

    And in any case thanks to both Simon and Cagatay for answering my mails
    and providing me with the needed input to solve the problem! ;-)

    Greetings,
    Johannes
  • Simon Kitching at Mar 15, 2009 at 5:47 pm

    On Sun, 2009-03-15 at 13:28 +0000, Johannes Ruthenberg wrote:
    Hi everyone!

    First, a big thank you to Cagatay! I was about to agree with Simon that
    f:selectItems couldn't do this (judging from the documentation), but it
    seems that I didn't even try it without the other attributes. I just
    tried it out and this actually works like expected (selectedRoles being
    a Long[] and allRoles a Map<String, Long>):

    <h:selectManyCheckbox id="selectedRoles" value="#{myBean.selectedRoles}">
    <f:selectItems value="#{myBean.allRoles}"/>
    </h:selectManyCheckbox>

    The same with t:selectItems does not work, even if the documentation
    states that it should. ;-)
    Hmm..interesting.

    This behaviour of building a list of SelectItems from a Map is actually
    implemented in the SelectItemsIterator class.

    (1) I'm not sure that this is standard JSF behaviour, ie I am not sure
    that code depending on this will run on any JSF implementation other
    than MyFaces.

    (2) By adding custom Map handing inside the t:selectItems component,
    this prevents the SelectItemsIterator functionality from working. The
    SelectItemsIterator class never sees that the "value" is of type Map
    because the t:selectItems has already replaced the map with a list of
    SelectItems it has built itself.

    The situation seems really messy to me, but it's not clear what the best
    solution would be. I have created a JIRA issue for this:
    https://issues.apache.org/jira/browse/TOMAHAWK-1403

    Thanks for reporting this Johannes, and sorry you struck such an odd
    issue shortly after starting with JSF!

    Regards,
    Simon
  • EDHEDH at Apr 4, 2010 at 4:04 pm
    I'm replying because I have a related problem. Hope somebody can help.

    I'm using the following code (it's part of a for each loop in which a vary
    the unDimDefSid variable).

    <ice:selectOneListbox id="#{unDimDefSid}"
    value="#{addBinDimRelActionBean.mapOfSelectedUnDims[unDimDefSid]}">
    <f:selectItems
    value="#{addBinDimRelActionBean.mapOfUnDimSelectItems[unDimDefSid]}" />
    </ice:selectOneListbox>

    The listboxes are displayed correctly with the values from the appropriate
    map entry determined by the value of the unDimDefSid variable. That is, the
    f:selectItems is working appropriately.

    However the expression in the value= attribute doesn't work as expected.
    On submitting the form (commandbutton) I would have expected the selected
    value to be added to the map at the specified (value of unDimDefSid
    variable) key.
    However I get a jsf el propertynotfoundexception, target unreachable,
    exception instead.

    Is it impossible to put the selected value of a listbox to specific map
    position ?
    If not, how should it be done ?
    If I have many listboxes that are created as above in the same for each
    loop. Can they all write to the same back map (at a different key) ?

    The reason behind all this I have an unknown number of listboxes to display.
    That is, the number of listboxes and the values they contain is determined
    at runtime.

    Any advice is welcome.

    Thanks,
    EDH


    Johannes Ruthenberg-2 wrote:
    Hi!

    I'm new to this list and to JSF in general. I have a problem with the
    t:selectItems component and would be grateful for any advice. I'm using
    MyFaces 1.2.5, Tomahawk 1.1.8 and Facelets.

    I have this in my file:

    <h:selectManyCheckbox id="selectedRoles" value="#{myBean.selectedRoles}"
    layout="pageDirection">
    <t:selectItems value="#{myBean.allRoles}" var="role"
    itemLabel="#{role}" itemValue="#{role}" />
    </h:selectManyCheckbox>

    selectedRoles is a Long[] array, allRoles is a Map<String, Long>,
    mapping the role name to their ID. What I want is the ID as value of the
    checkbox and the name as label text. Above code generates the ID as both
    the value and the label text though.

    Now page [1] has a more detailed explanation of the possibilities of the
    value attribute than [2], but I did check the source code of
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems and it seems
    to be the same for both the JSF 1.1 and 1.2 version of the component.

    [1]
    http://myfaces.apache.org/tomahawk-project/tomahawk/tagdoc/t_selectItems.html
    [2]
    http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_selectItems.html

    The documentation under [1] says:

    value: An EL expression that specifies the contents of the selection
    list. The expression can refer to one of the following:

    1. A single SelectItem
    2. An array or Collection of SelectItem instances
    3. A Map. The contents of the Map are used to create SelectItem
    instances, where the SelectItem's label is the map's key value, and the
    SelectItem's value is the map's value. When using a map, it is
    recommended that an ordered implementation such as java.util.TreeMap is
    used.

    Number 3 is exactly what I want, I'm even using a TreeMap. I would
    expect to get the keys as labels and the values as checkbox values
    automatically with just using <t:selectItems
    value="#{myBean.allRoles}"/>. But looking in the source code, I don't
    see anything to provide this functionality. Unfortunately I also don't
    see a way to configure the component with the other attributes to do
    this, since only the value of the Map.Entry is set as request attribute.
    Meaning, I can't just do something like:

    <t:selectItems value="#{myBean.allRoles}" var="mapEntry"
    itemLabel="#{mapEntry.key}" itemValue="#{mapEntry.value}" />

    So, am I missing something here? Is there a way to do this? Or is the
    documentation simply wrong?

    Regards,
    Johannes Ruthenberg
    --
    View this message in context: http://old.nabble.com/t%3AselectItems-with-a-map-tp22504143p28133463.html
    Sent from the MyFaces - Users mailing list archive at Nabble.com.
  • Jakob Korherr at Apr 6, 2010 at 9:44 am
    Hi Edwin,

    I am pretty sure your problem exists, because you are using the for each
    loop. You see, when JSF renders the page unDimDefSid is set in the request
    scope for every loop and thus JSF is able to get the right value from the
    ValueExpression, because both addBinDimRelActionBean.mapOfUnDimSelectItems
    and unDimDefSid can be resolved.

    However when setting the value I think the problem is that unDimDefSid
    cannot be resolved (but frankly I don't know if this is a JSF bug/problem)
    and so JSF cannot set the value, because basically setting the value in a
    Map is possible.

    Have you tried using a t:dataTable to render the input fields instead of the
    for each loop?

    Regards,
    Jakob

    2010/4/4 EDHEDH <edwin.dhondt@hp.com>
    I'm replying because I have a related problem. Hope somebody can help.

    I'm using the following code (it's part of a for each loop in which a vary
    the unDimDefSid variable).

    <ice:selectOneListbox id="#{unDimDefSid}"
    value="#{addBinDimRelActionBean.mapOfSelectedUnDims[unDimDefSid]}">
    <f:selectItems
    value="#{addBinDimRelActionBean.mapOfUnDimSelectItems[unDimDefSid]}" />
    </ice:selectOneListbox>

    The listboxes are displayed correctly with the values from the appropriate
    map entry determined by the value of the unDimDefSid variable. That is, the
    f:selectItems is working appropriately.

    However the expression in the value= attribute doesn't work as expected.
    On submitting the form (commandbutton) I would have expected the selected
    value to be added to the map at the specified (value of unDimDefSid
    variable) key.
    However I get a jsf el propertynotfoundexception, target unreachable,
    exception instead.

    Is it impossible to put the selected value of a listbox to specific map
    position ?
    If not, how should it be done ?
    If I have many listboxes that are created as above in the same for each
    loop. Can they all write to the same back map (at a different key) ?

    The reason behind all this I have an unknown number of listboxes to
    display.
    That is, the number of listboxes and the values they contain is determined
    at runtime.

    Any advice is welcome.

    Thanks,
    EDH


    Johannes Ruthenberg-2 wrote:
    Hi!

    I'm new to this list and to JSF in general. I have a problem with the
    t:selectItems component and would be grateful for any advice. I'm using
    MyFaces 1.2.5, Tomahawk 1.1.8 and Facelets.

    I have this in my file:

    <h:selectManyCheckbox id="selectedRoles" value="#{myBean.selectedRoles}"
    layout="pageDirection">
    <t:selectItems value="#{myBean.allRoles}" var="role"
    itemLabel="#{role}" itemValue="#{role}" />
    </h:selectManyCheckbox>

    selectedRoles is a Long[] array, allRoles is a Map<String, Long>,
    mapping the role name to their ID. What I want is the ID as value of the
    checkbox and the name as label text. Above code generates the ID as both
    the value and the label text though.

    Now page [1] has a more detailed explanation of the possibilities of the
    value attribute than [2], but I did check the source code of
    org.apache.myfaces.custom.selectitems.AbstractUISelectItems and it seems
    to be the same for both the JSF 1.1 and 1.2 version of the component.

    [1]
    http://myfaces.apache.org/tomahawk-project/tomahawk/tagdoc/t_selectItems.html
    [2]
    http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_selectItems.html
    The documentation under [1] says:

    value: An EL expression that specifies the contents of the selection
    list. The expression can refer to one of the following:

    1. A single SelectItem
    2. An array or Collection of SelectItem instances
    3. A Map. The contents of the Map are used to create SelectItem
    instances, where the SelectItem's label is the map's key value, and the
    SelectItem's value is the map's value. When using a map, it is
    recommended that an ordered implementation such as java.util.TreeMap is
    used.

    Number 3 is exactly what I want, I'm even using a TreeMap. I would
    expect to get the keys as labels and the values as checkbox values
    automatically with just using <t:selectItems
    value="#{myBean.allRoles}"/>. But looking in the source code, I don't
    see anything to provide this functionality. Unfortunately I also don't
    see a way to configure the component with the other attributes to do
    this, since only the value of the Map.Entry is set as request attribute.
    Meaning, I can't just do something like:

    <t:selectItems value="#{myBean.allRoles}" var="mapEntry"
    itemLabel="#{mapEntry.key}" itemValue="#{mapEntry.value}" />

    So, am I missing something here? Is there a way to do this? Or is the
    documentation simply wrong?

    Regards,
    Johannes Ruthenberg
    --
    View this message in context:
    http://old.nabble.com/t%3AselectItems-with-a-map-tp22504143p28133463.html
    Sent from the MyFaces - Users mailing list archive at Nabble.com.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupusers @
categoriesmyfaces
postedMar 13, '09 at 8:06p
activeApr 6, '10 at 9:44a
posts10
users5
websitemyfaces.apache.org

People

Translate

site design / logo © 2019 Grokbase