I have a web application that allows me to add a Book object to a list displayed as a "datatable" using JSF, EJB, MVC, JPA.
The View code "listBooks.xhtml":
<h:dataTable value="#{bookController.booklist}" var="elementBook" border="1" cellpadding="5">
<h:column>
<f:facet name="isbn">
<h:outputText value="ISBN"/> </f:facet>
<h:outputText value="#{elementBook.isbn}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Title"/></f:facet>
<h:outputText value="#{elementBook.title}"/>
</h:column>
/* rest of the code */
</h:dataTable>
The View Code "addNewBook.xhtml" :
<h:form>
<h4>
ISBN:
<h:inputText value="#{bookController.book.isbn}" size = "10" /> <br />
Title:
<h:inputText value="#{bookController.book.title}" size = "10" /> <br />
Price:
<h:inputText value="#{bookController.book.price}" size = "10" /> <br />
Description:
<h:inputTextarea value="#{bookController.book.description}" /> <br />
Number of pages:
<h:inputText value="#{bookController.book.bnOfPage}" size = "10" /> <br />
Illustrations:
<h:selectBooleanCheckbox value="#{bookController.book.illustrations}"/> <br />
<h:commandButton value="Create a book" action="#{bookController.doCreateBook()}" />
</h4>
<hr/>
<h2> Librairie en ligne</h2>
</h:form>
Controller Layer :
@ManagedBean
@RequestScoped
public class BookController {
@EJB
private BookEJB bookEJB;
private Book book;
private List<Book> booklist;
public BookController() {
book = new Book();
booklist=new ArrayList();
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public List<Book> getBooklist() {
return booklist;
}
public void setBooklist(List<Book> booklist) {
this.booklist = booklist;
}
public String doCreateBook() {
bookEJB.create(book);
booklist= bookEJB.findAll();
return "listBooks.xhtml";
}
}
Business Logic Layer :
@Stateless
public class BookEJB extends AbstractFacade<Book> {
@PersistenceContext(unitName = "tpbookPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public BookEJB() {
super(Book.class);
}
}
And an Abstract Facade :
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
//......
And finally the Entity "Book" :
@Entity
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private Float price;
private String description;
private String isbn;
private Integer bnOfPage;
private Boolean illustrations;
public Book(){
}
public Integer getBnOfPage() {
return bnOfPage;
}
public String getDescription() {
return description;
}
public Boolean getIllustrations() {
return illustrations;
}
public String getIsbn() {
return isbn;
}
public Float getPrice() {
return price;
}
The controller class "BookController", its job is to use the view to update the model, it declares a book variable private Book book;
and the same for the EJB, private BookEJB bookEJB;
and then using this bookEJB.create(book);
Shouldn't it be like BookEJB bookEJB = new BookEJB();
and then doing the bookEJB.create(book);
in the doCreateBook()
method. Shouldn't we always use the new
word to be able to use the object and access their methods ? because if we used just the name of the class instead we'll access just the static methods.
And then why using the new
Keyword in the BookController constructor ? Book book = new Book();
?