JSF SelectOneMenu选中的项目

时间:2012-06-08 22:03:12

标签: jsf selectonemenu

我无法从SelectOneMenu中获取所选项目。我向菜单提供了一个ArrayList,并希望用户选择其中一个。我把菜单放到一个表单中,所以我有一个commandButton,我打算用它来执行选择。这个实现给了我这个错误:当我从菜单“user3”中选择时,无法将类型为java.lang.String的user3转换为类java.util.ArrayList,因此它实际上正确地执行了选择。错误引用此行

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

以下是我的xhtml中生成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>

这是我的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;
}
}

2 个答案:

答案 0 :(得分:9)

你的arraylist映射到

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

并在选择之后,您尝试将所选值放在同一列表中:

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

您的managed bean视频中应该有一些对象(或您的字符串中的字符串)与您的视图相关联,并按myUsers的所选项填充。例如:

private String selectedUser; // + appropriate getter and setter

<h:selectOneMenu>应如下所示:

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

所选项目应存储在selectedUser到jsf生命周期

的末尾

答案 1 :(得分:-2)

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