我有1类和1个bean:Book.java和BookCatelogBean.java. Book类有3个属性:ID,Title和author,它们是相应的getter和setter. BookCatelogBean包含一个ArrayList< Book>在那里我用书籍填充(以后我会把它连接到一个数据库).
我有两个页面:index.xhtml和book.xhtml.我想在index.xhtml上显示每个格式为REST链接的图书标题列表,并将其ID设置为book.xhtml,如下所示:< h:link outcome =“book?ID =#{bookCatelogBean.ID}”值=“#{bookCatelogBean.Title}”/>
我知道如何使用BookCatelogBean来显示1本书,但是我想显示所有的书?我有一个想法,调用一个名为getAllBooks()的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@SessionScopedpublic 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>的列.两个组件都可以包含列表E.作为价值. 所以,你可以让你的托管bean如下:
@ManagedBean@RequestScopedpublic class BookCatalog implements Serializable { private List<Book> books; @postconstruct public voID init() { books = new ArrayList<Book>(); books.add(new Book(1,"LuDWig von Mises")); books.add(new Book(2,"Murry Rothbard")); books.add(new Book(3,"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?
> When should I load the collection from database for h:dataTable
> Creating master-detail pages for entities,how to link them and which bean scope to choose
以上是内存溢出为你收集整理的循环 – 如何显示从Bean到JSF网页的项目列表?全部内容,希望文章能够帮你解决循环 – 如何显示从Bean到JSF网页的项目列表?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)