本文介绍了如何将 Bean 中的项目列表显示到 JSF 网页上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有两个页面:index.xhtmlbook.xhtml.我想在 index.xhtml 上显示书名列表,每个书名都格式化为 REST 链接及其 ID 到 book.xhtml,如下所示:<h:link 结果="book?id=#{bookCatelogBean.id}" value="#{bookCatelogBean.title}"/>

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

谢谢

这是我的代码:

Book.java

包书店;导入 java.io.Serializable;公共类 Book 实现了 Serializable {私有整数 ID;私人字符串标题;私人字符串作者;公共书(int id,字符串标题,字符串作者){this.title = 标题;this.id = id;this.author = 作者;}公共字符串 getAuthor() {返回作者;}公共无效setAuthor(字符串作者){this.author = 作者;}公共 int getId() {返回标识;}公共无效 setId(int id) {this.id = id;}公共字符串 getTitle() {返回标题;}公共无效设置标题(字符串标题){this.title = 标题;}}

BookCatelogBean.java

包书店;导入 java.io.Serializable;导入 java.util.ArrayList;导入 java.util.Arrays;导入 javax.faces.bean.ManagedBean;导入 javax.faces.bean.SessionScoped;@ManagedBean@SessionScoped公共类 BookCatelogBean 实现了 Serializable {私人 int currentItem = 0;私有 ArrayListbook = new ArrayList(Arrays.asList(新书(1,货币与信用理论",路德维希·冯·米塞斯"),新书(2,人,经济与国家",默里罗斯巴德"),new Book(3, "实时关系", "Stefan Molyneux")));公共字符串 getTitle(){返回 book.get(currentItem).getTitle();}公共 int getId(){返回 book.get(currentItem).getId();}公共字符串 getAuthor(){返回 book.get(currentItem).getAuthor();}}

index.xhtml

解决方案

JSF2 提供了两个开箱即用的迭代组件:.前者不向响应呈现任何内容(因此您可以 100% 控制最终的 HTML 输出),而后者向响应呈现 HTML <table> 并需要 表示一列 .两个组件都可以将 List 作为值.

所以,你可以像下面这样拥有你的托管 bean:

@ManagedBean@RequestScoped公共类 BookCatalog 实现了 Serializable {私人列表<Book>图书;@PostConstruct公共无效初始化(){书籍 = 新的 ArrayList();book.add(new Book(1, "Theory of Money and Credit", "Ludwig von Mises"));book.add(new Book(2, "Man, Economic and State", "Murry Rothbard"));book.add(new Book(3, 实时关系", Stefan Molyneux"));}公共列表<Book>获取书籍(){还书;}}

您可以使用 生成例如

  • :

      <ui:repeat value="#{bookCatalog.books}";var=书"><li><h:link value="#{book.title}";结果=书"><f:param name="id";值=#{book.id}";/></h:link></ui:repeat>

    (请注意,var 属性基本上通过组件内 EL 范围内的给定名称公开当前迭代的项目)

    这里是如何使用 代替:

    <h:dataTable value="#{bookCatalog.books}"var=书"><h:列><h:link value="#{book.title}";结果=书"><f:param name="id";值=#{book.id}";/></h:link></h:列></h:dataTable>

    对于 JSTL <c:forEach>,这也是很有可能的,但是您应该记住,JSTL 标记与 JSF 组件具有不同的生命周期.长话短说:JSF2 Facelets 中的 JSTL ......有意义吗?>

    另见:

    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

    <?
    解决方案

    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:

    这篇关于如何将 Bean 中的项目列表显示到 JSF 网页上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 17:03