如何使用当前上下文 JPA、JSF、Java 集合删除项目集合
我想删除与当前电影不匹配的评论;
信息
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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,这是一段糟糕的代码。这个版本怎么样?
改进:
equals()
比较对象,而不是==
(您应该相应地实现Movie.equals(Object)
)。但要真正解决您的问题:
您是否在不同的电影中重用此组件?如果是这样,将评论保留在字段中就是无稽之谈。删除对
userCommentItems
的所有分配和读取。First of all, this is awful code. How about this version?
Improvements:
equals()
, not==
(you should implementMovie.equals(Object)
accordingly).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
.