使用的技术:Struts2、JPA (EclipseLink)、Apache Derby 数据库。
我的项目在图像托管上,并且我UserEntity
(父级)配置为包含对ImageEntity
.
我有一个名为的会话变量userEntity
,它是登录时获取的用户对象。myUploads.jsp
使用用户对象中的列表并显示所有值的 JSP 页面。我使用了 struts 迭代器标签<s:iterator>
来循环List
.
问题 :
随机复制行<s:iterator>
。
注意:我查过数据库,没有重复,可以观察到重复的条目共享相同的图像ID。
JSP代码:
<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>
生成的 HTML 代码(运行时查看源代码):
<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>
这对于任何图像都是随机发生的,但是始终反映相同的重复,即如果图像 A 重复一次,并且我上传不重复的图像 B,则myUploads.jsp
图像 A 将两次。
但是当我删除重复的图像 ONCE 另一个副本被窃听(无法下载,无法查看)时,这意味着列表不是冗余的,但名称和评论字段仍然存在,并且存在一个图像 A
List.remove() API 引用:从该列表中删除第一次出现的指定元素(如果存在)(可选操作)。
例如
再上传几张图片后的另一个视图源(其他图片不重复)
<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">
A 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>
实体类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
方法和添加到列表的方法的使用
注意:该方法“仅使用一次”,如下图所示。所以,我没有可能添加的错误代码
addImage()
方法:
private boolean addImage()
{
DatabaseService db;
try {
db = new DatabaseService();
// get userEntity from session
Map sessionMap = ActionContext.getContext().getSession();
userEntity = (UserEntity) sessionMap.get("userEntity");
// save image to 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 is shared with you");
sendMailService.setSubject("DJVT IMAGE HOSTING");
for (UserEntity ue : imageEntity.getShares()) {
sendMailService.setTo(ue.getEmailid());
}
sendMailService.sendMail();
}
} catch (Exception e) {
throw new RuntimeException("img upload failed" + e.toString());
}
return true; // success
}
我如何解决我的错误,顺便说一下,您对这个错误有什么看法(或者如果我犯了愚蠢的错误,则错误)?
我注销并登录到我的应用程序,重复一次就消失了。