2014 pre-msc-is-6 presentation layer
TRANSCRIPT
Andreas Martin - Page 1
Master of Science in Business Information Systems FHNW Pre-Master Information Systems
6. Presentation Layer
Andreas Martin
6. Presentation Layer
http://www.flickr.com/photos/dirk_hofmann/4200450207
Andreas Martin - Page 2
Presentation Layer
Presentation layer
JavaServer Faces
Reference Project:
Lending-Reference-Project-1: Book Lending Example using JSF
6. Presentation Layer
Presentation Layer
http://www.flickr.com/photos/ajmexico/3281139507
The Latte Macchiato «Layering» Principle
http://www.flickr.com/photos/tf28/4367660424
Presentation Layer
Goal: Display of information, processing / forwarding of user interactions. Technologies: JavaServer Faces (JSF), JavaServer Pages (JSP), Servlets, etc.
Business (Logic) Layer
Goal: Reproduction of actions or «verbs» of the application (buy a book, print an order, deliver a book, etc.). Technologies: Enterprise Java Beans (EJBs)
Persistence Layer
Goal: Reproduction of database attributes, information or «nouns» in object / class attributes (Object-Relational Mapping, ORM). Technologies: Java Persistence API (JPA)
Andreas Martin - Page 5
Business Layer
Java EE 7 – Typical Layering including injected POJO’s
6. Presentation Layer
Persistence Layer
@EntityManager (JPA)
(JPA) @Entity (JPA)
Session Bean (EJB)
@PersistenceContext
Presentation Layer
Facelets (JSF) @ManagedBean (JSF)
Facelets (JSF)
Users
Databases
@EJB / @Inject
POJO
@Inject
Andreas Martin - Page 6
Java EE 7 Web Applications
Servlet {1997}:
Java classes, which creates HTML and processes HTTP- requests.
Java ServerPages (JSP) {1999}:
Quasi HTML- pages with Java code embedded.
Java ServerFaces (JSF) {v1 2004; v2 2009}:
Java ServerFaces is a web framework – the basic structure of a web application is given.
Is based on Servlets and XHTML.
JSF is a MVC (Model View Controller) architecture.
Configuration over annotations.
AJAX functionalities.
6. Presentation Layer
Andreas Martin - Page 7
MVC (Model View Controller) Pattern …using JSF
6. Presentation Layer
Adapted from: Goncalves: Code and Models licensed under a CC BY-SA 3.0 License from https://github.com/agoncal/agoncal-book-javaee7
Andreas Martin - Page 8
JSF Procedure
6. Presentation Layer
Web- Container
Faces Servlet
EJB
Entity
XHTML
(1) request
(6) response
(2) (3.2)
(3.1)
(4) (5)
Business Objects
(EJBs, etc.)
Controller Managed
Bean
Andreas Martin - Page 9
Controller Example – Managed Bean
6. Presentation Layer
Listing: BookController Managed Bean @ManagedBean @SessionScoped public class BookController { @EJB private BookEJB bookEJB; private Book book = new Book(); public String doCreateBook() { book = bookEJB.createBook(book); return "listBooks.xhtml"; } // Getters, setters
}
Enterprise JavaBean using Dependency Injection
Entity Bean
«View declaration; what is shown as next.»
Andreas Martin - Page 10
Scopes
Request-Scope (@RequestScoped ) Managed bean lives during a HTTP-request.
View-Scope (@ViewScoped ) Managed bean live as long as a corresponding view is shown..
Session-Scope (@SessionScoped ): Managed beans exists during a user holds a session.
Application-Scope (@ApplicationScoped ): One managed bean lives as long as the whole application is alive (usually
in standalone desktop applications).
6. Presentation Layer
@ApplicationScoped [CDI]
@SessionScoped [CDI]
@ViewScoped [JSF]
@RequestScoped [CDI]
@ConversationScoped [CDI]
Life time
Andreas Martin - Page 11
Model Example - XHTML
6. Presentation Layer
Listing: Snippet of an XHTML Page <!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>Creates a new book</TITLE>
</h:head>
<h:body>
<H1>Create a new book</H1>
<HR />
<h:form>
<TABLE BORDER="0">
<TR>
<TD><h:outputLabel value="ISBN : " /></TD>
<TD><h:inputText value="#{bookController.book.isbn}" /></TD>
<TR>
</TR>
<TD><h:outputLabel value="Title :" />
</TD>
<TD><h:inputText value="#{bookController.book.title}" /></TD>
</TR>
</TABLE>
<h:commandButton value="Create a book"
action="#{bookController.doCreateBook}"
styleClass="submit" />
</h:form>
<HR /> <I>APress - Beginning Java EE 6</I>
</h:body>
</HTML>
Andreas Martin - Page 12
JavaServer Faces 2.0 – DZone Refcardz
6. Presentation Layer
Andreas Martin - Page 13
Lending-Reference-Project-1 Book Lending Example using JSF
6. Presentation Layer
Andreas Martin - Page 14
Hands-on-7
6. Presentation Layer
Mo
de
l
View Controller
Book EJB Customer EJB
Business cases: • Create a customer including address • …CRUD…
Book Lending EJB
<Entity> Address
<Entity> Customer
<Entity> Book Lending
<Entity> Book
n 1 0..n 0..m 1
Business cases: • Lend a book • Return a book • Show all lendings
Business cases: • Create a book • …CRUD..
1
DEMO
http://www.flickr.com/photos/jurvetson/489257240