如何从列表中删除bean数据

时间:2012-07-08 11:35:25

标签: collections jsf-2 arraylist javabeans

我有DrawingBean作为

public class DrawingBean {
    String drawingTitle;

    public String getDrawingTitle() {
        return drawingTitle;
    }

    public void setDrawingTitle(String drawingTitle) {
        this.drawingTitle = drawingTitle;
    }
}

在JSF中,我有一个页面drawingPatentss.xhtml,其中包含h:form标记内的下方。

<h:inputText value="#{PersonalInformationDataBean.drawingTitle}" size="80" id="drawingTitle" />
<h:commandButton value="Upload More Drawing" action="# {PersonalInformationDataBean.checkMyDrawingPageData()}" />

<h:dataTable id="patentDrawingList" var="patentDrawingList" value="#{PersonalInformationDataBean.drawingList}"  border="1" width="30%">
<h:column>
<f:facet name="header">
<h:outputText value="Drawing Title" />
</f:facet>
<h:outputText value="#{patentDrawingList.drawingTitle}"/> 
<h:commandButton value="Delete" action="#{PersonalInformationDataBean.removeDataFromDrawingList()}">
<f:setPropertyActionListener target="#{PersonalInformationDataBean.drawingTitle}" value="#{patentDrawingList.drawingTitle}" />
</h:commandButton>
</h:column>
</h:dataTable>

PersonalInformationDataBean.java我有

private List<DrawingBean> drawingList = new ArrayList<DrawingBean>();
private String drawingTitle;
DrawingBean drawingBean;
// getter and setter for all of above.

public String checkMyDrawingPageData() {
drawingBean = new DrawingBean();
drawingBean.setDrawingTitle(drawingTitle);
drawingList.add(drawingBean);
return "drawingPatentss?faces-redirect=true";
}

public String removeDataFromDrawingList() {
    drawingBean = new DrawingBean();
    drawingBean.setDrawingTitle(drawingTitle);
    boolean removedStatus = false;
    removedStatus = drawingList.remove(drawingBean);
    System.out.println("removed status === " + removedStatus);
    return "drawingPatentss?faces-redirect=true";
}

然而,bean没有从列表中删除......

但是,如果我在removeDataFromDrawingList()中添加以下行,则列表会更新。

    drawingBean = new DrawingBean();
    drawingBean.setDrawingTitle("Temp Title");
    drawingList.add(drawingBean);

请求bean没有从我所拥有的列表中删除的任何原因。

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。我不得不使用Iterator课程。下面的代码就是诀窍......

drawingBean = new DrawingBean();
drawingBean.setDrawingTitle(drawingTitle);
boolean removedStatus = false;

for (Iterator itr=drawingList.iterator();itr.hasNext();) {
    drawingBean = (DrawingBean) itr.next();
    if (drawingBean.getDrawingTitle().equals(drawingTitle)) {
        itr.remove();
        removedStatus = true;
    }
}

Reference