如何删除 Struts 2 中的随机重复值?

问题描述 投票:0回答:1

使用的技术:Struts 2、JPA (EclipseLink)、Apache Derby Database。

我的项目位于 图像托管,并且我已将

UserEntity
(父级)配置为包含对
ImageEntity
的引用。

我有一个名为

userEntity
的会话变量,它是登录时获取的
User
对象。 JSP 页面
myUploads.jsp
使用用户对象中的
List
并显示所有值。我使用了 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 两次。

但是当我删除重复的图像一次时,另一个副本会被窃听(无法下载,无法查看),这意味着列表不是多余的,但是

Name
Comment
字段仍然存在,并且一个图像 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 

方法的使用以及添加到列表的方法

注意: 该方法仅使用一次,如下图所示。所以,我没有得到可能添加的错误代码

enter image description here

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 }
我如何解决我的错误,顺便问一下,您对此错误有何看法(如果我犯了愚蠢的错误,则为错误)?

我注销并登录到我的应用程序,重复项就消失了一次。

java jpa struts2 eclipselink derby
1个回答
0
投票
我如何解决我的错误,顺便问一下你对此有何看法 这个错误(或者错误,如果我犯了愚蠢的错误)

您共享在会话中迭代的集合的问题。因此,其他操作可能会在执行结果之前“更新”此集合。要检查这一点,您可以在可以检查重复项的方法上添加

BeforeResult 注释。确保堆栈上有

annotationWorkflow 拦截器 替换 
workflow
拦截器。
其他可能性可能是如果您将默认主题更改为

<constant name="struts.ui.theme" value="simple"/>

struts.xml
或使用

css_xhtml

 主题。
	

© www.soinside.com 2019 - 2024. All rights reserved.