1
我嘗試使用CDI和JSF獲取數據庫中所有書籍的列表,格式設置爲dataTable並打印在網頁中,但取而代之,我獲得空白頁面。我也嘗試使用JSF selectOneMenu代替dataTable,但結果相同。h:dataTable不顯示任何記錄
Here`s頁面的代碼
<?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();
}
}
你調試了什麼?當你通過製作人製作'allBooks'時,你爲什麼使用'#{pub.allBooks}'?你是否在jsf開發模式下運行你的應用程序時出現任何錯誤?如果它與另一個組件失敗,爲什麼你將它標記爲'datatable'?原因很可能有些其他。試過一個簡單的'ui:repeat'? – Kukeltje