简体   繁体   English

遍历从JSF页面内的查询获得的实体的ArrayList

[英]Iterate over ArrayList of Entities obtained from a query within a JSF page

I have a managed bean as my View in which I have a method called List<ArrayList> getImages() where I query the database and get a List of entities which is returned by the method. 我有一个托管bean作为View,其中有一个名为List<ArrayList> getImages() ,在其中查询数据库并获取由该方法返回的实体列表。 All well and good. 一切都很好。

My problem is that when I try to iterate over this List from with JSF using either <c:forEach or ui:repeat eg <c:forEach var="image" items="#{viewBean.images}"> the server, Tomee throws and exception java.lang.UnsupportedOperationException: Result lists are read-only . 我的问题是,当我尝试使用<c:forEachui:repeat从JSF遍历此列表时,例如<c:forEach var="image" items="#{viewBean.images}">服务器Tomee引发并抛出异常java.lang.UnsupportedOperationException: Result lists are read-only and I'm not even doing anything with the values at this point. 而且我现在甚至都不用这些值做任何事情。

If I just return the ArrayList with simple objects, no problem. 如果我只返回带有简单对象的ArrayList,就没有问题。 I understand it must be something to do with the fact the object is an entity therefore tied to the database but I'm not sure the correct way, or best practice, to return the what I need to the JSP page. 我知道这一定与对象是一个与数据库绑定的实体有关,但是我不确定将我需要的东西返回到JSP页面的正确方法或最佳实践。

Thanks. 谢谢。 Jason. 杰森。

Edit. 编辑。 Below is method used for retrieving objects from db for iteration in JSF. 以下是用于从db中检索对象以在JSF中进行迭代的方法。

public List<ProfileImage> getProfileImageList() { profileImageList = facade.findAllByProfileId(1L); while (profileImageList.size() < 4) { // Add placeholders to bring the list size to 4 ProfileImage placeHolder = new ProfileImage(); placeHolder.setProfileId(1L); profileImageList.add(placeHolder); } return Collections.unmodifiableList(profileImageList); }

JSF snippet below : Note, I am not doing anything with the value of var for now 下面的JSF代码段:注意,我暂时不使用var的值做任何事情

<ui:repeat value="${imageUploadView.profileImageList}" var="profileImage">
    <p:commandButton id="imageBtn_1" value="Picture 1" type="button" />
    <p:overlayPanel id="imagePanel_1" for="imageBtn_1" hideEffect="fade" >
        <ui:include src="/WEB-INF/xhtml/includes/profile_imageupload.xhtml" />
    </p:overlayPanel>
</ui:repeat>

The following error is generated 产生以下错误

javax.el.ELException: Error reading 'profileImageList' on type com.goobang.view.ImageUploadView

viewId=/profile/create_profile.xhtml
location=/Users/xxxxxxxxx/Documents/NetBeansProjects/testmaven/target/testmaven-1.0-SNAPSHOT/profile/create_profile.xhtml
phaseId=RENDER_RESPONSE(6)

Caused by:
java.lang.UnsupportedOperationException - Result lists are read-only.
at org.apache.openjpa.lib.rop.AbstractResultList.readOnly(AbstractResultList.java:44)

/profile/create_profile.xhtml at line 16 and column 87 value="${imageUploadView.profileImageList}"

I have solved it. 我已经解决了 The exception is thrown because I am modifying the list after assigning it to the result set. 抛出异常是因为我在将列表分配给结果集之后正在修改列表。 If I simply return the result set all is fine. 如果我只返回结果集,一切都很好。 So to achieve what I intended in getProfileImageList() I created a new ArrayList from the original, as suggested by tt_emrah , and then modify that before returning it. 因此,为了实现我在getProfileImageList()中的预期目的,我按照tt_emrah的建议从原始对象创建了一个新的ArrayList,然后在返回之前对其进行修改。

public List<ProfileImage> getProfileImageList() {
    profileImageList = new ArrayList(facade.findAllByProfileId(1L));
    while (profileImageList.size() < 4) { // Add placeholders to bring the list size to 4 
        ProfileImage placeHolder = new ProfileImage();
        placeHolder.setProfileId(1L);
        profileImageList.add(placeHolder);
    }
    return profileImageList;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM