gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:47:16 25 4
gpt4 key购买 nike

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

我有 1 个类和 1 个 bean:Book.javaBookCatelogBean.java . Book 类有 3 个属性:id , title , 和 author有相应的 getter 和 setter。 BookCatelogBean包含 ArrayList<Book>我用 Books 填充它(将来我会将它连接到数据库)。

我有两页:index.xhtmlbook.xhtml .我想在 index.xhtml 上显示书名列表每个都格式化为 REST 链接,其 ID 为 book.xhtml ,像这样:<h:link outcome="book?id=#{bookCatelogBean.id}" value="#{bookCatelogBean.title}" />
我知道如何使用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;
}
}

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> .前者不渲染任何响应(因此您可以 100% 控制最终的 HTML 输出),而后者渲染 HTML <table>响应并需要 <h:column> 表示 <td> 的列s。这两个组件都可以采用 List<E>作为值(value)。
所以,你可以像下面这样拥有你的托管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;
}

}
您可以使用 <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?
  • How and when should I load the model from database for h:dataTable
  • Creating master-detail pages for entities, how to link them and which bean scope to choose
  • 关于loops - 如何将 Bean 中的项目列表显示到 JSF 网页上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9186364/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com