繁体   English   中英

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

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

我有一个托管bean作为View,其中有一个名为List<ArrayList> getImages() ,在其中查询数据库并获取由该方法返回的实体列表。 一切都很好。

我的问题是,当我尝试使用<c:forEachui:repeat从JSF遍历此列表时,例如<c:forEach var="image" items="#{viewBean.images}">服务器Tomee引发并抛出异常java.lang.UnsupportedOperationException: Result lists are read-only 而且我现在甚至都不用这些值做任何事情。

如果我只返回带有简单对象的ArrayList,就没有问题。 我知道这一定与对象是一个与数据库绑定的实体有关,但是我不确定将我需要的东西返回到JSP页面的正确方法或最佳实践。

谢谢。 杰森。

编辑。 以下是用于从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代码段:注意,我暂时不使用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>

产生以下错误

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}"

我已经解决了 抛出异常是因为我在将列表分配给结果集之后正在修改列表。 如果我只返回结果集,一切都很好。 因此,为了实现我在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