如何使用当前上下文 JPA、JSF、Java 集合删除项目集合

发布于 2024-10-18 19:58:00 字数 2822 浏览 1 评论 0原文

我想删除与当前电影不匹配的评论;

信息

         jpaController = (CommentFacade) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "commentJpa");
         movieController = (MovieController) facesContext.getApplication().evaluateExpressionGet(facesContext, "#{movie}", MovieController.class);
         private List<Comment> userCommentItems = null;
         private Comment userComment = null;

 public List<Comment> getUserCommentItems() {
    if (userCommentItems == null) {
        getUserPagingInfo();
        Vector comments = (Vector) jpaController.findAll();
        Vector v = new Vector(comments);
        for (Iterator iterator = comments.listIterator(); iterator.hasNext();){
                userComments = (Comment) iterator.next();
                if (userComments.getMovie().getIdMovie() != movieController.getMovie().getIdMovie()){
                    v.remove(userComments);
                }
            }
        userCommntItems = v;
    }
    return userCommentItems ;
}


  <h:panelGroup>
       <h:outputText value="Item #{comment.userPagingInfo.firstItem +  1}..#{comment.userPagingInfo.lastItem} of #{comment.userPagingInfo.itemCount}"/>&nbsp;
      <h:commandLink action="#{comment.userPrev}" value="Previous #{comment.userPagingInfo.batchSize}" 
    rendered="#{comment.userPagingInfo.firstItem >= comment.userPagingInfo.batchSize}"/>
     <h:commandLink action="#{comment.userNext}" value="Next #{comment.userPagingInfo.batchSize}" rendered="#{comment.userPagingInfo.lastItem +  comment.userPagingInfo.batchSize <= comment.userPagingInfo.itemCount}"/>&nbsp;
     <h:commandLink action="#{comment.userNext}" value="Remaining #{comment.userPagingInfo.itemCount - comment.userPagingInfo.lastItem}"
                          rendered="#{comment.userPagingInfo.lastItem < comment.userPagingInfo.itemCount && comment.userPagingInfo.lastItem + comment.userPagingInfo.batchSize > comment.userPagingInfo.itemCount}"/>

    <h:dataTable value="#{comment.userCommentItems}" var="item"
             border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px"
             rendered="#{not empty comment.movieController.movie.commentCollection}">

    <h:column>
        <f:facet name="header">
            <h:outputText value="IdUser"/>
        </f:facet>
        <h:outputText value="#{item.idUser.user}"/>
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:outputText value="Text"/>
        </f:facet>
        <h:outputText value="#{item.text}"/>
    </h:column>
</h:dataTable>
</h:panelGroup>

I want to remove comment that do not match the current movie;

info

         jpaController = (CommentFacade) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "commentJpa");
         movieController = (MovieController) facesContext.getApplication().evaluateExpressionGet(facesContext, "#{movie}", MovieController.class);
         private List<Comment> userCommentItems = null;
         private Comment userComment = null;

 public List<Comment> getUserCommentItems() {
    if (userCommentItems == null) {
        getUserPagingInfo();
        Vector comments = (Vector) jpaController.findAll();
        Vector v = new Vector(comments);
        for (Iterator iterator = comments.listIterator(); iterator.hasNext();){
                userComments = (Comment) iterator.next();
                if (userComments.getMovie().getIdMovie() != movieController.getMovie().getIdMovie()){
                    v.remove(userComments);
                }
            }
        userCommntItems = v;
    }
    return userCommentItems ;
}


  <h:panelGroup>
       <h:outputText value="Item #{comment.userPagingInfo.firstItem +  1}..#{comment.userPagingInfo.lastItem} of #{comment.userPagingInfo.itemCount}"/> 
      <h:commandLink action="#{comment.userPrev}" value="Previous #{comment.userPagingInfo.batchSize}" 
    rendered="#{comment.userPagingInfo.firstItem >= comment.userPagingInfo.batchSize}"/>
     <h:commandLink action="#{comment.userNext}" value="Next #{comment.userPagingInfo.batchSize}" rendered="#{comment.userPagingInfo.lastItem +  comment.userPagingInfo.batchSize <= comment.userPagingInfo.itemCount}"/> 
     <h:commandLink action="#{comment.userNext}" value="Remaining #{comment.userPagingInfo.itemCount - comment.userPagingInfo.lastItem}"
                          rendered="#{comment.userPagingInfo.lastItem < comment.userPagingInfo.itemCount && comment.userPagingInfo.lastItem + comment.userPagingInfo.batchSize > comment.userPagingInfo.itemCount}"/>

    <h:dataTable value="#{comment.userCommentItems}" var="item"
             border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px"
             rendered="#{not empty comment.movieController.movie.commentCollection}">

    <h:column>
        <f:facet name="header">
            <h:outputText value="IdUser"/>
        </f:facet>
        <h:outputText value="#{item.idUser.user}"/>
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:outputText value="Text"/>
        </f:facet>
        <h:outputText value="#{item.text}"/>
    </h:column>
</h:dataTable>
</h:panelGroup>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-10-25 19:58:00

首先,这是一段糟糕的代码。这个版本怎么样?

public List<Comment> getUserCommentItems() {
    if (userCommentItems == null) {
        getUserPagingInfo();
        List<Comment> comments = jpaController.findAll();
        for (Iterator<Comment> iterator = comments.iterator(); iterator.hasNext();){
                userComments = iterator.next();
                if (!userComments.getMovie().equals(movieController.getMovie()){
                    iterator.remove();
                }
            }
        userCommntItems = comments;
    }
    return userCommentItems ;
}

改进:

  • 不再使用已弃用的 Vector 类(尤其是不再不必要的转换)
  • 使用 equals() 比较对象,而不是 == (您应该相应地实现Movie.equals(Object))。
  • 无需复制集合,只需处理原始集合(如果您的 DAO 返回一些我无法更改的内容,那就很糟糕),

但要真正解决您的问题:

您是否在不同的电影中重用此组件?如果是这样,将评论保留在字段中就是无稽之谈。删除对 userCommentItems 的所有分配和读取。

First of all, this is awful code. How about this version?

public List<Comment> getUserCommentItems() {
    if (userCommentItems == null) {
        getUserPagingInfo();
        List<Comment> comments = jpaController.findAll();
        for (Iterator<Comment> iterator = comments.iterator(); iterator.hasNext();){
                userComments = iterator.next();
                if (!userComments.getMovie().equals(movieController.getMovie()){
                    iterator.remove();
                }
            }
        userCommntItems = comments;
    }
    return userCommentItems ;
}

Improvements:

  • No more usage of deprecated Vector class (especially no more unnecessary casts)
  • Compare object using equals(), not == (you should implement Movie.equals(Object) accordingly).
  • No need to copy the collection, work on the original (if your DAO returns something I can't change then it sucks)

But to actually solve your problem:

Are you reusing this component for different movies? If so, keeping the comments in a field is nonsense. Remove all assignments to and reads from userCommentItems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文