问题描述
我是JSF的新手,在学习建立在线书店应用程序的过程中。
我有1个类和1个bean: Book.java 和 BookCatelogBean.java 。 Book类有3个属性: id , title 和 author 与它的相应的getters和setters。 BookCatelogBean 包含 ArrayList< Book> ,其中用 Books (在将来我会将其连接到数据库)。
我有两个网页: index.xhtml 和 book.xhtml 。我想在 index.xhtml 上显示每个格式化为REST链接的书名列表,以及它们的ID为 book.xhtml ,像这样:< h:link outcome =book?id =#{bookCatelogBean.id}value =#{bookCatelogBean.title}/>
我知道如何使用 BookCatelogBean 显示1 book 但我想显示所有的?我有一个想法从 BookCatelogBean 调用 getAllBooks()返回每个书籍标题的方法,我将每个人返回index.xhtml作为JavaserverFace链接而不是字符串?
感谢
这是我的代码:
Book.java
包书店;
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 / p>
包书店;
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 // ENhttp://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 < table> 表示< td> 的列。两个组件可以包括 List< E> 作为值。
bean如下:
@ManagedBean
@RequestScoped
public class BookCatalog implements Serializable {
private List< Book>图书;
@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));
}
公开列表< Book> getBooks(){
return books;
}
}
$ c>< ui:repeat> 以生成例如< ul>< li> :
< ul>
< ui:repeat value =#{bookCatalog.books}var =book>
< li>
< h:link value =#{book.title}outcome =book>
< f:param name =idvalue =#{book.id}/>
< / h:link>
< / li>
< / ui:repeat>
< / ul>
属性基本上暴露当前迭代的项目在组件内的EL范围内的给定名称)
下面是如何使用< h:dataTable> 代替:
< h :dataTable value =#{bookCatalog.books}var =book>
< h:column>
< h:link value =#{book.title}outcome =book>
< f:param name =idvalue =#{book.id}/>
< / h:link>
< / h:column>
< / h:dataTable>
对于JSTL < c:forEach> ,这也是可能的,但你应该记住JSTL标签有不同的生命周期比JSF组件。长故事: p>
另请参阅:
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>
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:
- 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
这篇关于如何将Bean中的项目列表显示到JSF网页上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!