我是JSF的新手,在学习我建立在线书店应用程序的过程中。
我有1个班级和1个豆子:Book.java
和BookCatelogBean.java
。 Book类有3个属性:id
,title
和author
及其相应的getter和setter。 BookCatelogBean
包含ArrayList<Book>
,我在其中填充Books
(将来我会将其连接到数据库)。
我有两个页面:index.xhtml
和book.xhtml
。我希望在index.xhtml
上显示每个格式为REST链接的图书标题列表,并将其ID标识为book.xhtml
,如下所示:<h:link outcome="book?id=#{bookCatelogBean.id}" value="#{bookCatelogBean.title}" />
我知道如何使用BookCatelogBean
来显示1 book
,但我想显示所有这些内容?我有一个想法是从BookCatelogBean
调用一个名为getAllBooks()
的方法来返回每个书籍标题,但我如何将它们中的每一个返回到index.xhtml作为JavaserverFace链接而不是字符串?< / p>
由于
这是我的代码:
Book.java
package bookshop;
import java.io.Serializable;
public class Book implements Serializable {
private int id;
private String title;
private String author;
public Book(int id, String title, String author){
this.title = title;
this.id = id;
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
BookCatelogBean.java
package bookshop;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class BookCatelogBean implements Serializable {
private int currentItem = 0;
private ArrayList<Book> books = new ArrayList<Book>(Arrays.asList(
new Book(1, "Theory of Money and Credit", "Ludwig von Mises"),
new Book(2, "Man, Economy and State", "Murry Rothbard"),
new Book(3, "Real Time Relationships", "Stefan Molyneux")));
public String getTitle(){
return books.get(currentItem).getTitle();
}
public int getId(){
return books.get(currentItem).getId();
}
public String getAuthor(){
return books.get(currentItem).getAuthor();
}
}
的index.xhtml
<?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://java.sun.com/jsf/html">
<h:head>
<title>BookShop</title>
</h:head>
<h:body>
<h:link outcome="book?id=#{bookCatelogBean.id}" value="#{bookCatelogBean.title}" />
</h:body>
</html>
答案 0 :(得分:31)
JSF2提供了两个迭代组件:<ui:repeat>
和<h:dataTable>
。前者对响应没有任何作用(因此您可以100%控制最终的HTML输出),而后者则为响应呈现HTML <table>
,并且需要<h:column>
来表示{{3}列。 1}}秒。这两个组件都可以将<td>
作为值。
所以,您可以像下面这样使用托管bean:
List<E>
您可以使用@ManagedBean
@RequestScoped
public class BookCatalog implements Serializable {
private List<Book> books;
@PostConstruct
public void init() {
books = new ArrayList<Book>();
books.add(new Book(1, "Theory of Money and Credit", "Ludwig von Mises"));
books.add(new Book(2, "Man, Economy and State", "Murry Rothbard"));
books.add(new Book(3, "Real Time Relationships", "Stefan Molyneux"));
}
public List<Book> getBooks() {
return books;
}
}
生成例如<ui:repeat>
:
<ul><li>
(请注意,<ul>
<ui:repeat value="#{bookCatalog.books}" var="book">
<li>
<h:link value="#{book.title}" outcome="book">
<f:param name="id" value="#{book.id}" />
</h:link>
</li>
</ui:repeat>
</ul>
属性基本上将当前迭代的项目暴露给组件内EL范围内的给定名称)
以下是如何使用var
代替:
<h:dataTable>
对于JSTL <h:dataTable value="#{bookCatalog.books}" var="book">
<h:column>
<h:link value="#{book.title}" outcome="book">
<f:param name="id" value="#{book.id}" />
</h:link>
</h:column>
</h:dataTable>
,这也很有可能,但是你应该记住,JSTL标签的生命周期与JSF组件不同。长话短说:JSTL in JSF2 Facelets... makes sense?
答案 1 :(得分:1)
你也可以使用primefaces库 Primefaces datatable