Pio
Pio

Reputation: 4064

JSF SelectOneMenu selected item

I can't get the selected item from a SelectOneMenu. I supply an ArrayList to the menu and want the user to select one of it. I put the menu into a form, so I have a commandButton which I intended to use to perform the selection. This implementation gives me this error : Cannot convert user3 of type class java.lang.String to class java.util.ArrayList when I select from the menu "user3", so it actually performs the selection correctly. The error refers to this line

<h:selectOneMenu value="#{user.myUsers}"

Here is the part of my xhtml which generates the selectOneMenu.

 <h:panelGrid columns="3">


                    <h:form>
                    <h:selectOneMenu value="#{user.myUsers}">
                        <f:selectItems value="#{user.myUsers }"/>
                    </h:selectOneMenu>
                    <h:commandButton value="#{msgs.remove_user}" action="#{user.select }" ></h:commandButton>
                    </h:form>
                    <h:outputText value="#{ user.select}"></h:outputText>
            </h:panelGrid>

And here is my UserBean:

 @ManagedBean(name="user")
 @SessionScoped
public class UserBean implements Serializable {

private String selected;


public ArrayList<String> getMyUsers()throws Exception
{
    ArrayList<String> ret;
    MySQLConnection conn = new MySQLConnection();
    try{
        ret = conn.getMyUsers(name);
    }finally
    {
        conn.closeConnection();
    }
    return ret;
}

public String getSelect() throws Exception
{
     if (this.selected==null) return this.getMyUsers().get(0);

     return this.selected;  
}


public void setSelect(String s)
{
    this.selected = s;
}
}

Upvotes: 1

Views: 30638

Answers (2)

bhaskar ettem
bhaskar ettem

Reputation: 1

<table class="tableClass" id="productDescriptionTable">
    <thead>
        <tr class="trPDClass">
            <th class="thPDClass"></th>
            <th class="thPDClass">Feature</th>
            <th class="thPDClass">SubFeature</th>
            `enter code here`<th class="thPDClass">Type</th>
            <th class="thPDClass">Sub-Feature Value</th>
            `enter code here`<th class="thPDClass">Is Active</th>
            <th class="thPDClass">Deleted</th>
        </tr>
    </thead>
    <tbody>
        <tr class="trPDClass">
            <td class="tdPDClass" style="width: 30;" ><input type="checkbox" /></td>
            <td class="tdPDClass"><input type="text" id="0PDfeature" name="PDfeature" /></td>
            <td class="tdPDClass"><input type="text" id="0PDsubFeature" name="PDsubFeature" /></td>
            <td class="tdPDClass"><input type="text" id="0PDtype" name="PDtype" /></td>
            <td class="tdPDClass">
                <div id="0PDsubFeatureValueDiv" name="PDsubFeatureValueDiv"></div>
            </td>
            <td class="tdPDClass">
                <table class="radioClass">
                    <tr>
                        <td width="24%"><input type="radio" name="PDisActive" id="PDisActiveY" value="Y"  /></td>
                        <td width="40%">Yes</td>
                        <td width="20%"><input type="radio" name="PDisActive" id="PDisActiveN" value="N"  /></td>
                        <td width="16%">No</td>
                    </tr>
                </table>
            </td>
            <td class="tdPDClass">
                <table class="radioClass">
                    <tr>
                        <td width="24%"><input type="radio" name="PDdeleted" id="0PDdeletedY" value="Y"  /></td>
                        <td width="40%">Yes</td>
                        <td width="20%"><input type="radio" name="PDdeleted" id="0PDdeletedN" value="N"  /></td>
                        <td width="16%">No</td>
                    </tr>
                </table>
            </td>
        </tr>
    </tbody>
</table>

Upvotes: -4

gaffcz
gaffcz

Reputation: 3479

Your arraylist is mapped to

<f:selectItems value="#{user.myUsers}"/>, 

and after selection you are trying to put the selected value to the same list:

<h:selectOneMenu value="#{user.myUsers}">

You should have some object (or string in your case) in your managed bean linked with your view and fill it by selected item of myUsers. For example:

private String selectedUser; // + appropriate getter and setter

and <h:selectOneMenu> should look like:

<h:selectOneMenu value="#{user.selectedUser}">

selected item sould be stored in selectedUser to the end of jsf lifecycle

Upvotes: 9

Related Questions