【问题标题】:Display table titles from nested list shows to much显示嵌套列表中的表标题
【发布时间】:2018-04-20 12:03:28
【问题描述】:

我正在写一个博客,我想从数据库 dataTable 中显示 2 列,在其中一列中我想要帖子标题,在另一列中我想要发布内容,但是当我尝试在我的页面,我得到的唯一结果是在帖子标题列中显示每一行的所有帖子标题。我想问你,我怎样才能达到我的目标。 现在我有

title1 title2 ... | content1 
title1 title2 ... | content2 
etc

但我想要

title 1| content1 <br>
title2 | content2 <br>

这是我的 index.xhtml 代码来显示数据表

<h:dataTable value="#{postView.postList}" 
                     var="k"
                     styleClass="table"
                     headerClass="tableHeader"
                     rowClasses="tableContent"
                     >
            <h:column>
                <h:dataTable value="#{postView.postList}" var="t">
                    <h:column>
                        #{t.title}
                    </h:column>
                </h:dataTable>
            </h:column> 
            <h:column>
               #{k.postContent}               
            </h:column>

        </h:dataTable>

这是我的控制器

package ManagePost;

import Entities.Post;
import FacadeDAO.PostFacade;
import FacadeDAO.UserFacade;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ViewScoped
public class PostView implements Serializable {

    @Inject
    private PostFacade postDAO;

    private List<Post> postList;
    private String title;
    private String content;

    public PostView() {

    }

    public List<Post> getPostList() {
        return postList;
    }

    public void setPostList(List<Post> postList) {
        this.postList = postList;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @PostConstruct
    public void init() {
        postList = postDAO.showAll();
    }

    public String validate() {
        Post post = postDAO.checkIfExist(title);
        if (post == null) {
            Post newPost = new Post();
            newPost.setTitle(title);
            newPost.setPostContent(content);
            try {
                postDAO.add(newPost);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return "loginPage";
        } else {
            return null;
        }
    }

}

最后一个,PostDao

public List<Post> showAll() {
        List<Post> postList = new ArrayList<>();

        List<Post> result = getEntityManager().createNamedQuery("Post.findAll", Post.class).getResultList();

        postList.addAll(result);

        return postList;
    }


【问题讨论】:

  • 请注意良好的标记...通过添加 glassfish-5 你例如声明在早期版本的 Glasdfish 或 Wildfly 中没有问题。而且这个问题不是 JSF-2.2 特有的。标签是针对问题所在的,而不是您使用的。
  • Uhmmm...你对同一个列表进行了两次迭代!!!那可不好
  • @Kukeltje 你说得对,对不起,我会记住的

标签: jsf datatable


【解决方案1】:

您正在嵌入数据表中显示所有标题。您应该删除嵌套的数据表标签:

<h:dataTable value="#{postView.postList}" 
             var="k"
             styleClass="table"
             headerClass="tableHeader"
             rowClasses="tableContent"
             >
    <h:column>
        #{k.title}
    </h:column> 
    <h:column>
       #{k.postContent}               
    </h:column>

</h:dataTable>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2021-07-31
    • 2019-10-03
    • 2019-07-11
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多