java - Struts2 <s:iterator> random duplicating values -
technology used : struts2 , jpa (eclipselink) , apache derby database.
my project on image hosting , have userentity (parent) configured contain reference imageentity.
i have session variable called userentity user object fetched on login. jsp page myuploads.jsp use list in user object , display values. have used struts iterator tag <s:iterator> loop list.
problem :
random duplication of rows <s:iterator>.
note : have check database , , has no duplication, can observed duplicated entry share same image id.
jsp code:
<table border="1"> <thead> <tr> <th>name</th> <th>comments</th> <th>link</th> <th>image</th> <th>delete image</th> <th>share details</th> </tr> </thead> <tbody> <s:iterator value="#session.userentity.images" var="image"> <s:url id="imageurl" action="myuploads" > <s:param name="imageid" value="#image.id"></s:param> </s:url> <s:url id="deleteimageurl" action="deleteimage" method="deleteimage"> <s:param name="imageid" value="#image.id"></s:param> </s:url> <tr> <td><s:property value="#image.name"/></td> <td><s:property value="#image.comments"/></td> <td><s:a href="%{imageurl}" cssclass="linkstyle"> <s:property value="#image.imagename"/> </s:a></td> <td> <s:a href="%{imageurl}" cssclass="linkstyle"> <img width="200" height="200" src="<s:url action='imageaction'><s:param name='imageid' value="#image.id"></s:param></s:url>" /> </s:a></td> <td> <s:a href="%{deleteimageurl}" cssclass="linkstyle"> delete </s:a> </td> <td> <s:if test="%{#image.shares != null}"> <ul> <s:iterator value="#image.shares" var="share"> <li><s:property value="#share.fullname"/></li> </s:iterator> </ul> </s:if> </td> </tr> </s:iterator> </tbody> </table> generated html code (view source on run):
<table border="1"> <thead> <tr> <th>name</th> <th>comments</th> <th>link</th> <th>image</th> <th>delete image</th> <th>share details</th> </tr> </thead> <tbody> row 1 : imageid=151 <tr> <td>sun</td> <td>sun set</td> <td><a href="/imagehosting5.7/myuploads.action?imageid=151" class="linkstyle"> sunset.jpg </a></td> <td> <a href="/imagehosting5.7/myuploads.action?imageid=151" class="linkstyle"> <img width="200" height="200" src="/imagehosting5.7/imageaction.action?imageid=151" /> </a></td> <td> <a href="/imagehosting5.7/deleteimage!deleteimage.action?imageid=151" class="linkstyle"> delete </a> </td> <td> <ul> <li>b</li> </ul> </td> </tr> row 2 : imageid=151 <tr> <td>sun</td> <td>sun set</td> <td><a href="/imagehosting5.7/myuploads.action?imageid=151" class="linkstyle"> sunset.jpg </a></td> <td> <a href="/imagehosting5.7/myuploads.action?imageid=151" class="linkstyle"> <img width="200" height="200" src="/imagehosting5.7/imageaction.action?imageid=151" /> </a></td> <td> <a href="/imagehosting5.7/deleteimage!deleteimage.action?imageid=151" class="linkstyle"> delete </a> </td> <td> <ul> <li>b</li> </ul> </td> </tr> </tbody> </table> this happens randomly image, same repetition reflected times, if image repeated once, , upload image b not repeated, myuploads.jsp have image twice .
but when delete repeated image once other copy gets bugged (cannot download, cannot view) implies list not redundant, name , comment fields remain, , 1 image exists
list.remove() api quote: removes first occurrence of specified element list, if present (optional operation).
e.g.
another view source after few more image uploads (where other images not repeated)
<table border="1"> <thead> <tr> <th>name</th> <th>comments</th> <th>link</th> <th>image</th> <th>delete image</th> <th>share details</th> </tr> </thead> <tbody> row 1 : imageid=151 <tr> <td>sun</td> <td>sun set</td> <td><a href="/imagehosting5.7/myuploads.action?imageid=151" class="linkstyle"> sunset.jpg </a></td> <td> <a href="/imagehosting5.7/myuploads.action?imageid=151" class="linkstyle"> <img width="200" height="200" src="/imagehosting5.7/imageaction.action?imageid=151" /> </a></td> <td> <a href="/imagehosting5.7/deleteimage!deleteimage.action?imageid=151" class="linkstyle"> delete </a> </td> <td> <ul> <li>b</li> </ul> </td> </tr> row 2 : imageid=151 <tr> <td>sun</td> <td>sun set</td> <td><a href="/imagehosting5.7/myuploads.action?imageid=151" class="linkstyle"> sunset.jpg </a></td> <td> <a href="/imagehosting5.7/myuploads.action?imageid=151" class="linkstyle"> <img width="200" height="200" src="/imagehosting5.7/imageaction.action?imageid=151" /> </a></td> <td> <a href="/imagehosting5.7/deleteimage!deleteimage.action?imageid=151" class="linkstyle"> delete </a> </td> <td> <ul> <li>b</li> </ul> </td> </tr> row 3 : imageid=152 <tr> <td>abc</td> <td>aaa</td> <td><a href="/imagehosting5.7/myuploads.action?imageid=152" class="linkstyle"> blue hills.jpg </a></td> <td> <a href="/imagehosting5.7/myuploads.action?imageid=152" class="linkstyle"> <img width="200" height="200" src="/imagehosting5.7/imageaction.action?imageid=152" /> </a></td> <td> <a href="/imagehosting5.7/deleteimage!deleteimage.action?imageid=152" class="linkstyle"> delete </a> </td> <td> <ul> </ul> </td> </tr> row 4 : imageid=153 <tr> <td>apng</td> <td>aaa</td> <td><a href="/imagehosting5.7/myuploads.action?imageid=153" class="linkstyle"> png image.png </a></td> <td> <a href="/imagehosting5.7/myuploads.action?imageid=153" class="linkstyle"> <img width="200" height="200" src="/imagehosting5.7/imageaction.action?imageid=153" /> </a></td> <td> <a href="/imagehosting5.7/deleteimage!deleteimage.action?imageid=153" class="linkstyle"> delete </a> </td> <td> <ul> <li>b</li> </ul> </td> </tr> </tbody> </table> entity class image:
@id @generatedvalue(strategy = generationtype.auto) private long id; --------> unique private string name; private string comments; @manytoone private userentity userentity; private string imagepath; private string imagename; @manytomany(cascade = cascadetype.merge) private list<userentity> shares; // list of owner ids private boolean publicshare; // isshared usage of method , method adds list
note : method used 'only once' spotted in image added below. so, got no bugged code may add

