Reputation: 94
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. 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
e.g. <c:forEach var="image" items="#{viewBean.images}">
the server, Tomee throws and exception 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. 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.
Thanks. Jason.
Edit. Below is method used for retrieving objects from db for iteration in 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
<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}"
Upvotes: 1
Views: 929
Reputation: 94
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.
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;
}
Upvotes: 1