h:dataTable不显示任何记录

时间:2016-12-28 22:13:48

标签: jsf jsf-2 datatable cdi

我正在尝试使用CDI和JSF来获取数据库中格式化为dataTable并在网页中打印的所有书籍的列表,但不是这样我得到空白页面。我也尝试使用JSF selectOneMenu而不是dataTable但结果是一样的。

这是页面代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <head>
        <title>Publish a book</title>
    </head>
    <h:body>
        <ui:define name="content">
            <h:dataTable value="#{pub.allBooks}" var="c">
                <h:column>#{c.title}</h:column>
            </h:dataTable>
        </ui:define>
    </h:body>
</html>

控制器

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

@Model
@ManagedBean("pub")
public class PublishABookController {

    @Inject
    PublishABook publishABook;

    @Inject
    FacesContext facesContext;

    @Produces
    @Named
    List<Book> allBooks;

    @PostConstruct
    public void initBooks(){
        allBooks = publishABook.getAllBooks();
    }
}

服务

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.List;
import java.util.logging.Logger;

@Stateless
public class PublishABookImpl implements PublishABook {

    @Inject
    private PublisherDAO publisherDAO;

    @Inject
    private BookDAO bookDAO;

    @Inject
    private Logger logger;

    @Override
    public void publishABook(Publisher publisher, Book book) {
            bookDAO.save(book);
            publisher.getPublishedBooks().add(book);
            publisherDAO.update(publisher);
            logger.info("Book created");
    }

    @Override
    public List<Book> getAllBooks() {
        return bookDAO.list();
    }
}

1 个答案:

答案 0 :(得分:0)

您只需要从页面中删除<ui:define name="content"></define>元素即可。

ui:define通常用于模板化的JSF页面,但在您的上下文中它只是阻止呈现页面的内容。