我是JSF的新手,正在学习学习如何构建在线书店应用程序。
我有1个班级和1个bean: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}" />
显示1个BookCatelogBean
,但是我想显示所有这些?我有一个想法,要从book
调用一个称为BookCatelogBean
的方法,该方法返回每本书的书名,但是我如何将每本书的书名作为JavaserverFace链接而不是字符串返回给index.xhtml?
谢谢
这是我的代码:
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>
最佳答案
JSF2提供了两个迭代组件:<ui:repeat>
和<h:dataTable>
。前者不对响应进行任何渲染(因此您可以对最终的HTML输出进行100%的控制),而后者则对响应进行呈现HTML <table>
,并且需要<h:column>
来表示<td>
的列。这两个组件都可以取List<E>
作为值。
因此,您可以像下面这样使用托管bean:
@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>
(请注意,
var
属性基本上通过组件内EL作用域中的给定名称公开当前迭代的项)以下是使用
<h:dataTable>
的方法:<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
<c:forEach>
,这也是很有可能的,但是您应该记住,JSTL标记的生命周期与JSF组件的生命周期不同。长话短说:JSTL in JSF2 Facelets... makes sense?也可以看看:
How to choose the right bean scope?
How and when should I load the model from database for h:dataTable
Creating master-detail pages for entities, how to link them and which bean scope to choose