【问题标题】:EL calls remove(int i) instead of remove(Object o) [duplicate]EL 调用 remove(int i) 而不是 remove(Object o) [重复]
【发布时间】:2015-01-18 15:06:21
【问题描述】:

在我的 JSF 页面中,我试图从集合中删除一个元素。我认为页面没有调用Collection.remove(Object o) 方法,而是调用Vector.remove(int i)

更新: tagsCollection 的类型是 org.eclipse.persistence.indirection.IndirectList

更新:它给出了与 Vector 相同的异常

使用下面的代码,我得到以下错误:

java.lang.IllegalArgumentException:无法转换 com.question.entities.Tags[ tagId=12 ] 类型类 com.question.entities.Tags 到 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:无法转换类型类的 true java.lang.Boolean 转 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;
    }

}

【问题讨论】:

  • 它是一个“标签”对象。
  • 当您在tagsCollection 之后键入点时,您可以使用哪些删除方法?
  • @SujanSivagurunathan 只有 remove(Object o) 可用。
  • 非常有趣。我昨天在我的个人笔记本电脑上使用 JDK 8 进行了尝试,并且遇到了你描述的同样的问题。现在,我已经尝试使用 jdk1.7.0_45 和 GF 4.0.0 build 89 并且工作正常。可能是jvm问题?
  • @Salih 为什么要将 Collection 转换为 Vector 然后在 getter 中返回 Collection ?无论如何,我发布的解决方法有效..你能确认吗?

标签: jsf jsf-2 el


【解决方案1】:

说实话,我不知道 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);
}

【讨论】:

  • 我已经知道了,我可以使用 backing bean 方法来做到这一点。我想直接做,不需要额外的方法。
  • @SalihErikci tagsCollection 是什么类型?
  • 我不知道它到底是什么类型。它由 JPA 实例化。它被定义为私有 Collection tagsCollection;
  • @SalihErikci 你应该知道tagsCollection 的类型。确保将其声明为 List(尽管它在幕后使用了 Collection 实现)
  • 我为什么要知道?我知道这是一个集合。 (它是这样声明的。)它应该调用正确的 remove() 方法。是List还是Set有关系吗?
猜你喜欢
  • 1970-01-01
  • 2018-10-27
  • 2013-05-27
  • 2011-02-23
  • 1970-01-01
  • 2020-06-05
  • 2022-12-27
  • 2022-12-28
  • 1970-01-01
相关资源
最近更新 更多