1

我无法从 SelectOneMenu 中获取所选项目。我为菜单提供了一个 ArrayList,并希望用户选择其中一个。我将菜单放入一个表单中,所以我有一个我打算用来执行选择的命令按钮。这个实现给了我这个错误:当我从菜单“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;
}
}
4

2 回答 2

9

您的数组列表映射到

<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 生命周期结束

于 2012-06-08T23:36:58.483 回答
-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>
于 2012-10-05T11:11:42.453 回答