Jean-Francois Leveque

Suppression de l'ancien exemple d'implémentation.

package org.legrog.web.book;
import org.legrog.entities.Book;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;
@Named
@RequestScoped
public class BookBean {
@Inject
private BookService bookServiceSpring;
private List<Book> booksAvailable;
private String bookTitle;
public String getBookTitle() {
return bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public List<Book> getBooksAvailable() {
return booksAvailable;
}
public void setBooksAvailable(List<Book> booksAvailable) {
this.booksAvailable = booksAvailable;
}
public String fetchBooks()
{
booksAvailable= bookServiceSpring.getAllBooks();
return "success";
}
public String add()
{
Book book = new Book();
book.setBookTitle(bookTitle);
bookServiceSpring.addBook(book);
return "success";
}
}
package org.legrog.web.book;
import org.legrog.entities.Book;
import java.util.List;
/**
* Created by jai on 27/08/16.
*/
public interface BookService {
void addBook(Book book);
List<Book> getAllBooks();
}
package org.legrog.web.book;
import org.legrog.entities.Book;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.enterprise.inject.Alternative;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import java.util.List;
@Stateless @Alternative
public class BookServiceOld implements BookService{
Logger logger = LoggerFactory.getLogger(getClass());
@Inject
private EntityManager entityManager;
public void addBook(Book book)
{
logger.debug("old one");
entityManager.persist(book);
}
public List<Book> getAllBooks()
{
CriteriaQuery<Book> cq = entityManager.getCriteriaBuilder().createQuery(Book.class);
cq.select(cq.from(Book.class));
return entityManager.createQuery(cq).getResultList();
}
public BookServiceOld() {
}
// package only constructor dedicated to injection
BookServiceOld(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
package org.legrog.web.book;
import org.legrog.entities.Book;
import org.legrog.entities.BookRepository;
import org.legrog.web.book.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.List;
@Stateless
public class BookServiceSpring implements BookService {
Logger logger = LoggerFactory.getLogger(getClass());
@Inject
BookRepository bookRepository;
public void addBook(Book book) {
logger.debug("spring one");
bookRepository.save(book);
}
public List<Book> getAllBooks() {
return bookRepository.findAll();
}
}
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body bgcolor="white">
<f:view>
<h:form>
<h:panelGrid columns="2">
<h:outputText value='Enter book title'/>
<h:inputText value='#{bookBean.bookTitle}'/>
<h:outputText value='Add'/>
<h:commandButton action="#{bookBean.add}" value="Add"/>
</h:panelGrid>
</h:form>
</f:view>
</h:body>
</html>
<?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:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:body>
<f:view>
<h:form id="mainForm">
<h2>Book Added !</h2>
<h:commandLink action="back">
<h:outputText value="Add more books"/>
</h:commandLink>
<h:commandButton action="#{bookBean.fetchBooks}" value="View books present"/>
<br/>
<ui:repeat value="#{bookBean.booksAvailable}" var="book">
#{book.bookTitle} <br/>
</ui:repeat>
</h:form>
</f:view>
</h:body>
</html>
\ No newline at end of file