The document discusses the presentation layer in a Java application. It describes the role of the presentation layer as displaying information to users and processing user interactions. Key technologies for the presentation layer mentioned are JavaServer Faces (JSF), JavaServer Pages (JSP), and servlets. The document also provides an example of a managed bean controller class and an XHTML page to illustrate how the model, view, and controller work together in the JSF framework.
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
2014 Pre-MSc-IS-6 Presentation Layer
1. 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
2. Andreas Martin - Page 2
Presentation Layer
Presentation layer
JavaServer Faces
Reference Project:
Lending-Reference-Project-1: Book Lending Example using JSF
6. Presentation Layer
4. 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)
5. 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
6. 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
7. 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
9. 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.»
10. 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
11. 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>
12. Andreas Martin - Page 12
JavaServer Faces 2.0 – DZone Refcardz
6. Presentation Layer
13. Andreas Martin - Page 13
Lending-Reference-Project-1
Book Lending Example using JSF
6. Presentation Layer
14. Andreas Martin - Page 14
Hands-on-7
6. Presentation Layer
Model
View
Controller
Book EJBCustomer 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