Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何在JSF网页上显示Bean中的项目列表?_Loops_Jsf_Jsf 2_Collections_Iteration - Fatal编程技术网

Loops 如何在JSF网页上显示Bean中的项目列表?

Loops 如何在JSF网页上显示Bean中的项目列表?,loops,jsf,jsf-2,collections,iteration,Loops,Jsf,Jsf 2,Collections,Iteration,我是JSF新手,正在学习im构建在线书店应用程序 我有一个类和一个bean:Book.java和BookCatelogBean.java。Book类有3个属性:id,title,和author,以及相应的getter和setter。BookCatelogBean包含一个ArrayList,我在其中填充了Books(将来我将把它连接到数据库) 我有两个页面:index.xhtml和book.xhtml。我想在index.xhtml上显示书籍标题列表,每个标题都格式化为REST链接,并且它们的ID指

我是JSF新手,正在学习im构建在线书店应用程序

我有一个类和一个bean:
Book.java
BookCatelogBean.java
。Book类有3个属性:
id
title
,和
author
,以及相应的getter和setter。
BookCatelogBean
包含一个
ArrayList
,我在其中填充了
Books
(将来我将把它连接到数据库)

我有两个页面:
index.xhtml
book.xhtml
。我想在
index.xhtml
上显示书籍标题列表,每个标题都格式化为REST链接,并且它们的ID指向
book.xhtml
,如下所示:

我知道如何使用
BookCatelogBean
显示1
book
,但我想显示所有这些?我想从
BookCatelogBean
调用一个名为
getAllBooks()
的方法,该方法返回每本书的标题,但如何将它们作为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;
    }
}
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();
    }

}
BookCatelogBean.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;
    }
}
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();
    }

}
包装书店;
导入java.io.Serializable;
导入java.util.ArrayList;
导入java.util.array;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.SessionScoped;
@ManagedBean
@会议范围
公共类BookCatelogBean实现了可序列化{
private int currentItem=0;
private ArrayList books=new ArrayList(Arrays.asList(
新书(1,《货币和信用理论》,《路德维希·冯·米塞斯》),
新书(2,《人、经济和国家》,《莫里·罗斯巴德》),
新书(3,《实时关系》,《斯特凡·莫利纽斯》);
公共字符串getTitle(){
return books.get(currentItem).getTitle();
}
公共int getId(){
return books.get(currentItem).getId();
}
公共字符串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提供了两个现成的迭代组件:和。前者不向响应呈现任何内容(因此您对最终HTML输出拥有100%的控制权),而后者向响应呈现HTML
,并且需要一个表示
的列。这两个组件都可以将
列表
作为值

因此,您可以使用托管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;
    }

}
至于JSTL
,这也是很有可能的,但是您应该记住,JSTL标记的生命周期与JSF组件不同。长话短说:

另见:

您也可以使用PrimeFaces库


Link:

这里需要的是c:forEach吗?我尝试了上面的代码,得到了一个显然是空的列表。我可能做错了什么?我收回上述问题。