循环 – 如何显示从Bean到JSF网页的项目列表?

前端之家收集整理的这篇文章主要介绍了循环 – 如何显示从Bean到JSF网页的项目列表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是JSF的新手,并且正在学习构建一个在线书店应用程序的过程.

我有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
@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>的列.两个组件都可以包含列表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,"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&gt ;:::

<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&gt,这也是很可能的,但是您应该记住,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

猜你在找的HTML相关文章