2014-11-20 48 views
3

在我的JSF頁面中,我試圖從集合中移除一個元素。而不是調用Collection.remove(Object o)方法,我認爲頁面調用Vector.remove(int i)EL調用remove(int i)而不是remove(Object o)

更新: tagsCollection是org.eclipse.persistence.indirection.IndirectList

更新的類型:它給出了矢量

同一異​​常

有了下面的代碼我得到以下錯誤:

java.lang.IllegalArgumentException: Cannot convert com.question.entities.Tags[ tagId=12 ] of type class com.question.entities.Tags to int

<ui:repeat value="#{backingBean.question.tagsCollection}" var="tag" > 
    <li> 
     <span>#{tag.tagTitle}</span> 
     <h:commandButton> 
      <f:ajax event="click" listener="#{backingBean.question.tagsCollection.remove(tag)}" render="@form" execute="@form"/> 
     </h:commandButton> 
    </li> 
</ui:repeat> 

更新:這是可以生成異常的最小代碼。它拋出以下異常:

java.lang.IllegalArgumentException: Cannot convert true of type class java.lang.Boolean to int

的index.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
     xmlns:f="http://xmlns.jcp.org/jsf/core"> 
    <h:head> 
     <title>Facelet Title</title> 
    </h:head> 
    <h:body> 
     <h:form id="form"> 
      <ui:repeat value="#{backingBean.myList}" var="tag"> 
       #{tag.booleanValue()} 
       <h:commandButton value="Delete"> 
        <f:ajax listener="#{backingBean.myList.remove(tag)}" execute="@form" render="@form"/> 
       </h:commandButton> 
      </ui:repeat> 
     </h:form> 
    </h:body> 
</html> 

BackingBean.java

@Named 
@ViewScoped 
public class BackingBean implements Serializable { 

    private Collection<Boolean> myList = new Vector<Boolean>(); 

    public BackingBean() { 
     myList.add(true); 
     myList.add(false); 
     myList.add(true); 

    } 

    public Collection<Boolean> getMyList() { 
     return myList; 
    } 

    public void setMyList(Collection<Boolean> myList) { 
     this.myList = myList; 
    } 

} 
+0

「tag」是什麼類型? – kolossus 2014-11-20 21:06:11

+0

這是一個「標籤」對象。 – 2014-11-20 21:16:00

+0

當您在'tagsCollection'後輸入點時,您可以使用哪些刪除方法? – 2014-11-20 23:43:02

回答

0

說實話,我不知道JSF如何處理這在後臺,但爲了避免它只是調用你自己的bean方法,也是一樣的

<f:ajax event="click" listener="#{backingBean.removeTag(backingBean.question, tag)}" render="@form" execute="@form"/> 

public void removeTag(Question question, Tag tag) { 
    question.getTagsCollection().remove(tag); 
} 
+0

我已經知道,我可以用支持bean的方法來做到這一點。我想直接做,沒有額外的方法。 – 2014-11-20 16:52:01

+0

@SalihErikci'tagsCollection'是什麼類型? – 2014-11-20 16:52:49

+0

我不知道它是什麼類型。它由JPA實例化。它被定義爲私人收藏 tagsCollection; – 2014-11-20 17:11:17