SelectOneMenuから選択したアイテムを取得できません。メニューにArrayListを指定し、ユーザーにその1つを選択してもらいたい。メニューをフォームに入れたので、選択を実行するために使用することを意図したcommandButtonがあります。この実装では、次のエラーが発生します。メニュー「user3」から選択すると、タイプクラスjava.lang.Stringのuser3をクラスjava.util.ArrayListに変換できないため、実際に正しく選択が実行されます。エラーはこの行を参照しています
<h:selectOneMenu value="#{user.myUsers}"
これがselectOneMenuを生成する私のxhtmlの一部です。
<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;
}
}