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

发布于 2025-01-03 15:47:00 字数 3073 浏览 2 评论 0 原文

我是 JSF 的新手,正在学习构建在线书店应用程序的过程中。

我有 1 个类和 1 个 bean:Book.javaBookCatelogBean.java。 Book 类有 3 个属性:idtitleauthor 及其相应的 getter 和 setter。 BookCatelogBean 包含一个 ArrayList,我在其中填充 Books(将来我会将其连接到数据库)。

我有两个页面:index.xhtmlbook.xhtml。我想在 index.xhtml 上显示每个格式为 REST 链接的书名列表及其 ID book.xhtml,如下所示:

我知道如何使用 BookCatelogBean 显示 1 book 但我想显示所有这些?我有一个想法,从 BookCatelogBean 中调用一个名为 getAllBooks() 的方法,该方法返回每一本书的标题,但是我如何将它们中的每一本书作为JavaserverFace 链接而不是字符串?

谢谢

这是我的代码:

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>

I'm new to JSF and in the process of learning im building an online book store application.

I have 1 class and 1 bean: Book.java and BookCatelogBean.java. The Book class has 3 properties: id, title, and author with it's corresponding getters and setters. The BookCatelogBean contains an ArrayList<Book> where I populate it with Books (in future I will connect it to a database).

I have two pages: index.xhtml and book.xhtml. I want to display the list of book titles on index.xhtml each formatted as a REST link and their ID to book.xhtml, like so: <h:link outcome="book?id=#{bookCatelogBean.id}" value="#{bookCatelogBean.title}" />

I know how to use BookCatelogBean to display 1 book but I want to display all of them? I have an idea to call a method from BookCatelogBean called getAllBooks() that returns each of the books titles but how would I return each one of them to index.xhtml as a JavaserverFace link instead of a string?

Thanks

Here is my code:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑叹一世浮沉 2025-01-10 15:47:00

JSF2 提供了两个现成的迭代组件: 。前者不向响应呈现任何内容(因此您可以 100% 控制最终的 HTML 输出),而后者向响应呈现 HTML 并需要 表示一列

。这两个组件都可以将 List 作为值。

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

}

并且您可以使用 生成

  • 例如:
<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 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 ,这也是很有可能的,但是你应该记住 JSTL 标签有不同的生命周期比 JSF 组件更重要。长话短说:JSF2 Facelets 中的 JSTL...有意义吗?

另请参阅:

JSF2 offers two iterating components out the box: <ui:repeat> and <h:dataTable>. The former renders nothing to the response (so you have 100% control over the final HTML output), while the latter renders a HTML <table> to the response and requires a <h:column> to represent a column of <td>s. Both components can take among others a List<E> as value.

So, you can just have your managed bean like follows:

@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;
    }

}

And you can use <ui:repeat> to generate for example an <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>

(note that the var attribute basically exposes the currently iterated item by exactly the given name in the EL scope within the component)

And here's how to use a <h:dataTable> instead:

<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>

As to the JSTL <c:forEach>, that's also quite possible, but you should keep in mind that JSTL tags have a different lifecycle than JSF components. Long story short: JSTL in JSF2 Facelets... makes sense?

See also:

梦幻之岛 2025-01-10 15:47:00

您还可以使用 PrimeFaces 库

链接:PrimeFaces 数据表

You can also use PrimeFaces library

Link: PrimeFaces datatable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文