Jean-Francois Leveque

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

1 -package org.legrog.web.book;
2 -
3 -import org.legrog.entities.Book;
4 -
5 -import javax.enterprise.context.RequestScoped;
6 -import javax.inject.Inject;
7 -import javax.inject.Named;
8 -import java.util.List;
9 -
10 -@Named
11 -@RequestScoped
12 -public class BookBean {
13 -
14 - @Inject
15 - private BookService bookServiceSpring;
16 -
17 - private List<Book> booksAvailable;
18 - private String bookTitle;
19 -
20 - public String getBookTitle() {
21 - return bookTitle;
22 - }
23 -
24 - public void setBookTitle(String bookTitle) {
25 - this.bookTitle = bookTitle;
26 - }
27 -
28 - public List<Book> getBooksAvailable() {
29 - return booksAvailable;
30 - }
31 -
32 - public void setBooksAvailable(List<Book> booksAvailable) {
33 - this.booksAvailable = booksAvailable;
34 - }
35 -
36 - public String fetchBooks()
37 - {
38 - booksAvailable= bookServiceSpring.getAllBooks();
39 - return "success";
40 - }
41 -
42 - public String add()
43 - {
44 - Book book = new Book();
45 - book.setBookTitle(bookTitle);
46 - bookServiceSpring.addBook(book);
47 - return "success";
48 - }
49 -}
1 -package org.legrog.web.book;
2 -
3 -import org.legrog.entities.Book;
4 -
5 -import java.util.List;
6 -
7 -/**
8 - * Created by jai on 27/08/16.
9 - */
10 -public interface BookService {
11 - void addBook(Book book);
12 -
13 - List<Book> getAllBooks();
14 -}
1 -package org.legrog.web.book;
2 -
3 -import org.legrog.entities.Book;
4 -import org.slf4j.Logger;
5 -import org.slf4j.LoggerFactory;
6 -
7 -import javax.ejb.Stateless;
8 -import javax.enterprise.inject.Alternative;
9 -import javax.inject.Inject;
10 -import javax.persistence.EntityManager;
11 -import javax.persistence.criteria.CriteriaQuery;
12 -import java.util.List;
13 -
14 -@Stateless @Alternative
15 -public class BookServiceOld implements BookService{
16 -
17 - Logger logger = LoggerFactory.getLogger(getClass());
18 -
19 - @Inject
20 - private EntityManager entityManager;
21 -
22 - public void addBook(Book book)
23 - {
24 - logger.debug("old one");
25 - entityManager.persist(book);
26 - }
27 -
28 -
29 - public List<Book> getAllBooks()
30 - {
31 - CriteriaQuery<Book> cq = entityManager.getCriteriaBuilder().createQuery(Book.class);
32 - cq.select(cq.from(Book.class));
33 - return entityManager.createQuery(cq).getResultList();
34 - }
35 -
36 - public BookServiceOld() {
37 - }
38 -
39 - // package only constructor dedicated to injection
40 - BookServiceOld(EntityManager entityManager) {
41 - this.entityManager = entityManager;
42 - }
43 -}
1 -package org.legrog.web.book;
2 -
3 -
4 -import org.legrog.entities.Book;
5 -import org.legrog.entities.BookRepository;
6 -import org.legrog.web.book.BookService;
7 -import org.slf4j.Logger;
8 -import org.slf4j.LoggerFactory;
9 -
10 -import javax.ejb.Stateless;
11 -import javax.inject.Inject;
12 -import java.util.List;
13 -
14 -
15 -@Stateless
16 -public class BookServiceSpring implements BookService {
17 -
18 - Logger logger = LoggerFactory.getLogger(getClass());
19 -
20 - @Inject
21 - BookRepository bookRepository;
22 -
23 - public void addBook(Book book) {
24 - logger.debug("spring one");
25 - bookRepository.save(book);
26 - }
27 -
28 - public List<Book> getAllBooks() {
29 - return bookRepository.findAll();
30 - }
31 -}
1 -<?xml version="1.0" encoding="UTF-8"?>
2 -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 -<html xmlns="http://www.w3.org/1999/xhtml"
5 - xmlns:f="http://java.sun.com/jsf/core"
6 - xmlns:h="http://java.sun.com/jsf/html">
7 -
8 -
9 -<h:body bgcolor="white">
10 - <f:view>
11 - <h:form>
12 - <h:panelGrid columns="2">
13 - <h:outputText value='Enter book title'/>
14 - <h:inputText value='#{bookBean.bookTitle}'/>
15 - <h:outputText value='Add'/>
16 - <h:commandButton action="#{bookBean.add}" value="Add"/>
17 - </h:panelGrid>
18 - </h:form>
19 - </f:view>
20 -</h:body>
21 -</html>
1 -<?xml version="1.0" encoding="UTF-8"?>
2 -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 -
5 -<html xmlns="http://www.w3.org/1999/xhtml"
6 - xmlns:f="http://xmlns.jcp.org/jsf/core"
7 - xmlns:h="http://xmlns.jcp.org/jsf/html"
8 - xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
9 -
10 -<h:body>
11 - <f:view>
12 - <h:form id="mainForm">
13 - <h2>Book Added !</h2>
14 - <h:commandLink action="back">
15 - <h:outputText value="Add more books"/>
16 - </h:commandLink>
17 -
18 -
19 - <h:commandButton action="#{bookBean.fetchBooks}" value="View books present"/>
20 -
21 - <br/>
22 - <ui:repeat value="#{bookBean.booksAvailable}" var="book">
23 - #{book.bookTitle} <br/>
24 - </ui:repeat>
25 -
26 - </h:form>
27 - </f:view>
28 -</h:body>
29 -</html>
...\ No newline at end of file ...\ No newline at end of file