Struts2 <s:iterator>随机重复值</s:iterator>

时间:2013-08-16 03:16:06

标签: java jpa struts2 eclipselink derby

使用的技术:Struts2,JPA(EclipseLink),Apache Derby数据库。

我的项目位于图片托管上,我UserEntity(父)已配置为包含对ImageEntity的引用。

我有一个名为userEntity的会话变量,它是登录时获取的User对象。 JSP页面myUploads.jsp,用于在用户对象中使用List并显示所有值。我使用了struts迭代器标记<s:iterator>来循环List

问题:

按行<s:iterator>对行进行RANDOM重复。

注意:我已检查数据库,并且没有重复,可以观察到重复的条目共享相同的图像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两次。

但是当我删除重复的图像时,另一个副本被窃听(无法下载,无法查看),这意味着LIST不是冗余,但名称和注释字段仍然存在,并且存在一个图像

  

List.remove()API引用:   从该列表中删除第一次出现的指定元素(如果存在)(可选操作)。

e.g。

在更多图片上传后的其他视图来源(其他图片不重复)

<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
}

我如何解决我的错误,以及你对这个错误的看法(或者如果我犯了愚蠢的错误,会出错)?

我退出并登录到我的应用程序,复制已经消失了一次。

1 个答案:

答案 0 :(得分:0)

  

我如何解决我的错误,以及你的意见   这个错误(如果我犯了愚蠢的错误,那就是错误)

您正在共享在会话中迭代的集合的问题。因此,其他操作可能会在执行结果之前更新此集合。要检查这一点,您可以将BeforeResult annotation放在可以检查重复项的方法上。确保你有annotationWorkflow interceptor替换堆栈上的workflow拦截器。

如果您将默认主题更改为

,则可能存在其他可能性
<constant name="struts.ui.theme" value="simple"/>

struts.xml或使用css_xhtml主题。