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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…