簡體   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