the addimage() method:
private boolean addimage() { databaseservice db; try { db = new databaseservice(); // userentity session map sessionmap = actioncontext.getcontext().getsession(); userentity = (userentity) sessionmap.get("userentity"); // save image file system string filepath = servletrequest.getsession().getservletcontext().getrealpath("/images"); file filetocreate = new file(filepath, this.uploadfilefilename); fileutils.copyfile(this.uploadfile, filetocreate); // make imageentity imageentity = new imageentity(); imageentity.setname(name); imageentity.setcomments(comments); imageentity.setuserentity(userentity); try { if(!usershare.isempty()) imageentity.setshares(new databaseservice().finduserentitysbyusernames(users)); } catch (exception e) { logger.getlogger(getclass().getname()).log(level.severe, "exception caught", e); } imageentity.setimagepath(filetocreate.getabsolutepath()); imageentity.setimagename(filetocreate.getname()); db.persist(imageentity); userentity.addtoimages(imageentity); // update userentity userentity = (userentity) db.merge(userentity); // update sessionmap sessionmap.put("userentity", userentity); // notify share if( ! imageentity.getshares().isempty()) { // init mail sendmailservice sendmailservice = new sendmailservice(); sendmailservice.setbody("image shared you"); sendmailservice.setsubject("djvt image hosting"); (userentity ue : imageentity.getshares()) { sendmailservice.setto(ue.getemailid()); } sendmailservice.sendmail(); } } catch (exception e) { throw new runtimeexception("img upload failed" + e.tostring()); } return true; // success } how solve bug, , way opinions bug (or error if making silly mistakes)?
i logged out , logged in application , duplication gone once.
how solve bug , , way opinions bug(or error if making silly mistakes)
the problems sharing collection iterate in session. other actions may update collection before result executed. check put beforeresult annotation on method check duplicates. make sure have annotationworkflow interceptor replaces workflow interceptor on stack.
other possibilities if change default theme
<constant name="struts.ui.theme" value="simple"/> in struts.xml or use css_xhtml theme.
Comments
Post a Comment