KEMBAR78
Vaadin 7 - Java Enterprise Edition integration | PDF
Vaadin 7 Java Enterprise Edition Integration 
Peter Lehto 
Vaadin Expert 
@peter_lehto
What is 
Vaadin? 
Java EE 7
Vaadin 
CDI 
Addon 
Structuring 
Vaadin 
App 
Application 
architecture
How to 
get star ted 
QA 
Authentication 
Authorization
User interface 
framework for rich 
web applications
User Interface 
Components
User Interface 
Components
Developer 
Productivity 
Rich 
UX
How does server-side 
UI work, really?
• Loader page 
• CSS Theme 
• Images 
• JavaScript
• Loader page 
• CSS Theme 
• Images 
• JavaScript 
135k 
Compressed & 
reduced 
thin client
• name=”Joonas” 
• button clicked 
! 
261 bytes
• name=”Joonas” 
• button clicked 
! 
261 bytes 
• Add notification 
! 
267 bytes
Java 
Enterprise Edition 7
Collection of Java Specification 
Requests (JSRs) for writing (web) 
applications
Collection of Java Specification 
Requests (JSRs) for writing (web) 
applications 
Application server implements Java 
EE specification
Do you know some 
Java EE specs?
Java Persistence 
API 2.1 (JPA) 
(JSR-338)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
(JSR-343)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340) 
Java Server Faces 
2.2 (JSF) 
(JSR-344)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340) 
Java Server Faces 
Java Server Pages 
2.2 (JSF) 
(JSR-344) 
2.3 (JSF) 
(JSR-245)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340) 
Java Server Faces 
Java Server Pages 
2.2 (JSF) 
(JSR-344) 
2.3 (JSF) 
(JSR-245) Bean Validation 1.1 
(JSR-349)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340) 
Java Server Faces 
Java Server Pages 
2.2 (JSF) 
(JSR-344) 
2.3 (JSF) 
(JSR-245) Bean Validation 1.1 
Int(eJSrRc-e34p9)tors 1.2 
(JSR-318)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340) 
Java Server Faces 
Java Server Pages 
2.2 (JSF) 
(JSR-344) 
2.3 (JSF) 
(JSR-245) Bean Validation 1.1 
Int(eJSrRc-e34p9)tors 1.2 
(JSR-318) Java Transaction 
API 1.2 (JTA) 
(JSR-907)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340) 
Java Server Faces 
Java Server Pages 
2.2 (JSF) 
(JSR-344) 
2.3 (JSF) 
(JSR-245) Bean Validation 1.1 
Int(eJSrRc-e34p9)tors 1.2 
(JSR-318) Java Transaction 
API 1.2 (JTA) 
(JSR-907) 
Java API for RESTful 
Web Services 2.0 
(JAX-RS) 
(JSR-907)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340) 
Java Server Faces 
Java Server Pages 
2.2 (JSF) 
(JSR-344) 
2.3 (JSF) 
(JSR-245) Bean Validation 1.1 
Int(eJSrRc-e34p9)tors 1.2 
(JSR-318) Java Transaction 
API 1.2 (JTA) 
(JSR-907) 
Java API for RESTful 
Web Services 2.0 
Java API for XML based 
Web Services 2.2 
(JAX-RS) 
(JSR-907) 
(JAX-WS) 
(JSR-907)
Java Persistence 
API 2.1 (JPA) 
Enterprise Java 
Beans (JSR-338) 
3.2 (EJB) 
(JSR-345) Java Servlet 3.1 
(JSR-340) Java Message 
Service 2.0 (JMS) 
Context and 
Dependency 
Injection (JSR-343) 
1.1 (CDI) 
(JSR-340) 
Java Server Faces 
Java Server Pages 
2.2 (JSF) 
(JSR-344) 
2.3 (JSF) 
(JSR-245) Bean Validation 1.1 
Int(eJSrRc-e34p9)tors 1.2 
(JSR-318) Java Transaction 
API 1.2 (JTA) 
(JSR-907) 
Java API for RESTful 
Web Services 2.0 
Java API for XML based 
Web Services 2.2 
(JAX-RS) 
(JSR-907) 
Java Architecture for 
XML Binding 2.2 
(JAX-WS) 
(JSR-907) 
(JAX-B) 
(JSR-222)
Which APIs should I know? 
APIs that are part of “Web Profile”
Java Servlet 3.1 
(JSR-340)
Java Servlet 3.1 
Ja(JvSaR- 3P40e) rsistence 
API 2.1 (JPA) 
(JSR-338)
Java Servlet 3.1 
Ja(JvSaR- 3P40e) rsistence 
API 2.1 (JPA) 
E(JnStRe-3r3p8r) ise Java 
Beans 3.2 (EJB) 
(JSR-345)
Java Servlet 3.1 
Ja(JvSaR- 3P40e) rsistence 
API 2.1 (JPA) 
E(JnStRe-3r3p8r) ise Java 
Beans 3.2 (EJB) 
Context and 
(JSR-Dependency 
345) 
Injection 1.1 (CDI) 
(JSR-340)
Java Servlet 3.1 
Ja(JvSaR- 3P40e) rsistence 
API 2.1 (JPA) 
E(JnStRe-3r3p8r) ise Java 
Beans 3.2 (EJB) 
Context and 
(JSR-Dependency 
345) 
Injection 1.1 (CDI) 
Java Message 
Service (JSR-340) 
2.0 (JMS) 
(JSR-343)
Java Servlet 3.1 
Ja(JvSaR- 3P40e) rsistence 
API 2.1 (JPA) 
E(JnStRe-3r3p8r) ise Java 
Beans 3.2 (EJB) 
Context and 
(JSR-Dependency 
345) 
Injection 1.1 (CDI) 
Java Message 
Service (JSR-340) 
2.0 (JMS) 
Bea(JSnR -V34a3l)idation 1.1 
(JSR-349)
Java Servlet 3.1 
Ja(JvSaR- 3P40e) rsistence 
API 2.1 (JPA) 
E(JnStRe-3r3p8r) ise Java 
Beans 3.2 (EJB) 
Context and 
(JSR-Dependency 
345) 
Injection 1.1 (CDI) 
Java Message 
Service (JSR-340) 
2.0 (JMS) 
Bea(JSnR -V34a3l)idation 1.1 
(JSR-349) 
Interceptors 1.2 
(JSR-318)
Java Servlet 3.1 
Ja(JvSaR- 3P40e) rsistence 
API 2.1 (JPA) 
E(JnStRe-3r3p8r) ise Java 
Beans 3.2 (EJB) 
Context and 
(JSR-Dependency 
345) 
Injection 1.1 (CDI) 
Java Message 
Service (JSR-340) 
2.0 (JMS) 
Bea(JSnR -V34a3l)idation 1.1 
(JSR-349) 
Interceptors 1.2 
Jav(JaS RA-3P1I8 )for RESTful 
Web Services 2.0 
(JAX-RS) 
(JSR-907)
Java Persistence API 2.1 
(JPA)
@Entity 
______________________________ 
Customer 
@Id 
@AutoGenerated 
Long id; 
! 
@Column(nullable = false) 
! 
String name; 
! 
Date birthdate;
@Entity 
______________________________ 
Customer 
! 
Customer 
Id name birthdate 
1 Alex 07.02.1984 
2 John 18.2.1992 
@Id 
@AutoGenerated 
Long id; 
! 
@Column(nullable = false) 
String name; 
! 
Date birthdate;
@Entity 
______________________________ 
Customer 
@Id 
@AutoGenerated 
Long id; 
! 
@Column(nullable = false) 
! 
String name; 
! 
Date birthdate; 
! 
@OneToMany(mappedBy=“customer”) 
List<Invoice> invoices; 
Customer 
Id name birthdate 
1 Alex 07.02.1984 
2 John 18.2.1992 
Invoice 
Id customer number 
1 1 123 
2 1 124
Enterprise Java Beans 3.2 
(EJB)
Enterprise Java Beans 
For defining business layer with 
service methods
Enterprise Java Beans 
For defining business layer with 
service methods 
For local and remote services
Enterprise Java Beans 
For defining business layer with 
service methods 
For local and remote services 
For transaction boundaries
(UI) 
CustomerView 
!
(@Remote) 
CustomerService 
(UI) 
CustomerView 
! 
@Remote 
@Local
(@Remote) 
CustomerService 
(UI) 
CustomerView 
! 
(@Stateless) 
(@Stateless) 
CustomerService 
CustomerService 
Bean 
@Remote 
@Local 
@Stateless 
@Stateful 
@Singleton 
(@Stateless) 
CustomerService 
Bean 
Bean
(DB) 
Customer 
Database
! 
@Local! 
public interface CustomerService {! 
! 
! void storeCustomers(Collection<Customer> customers);! 
! 
! void removeCustomers(Collection<Customer> customers);! 
! 
! Collection<Customer> getAllCustomers()! 
! 
! Optional<Customer> getCustomerByName(String name);! 
}
! 
@Stateless! 
public class UserServiceBean implements ! 
! UserService {! 
! 
! @PersistenceContext! 
! private EntityManager em;! 
! ! 
! public void storeUsers(Collection<User> users) {! 
! ! users.forEach(user -> storeUser(user));! 
! }! 
! 
! public void storeUser(User user) {! 
! ! em.merge(user);! 
! }! 
}
Context and 
Dependency 
Injection 1.1 (CDI)
Context and Dependency 
Injection 
Instead of saying new say @Inject
Context and Dependency 
Injection 
Instead of saying new say @Inject 
Decouples code and lets container 
manage dependencies
Context and Dependency 
Injection 
Injections occur within a scope
Context and Dependency 
Injection 
Injections occur within a scope 
@ApplicationScoped 
@SessionScoped 
@RequestScoped
Context and Dependency 
Injection 
Injections occur within a scope 
@ApplicationScoped 
@SessionScoped 
@RequestScoped 
@UIScoped 
@ViewScoped
@Stateless 
CustomerService 
_________________ 
! 
@EJB 
InvoiceService invoices; 
@UIScoped 
AppUI 
_________________ 
! 
@EJB 
CustomerService service; 
! 
@Inject 
MainMenu mainMenu; 
! 
@Inject 
User currentUser; 
@UIScoped 
MainMenu 
_________________ 
! 
@Stateless 
InvoiceService 
@Inject 
Event<MenuEvent> menuEventSource;
Integration to EE through 
Vaadin CDI
UI and View as managed bean with 
@CDIUI and @CDIView
UI and View as managed bean with 
@CDIUI and @CDIView 
Allows annotations like @Inject and 
@EJB to be processed inside Vaadin 
beans.
UI and View as managed bean with 
@CDIUI and @CDIView 
Allows annotations like @Inject and 
@EJB to be processed inside Vaadin 
beans. 
Easily reference EE objects
@CDIUI(“”)! 
public class AppUI extends UI {! 
! 
}
@CDIUI(“”)! 
public class AppUI extends UI {! 
! ! 
! @Inject! 
! private MainMenu mainMenu;! 
! 
! @Inject! 
private User currentUser;! 
! 
! @Inject! 
! private ViewManager viewManager;! 
! ! 
! public void init(VaadinRequest request) {! 
! ! VerticalLayout layout = new VerticalLayout();! 
! ! layout.addComponent(mainMenu);! 
! 
! ! setContent(layout);! 
! }! 
}
@UIScoped 
AppUI 
_____________________________ 
! 
@Inject 
private MainMenu menu; 
! 
@Inject 
private ViewManager viewMgr; 
! 
@Inject 
private User loggedInUser; 
<<UIScope>> 
! 
MainMenu 
ViewManager 
! 
<<SessionScope>> 
! 
User
<<UIScope>> 
<<UIScope>> 
<<UIScope>> 
! 
MenuBar 
Footer 
ViewManager 
! 
<<SessionScope>> 
! 
User 
@UIScoped 
AppUI 
_____________________________ 
! 
@Inject 
private MenuBar menu; 
! 
! 
@Inject 
private ViewManager viewMgr; 
! 
@Inject 
private User loggedInUser; 
<<UIScope>> 
! 
MenuBar 
Footer 
ViewManager 
! 
! 
MenuBar 
Footer 
ViewManager 
! 
! 
MenuBar 
ViewManager 
!
VaadinServlet
VaadinServlet 
HttpSession 
1 
n
VaadinServlet 
1 
n 
1 
HttpSession VaadinSession 
1 1 
n
VaadinServlet 
1 
HttpSession VaadinSession 
UI 
1 
n 
1 1 
n 
1 
*
@UIScoped
@UIScoped 
Necessary to acquire UI specific 
instances
@UIScoped 
Necessary to acquire UI specific 
instances 
CDI context to map beans per UI 
instance
@UIScoped 
Necessary to acquire UI specific 
instances 
CDI context to map beans per UI 
instance 
@UIScoped
@UIScoped! 
public class MainMenu extends CustomComponent {! 
! 
}
@UIScoped! 
public class MainMenu extends CustomComponent {! 
! 
! @Inject! 
! private Event<NavigationEvent> eventSource;! 
! 
! 
! protected void onMenuItemClicked(MenuItem item) {! 
! ! eventSource.fireEvent(new NavigationEvent(item));! 
! }! 
}
@CDIUI(“”)! 
public class AppUI extends UI {! 
! ! 
! …! 
! 
! protected void onNavigationEvent(@Observes! 
! ! ! ! ! ! ! ! ! ! ! ! ! ! NavigationEvent event) {! 
! 
viewMgr.navigateTo(event.getView());! 
! }! 
}
Structuring Vaadin App with 
Model View Presenter
Do you like 
spaghetti?
Do you like 
spaghetti? 
Let’s clean it!
History behind MVP
Late 1970s
Late 1970s 
Originally Model-View-Controller
Late 1970s 
Originally Model-View-Controller 
SmallTalk-80
Late 1970s 
Originally Model-View-Controller 
SmallTalk-80 
Controller is mediator between 
end user and application
Late 1970s 
Originally Model-View-Controller 
SmallTalk-80 
Controller is mediator between 
end user and application 
Model notifies about changes 
with Observer pattern
1980s
1980s 
Taligent Model-View-Presenter
1980s 
Taligent Model-View-Presenter 
Influenced by SmallTalk-80
1980s 
Taligent Model-View-Presenter 
Influenced by SmallTalk-80 
Model, View, Presenter, Interactors, 
Commands, Selections
1980s 
Taligent Model-View-Presenter 
Influenced by SmallTalk-80 
Model, View, Presenter, Interactors, 
Commands, Selections 
Presenter orchestrates the 
structure, not the input
late 1980s
late 1980s 
Simplified Taligent MVP
late 1980s 
Simplified Taligent MVP 
Views handle input events 
initially
late 1980s 
Simplified Taligent MVP 
Views handle input events 
initially 
Model may fire events
late 1980s 
Simplified Taligent MVP 
Views handle input events 
initially 
Model may fire events 
Presenter handles the logic, not 
the user input control
late 1980s 
Simplified Taligent MVP 
Views handle input events 
initially 
Model may fire events 
Presenter handles the logic, not 
the user input control
UI layer MVP targets 
Clean code best practices 
like…
Single responsibility principle 
Class should change only for one reason
Open closed principle 
Open for extension, closed for change
Today 
many MVP variations exists 
Here’s one…
Model-View-Presenter 
Presenter 
View Model
Model-View-Presenter 
Presenter 
View Model
Model-View-Presenter 
View Impl 
Presenter 
View Model
Example 
EditorView 
- Button saveButton;! 
- Button cancelButton;! 
- FieldGroup personFieldGroup; 
- saveButtonClicked()! 
- cancelButtonClicked() 
ClickListener 
+ buttonClicked()
Presenter 
+ saveButtonClicked()! 
+ cancelButtonClicked()! 
Example 
EditorView 
- Button saveButton;! 
- Button cancelButton;! 
- FieldGroup personFieldGroup; 
+ commitChanges()! 
+ discardChanges() 
ClickListener 
+ buttonClicked()
Presenter 
+ saveButtonClicked()! 
+ cancelButtonClicked()! 
+ commitChanges()! 
+ discardChanges() 
Example 
EditorViewImpl 
- Button saveButton;! 
- Button cancelButton;! 
- FieldGroup personFieldGroup; 
+ commitChanges()! 
+ discardChanges() 
ClickListener 
+ buttonClicked() 
EditorView
Benefits of MVP
Benefits of MVP 
Separation of concerns simplifying code
Benefits of MVP 
Separation of concerns simplifying code 
UI logic becomes separated from 
rendering providing testability
Benefits of MVP 
Separation of concerns simplifying code 
UI logic becomes separated from 
rendering providing testability 
Keeps spaghetti in a cup by 
following clean code principles
Application 
Architecture
Client 
Browser View 
Business Persistency 
<<EJB>> 
Business 
Logic 
Server-side-UI 
Presenter <<JPA>> 
Persistency
Server-side-UI 
Client 
Browser View
public interface CustomerView extends 
ApplicationView<CustomerViewPresenter> { 
! 
}
public interface CustomerView extends 
ApplicationView<CustomerViewPresenter> { 
! 
void populateCustomers(Collection<Customer> customers); 
! 
void openEditorFor(Customer customer); 
void closeEditor(); 
void removeTableSelection(); 
}
Server-side-UI 
Client 
Browser View 
Presenter
@ViewScoped 
public class CustomerViewPresenter extends AbstractPresenter<CustomerView> { 
! 
}
@ViewScoped 
public class CustomerViewPresenter extends AbstractPresenter<CustomerView> { 
! 
@EJB 
private CustomerService customerService; 
! 
@Override 
protected void onViewEnter() { 
getView().populateCustomers(customerService.getAllCustomers()); 
} 
}
@ViewScoped 
public class CustomerViewPresenter extends AbstractPresenter<CustomerView> { 
! 
@EJB 
private CustomerService customerService; 
! 
@Override 
protected void onViewEnter() { 
getView().populateCustomers(customerService.getAllCustomers()); 
} 
! 
public void onCustomerSaved(@Observes CustomerSavedEvent event) { … } 
! 
public void onCustomerRemoved(@Observes CustomerRemovedEvent event) { … } 
! 
public void onCustomerSelected(@Observes CustomerSelectedEvent event) { … } 
}
Client 
Browser View 
<<EJB>> 
Business 
Logic 
Server-side-UI 
Presenter 
Business
@Local 
public interface CustomerService { 
! 
void storeCustomers(Collection<Customer> customers); 
! 
void removeCustomers(Collection<Customer> customers); 
! 
Collection<Customer> getAllCustomers(); 
! 
Optional<Customer> getCustomerByUsername(String username); 
}
@Stateless 
@TransactionAttribute(Application 
TransactionAttributeType.REQUIRED) 
public Architecture 
class CustomerServiceBean implements CustomerService { 
! 
@PersistenceContext(unitName = "appUnit") 
private EntityManager entityManager; 
! 
@Override 
public void storeCustomers(Collection<Customer> customers) { 
customers.forEach(cu -> entityManager.merge(cu)); 
} 
! 
@Override 
public Collection<Customer> getAllCustomers() { 
return entityManager.createQuery(query).getResultList(); 
} 
! 
… 
}
Client 
Browser View 
Business Persistency 
<<EJB>> 
Business 
Logic 
Server-side-UI 
Presenter <<JPA>> 
Persistency
@Entity 
public Application 
Architecture 
class Customer { 
! 
@Id 
@AutoGenerated 
private Long id; 
! 
private String name; 
! 
@Temporal(DATE) 
private Date birthDate; 
! 
public boolean isPersisted() { 
return id != null; 
} 
! 
… 
}
<persistence-unit name="appUnit" transaction-type="JTA"> 
<jta-data-source>jdbc/app-backend</jta-data-source> 
<class>org.vaadin.example.backend.entity.Customer</class> 
! 
<properties> 
<property name="…" … /> 
</properties> 
</persistence-unit>
Authentication and 
Authorization
com.vaadin.cdi.access.AccessControl
com.vaadin.cdi.access.AccessControl 
@RolesAllowed
com.vaadin.cdi.access.AccessControl 
@RolesAllowed 
isUserSignedIn(), 
isUserInRole(String), 
getPrincipalName() 
isUserHavingAccessToView(ViewBean)
com.vaadin.cdi.access.AccessControl 
@RolesAllowed 
isUserSignedIn(), 
isUserInRole(String), 
getPrincipalName() 
isUserHavingAccessToView(ViewBean) 
Can be replaced with @Alternative
How to 
Get Started?
<dependency 
<groupId>com.vaadin</groupId> 
<artifactId>vaadin-cdi</artifactId> 
<version>1.0.0.beta4</version> 
</dependency> 
! 
<repository> 
<id>vaadin-addons</id> 
<url>http://maven.vaadin.com/vaadin-addons</url> 
</repository>
Eclipse 
Download plugin 
from Martketplace
IntelliJ IDEA 
Built-in support
Netbeans 
Download plugin 
Netbeans Plugin Portal
Example App 
github.com/peterl1084/cdiexample
Lessons learned today
Lessons learned today 
1. Java EE contains tons of specifications for enterprise apps
Lessons learned today 
1. Java EE contains tons of specifications for enterprise apps 
2. Vaadin is a great way to assemble web apps with components
Lessons learned today 
1. Java EE contains tons of specifications for enterprise apps 
2. Vaadin is a great way to assemble web apps with components 
3. Combining Vaadin with Java EE works best through CDI
Lessons learned today 
1. Java EE contains tons of specifications for enterprise apps 
2. Vaadin is a great way to assemble web apps with components 
3. Combining Vaadin with Java EE works best through CDI 
4. Following best practices makes your code maintainable
Lessons learned today 
1. Java EE contains tons of specifications for enterprise apps 
2. Vaadin is a great way to assemble web apps with components 
3. Combining Vaadin with Java EE works best through CDI 
4. Following best practices makes your code maintainable 
5. Vaadin is a Finnish word meaning Reindeer
Download for Free 
vaadin.com/book 
PDF, ePub, HTML 
ISBN 978-952-93-1970-1 
9789529319701 
ISBN 978-952-93-1970-1 
9789529319701 
Vol 1 
288 pages 
Vol 2 
434 pages
Questions or 
Comments? 
Peter Lehto 
Vaadin Expert 
@peter_lehto

Vaadin 7 - Java Enterprise Edition integration

  • 1.
    Vaadin 7 JavaEnterprise Edition Integration Peter Lehto Vaadin Expert @peter_lehto
  • 2.
    What is Vaadin? Java EE 7
  • 3.
    Vaadin CDI Addon Structuring Vaadin App Application architecture
  • 4.
    How to getstar ted QA Authentication Authorization
  • 7.
    User interface frameworkfor rich web applications
  • 8.
  • 9.
  • 10.
  • 11.
    How does server-side UI work, really?
  • 13.
    • Loader page • CSS Theme • Images • JavaScript
  • 14.
    • Loader page • CSS Theme • Images • JavaScript 135k Compressed & reduced thin client
  • 17.
    • name=”Joonas” •button clicked ! 261 bytes
  • 19.
    • name=”Joonas” •button clicked ! 261 bytes • Add notification ! 267 bytes
  • 20.
  • 21.
    Collection of JavaSpecification Requests (JSRs) for writing (web) applications
  • 22.
    Collection of JavaSpecification Requests (JSRs) for writing (web) applications Application server implements Java EE specification
  • 25.
    Do you knowsome Java EE specs?
  • 26.
    Java Persistence API2.1 (JPA) (JSR-338)
  • 27.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345)
  • 28.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340)
  • 29.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) (JSR-343)
  • 30.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340)
  • 31.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340) Java Server Faces 2.2 (JSF) (JSR-344)
  • 32.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340) Java Server Faces Java Server Pages 2.2 (JSF) (JSR-344) 2.3 (JSF) (JSR-245)
  • 33.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340) Java Server Faces Java Server Pages 2.2 (JSF) (JSR-344) 2.3 (JSF) (JSR-245) Bean Validation 1.1 (JSR-349)
  • 34.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340) Java Server Faces Java Server Pages 2.2 (JSF) (JSR-344) 2.3 (JSF) (JSR-245) Bean Validation 1.1 Int(eJSrRc-e34p9)tors 1.2 (JSR-318)
  • 35.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340) Java Server Faces Java Server Pages 2.2 (JSF) (JSR-344) 2.3 (JSF) (JSR-245) Bean Validation 1.1 Int(eJSrRc-e34p9)tors 1.2 (JSR-318) Java Transaction API 1.2 (JTA) (JSR-907)
  • 36.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340) Java Server Faces Java Server Pages 2.2 (JSF) (JSR-344) 2.3 (JSF) (JSR-245) Bean Validation 1.1 Int(eJSrRc-e34p9)tors 1.2 (JSR-318) Java Transaction API 1.2 (JTA) (JSR-907) Java API for RESTful Web Services 2.0 (JAX-RS) (JSR-907)
  • 37.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340) Java Server Faces Java Server Pages 2.2 (JSF) (JSR-344) 2.3 (JSF) (JSR-245) Bean Validation 1.1 Int(eJSrRc-e34p9)tors 1.2 (JSR-318) Java Transaction API 1.2 (JTA) (JSR-907) Java API for RESTful Web Services 2.0 Java API for XML based Web Services 2.2 (JAX-RS) (JSR-907) (JAX-WS) (JSR-907)
  • 38.
    Java Persistence API2.1 (JPA) Enterprise Java Beans (JSR-338) 3.2 (EJB) (JSR-345) Java Servlet 3.1 (JSR-340) Java Message Service 2.0 (JMS) Context and Dependency Injection (JSR-343) 1.1 (CDI) (JSR-340) Java Server Faces Java Server Pages 2.2 (JSF) (JSR-344) 2.3 (JSF) (JSR-245) Bean Validation 1.1 Int(eJSrRc-e34p9)tors 1.2 (JSR-318) Java Transaction API 1.2 (JTA) (JSR-907) Java API for RESTful Web Services 2.0 Java API for XML based Web Services 2.2 (JAX-RS) (JSR-907) Java Architecture for XML Binding 2.2 (JAX-WS) (JSR-907) (JAX-B) (JSR-222)
  • 39.
    Which APIs shouldI know? APIs that are part of “Web Profile”
  • 40.
    Java Servlet 3.1 (JSR-340)
  • 41.
    Java Servlet 3.1 Ja(JvSaR- 3P40e) rsistence API 2.1 (JPA) (JSR-338)
  • 42.
    Java Servlet 3.1 Ja(JvSaR- 3P40e) rsistence API 2.1 (JPA) E(JnStRe-3r3p8r) ise Java Beans 3.2 (EJB) (JSR-345)
  • 43.
    Java Servlet 3.1 Ja(JvSaR- 3P40e) rsistence API 2.1 (JPA) E(JnStRe-3r3p8r) ise Java Beans 3.2 (EJB) Context and (JSR-Dependency 345) Injection 1.1 (CDI) (JSR-340)
  • 44.
    Java Servlet 3.1 Ja(JvSaR- 3P40e) rsistence API 2.1 (JPA) E(JnStRe-3r3p8r) ise Java Beans 3.2 (EJB) Context and (JSR-Dependency 345) Injection 1.1 (CDI) Java Message Service (JSR-340) 2.0 (JMS) (JSR-343)
  • 45.
    Java Servlet 3.1 Ja(JvSaR- 3P40e) rsistence API 2.1 (JPA) E(JnStRe-3r3p8r) ise Java Beans 3.2 (EJB) Context and (JSR-Dependency 345) Injection 1.1 (CDI) Java Message Service (JSR-340) 2.0 (JMS) Bea(JSnR -V34a3l)idation 1.1 (JSR-349)
  • 46.
    Java Servlet 3.1 Ja(JvSaR- 3P40e) rsistence API 2.1 (JPA) E(JnStRe-3r3p8r) ise Java Beans 3.2 (EJB) Context and (JSR-Dependency 345) Injection 1.1 (CDI) Java Message Service (JSR-340) 2.0 (JMS) Bea(JSnR -V34a3l)idation 1.1 (JSR-349) Interceptors 1.2 (JSR-318)
  • 47.
    Java Servlet 3.1 Ja(JvSaR- 3P40e) rsistence API 2.1 (JPA) E(JnStRe-3r3p8r) ise Java Beans 3.2 (EJB) Context and (JSR-Dependency 345) Injection 1.1 (CDI) Java Message Service (JSR-340) 2.0 (JMS) Bea(JSnR -V34a3l)idation 1.1 (JSR-349) Interceptors 1.2 Jav(JaS RA-3P1I8 )for RESTful Web Services 2.0 (JAX-RS) (JSR-907)
  • 48.
  • 49.
    @Entity ______________________________ Customer @Id @AutoGenerated Long id; ! @Column(nullable = false) ! String name; ! Date birthdate;
  • 50.
    @Entity ______________________________ Customer ! Customer Id name birthdate 1 Alex 07.02.1984 2 John 18.2.1992 @Id @AutoGenerated Long id; ! @Column(nullable = false) String name; ! Date birthdate;
  • 51.
    @Entity ______________________________ Customer @Id @AutoGenerated Long id; ! @Column(nullable = false) ! String name; ! Date birthdate; ! @OneToMany(mappedBy=“customer”) List<Invoice> invoices; Customer Id name birthdate 1 Alex 07.02.1984 2 John 18.2.1992 Invoice Id customer number 1 1 123 2 1 124
  • 52.
  • 53.
    Enterprise Java Beans For defining business layer with service methods
  • 54.
    Enterprise Java Beans For defining business layer with service methods For local and remote services
  • 55.
    Enterprise Java Beans For defining business layer with service methods For local and remote services For transaction boundaries
  • 56.
  • 57.
    (@Remote) CustomerService (UI) CustomerView ! @Remote @Local
  • 58.
    (@Remote) CustomerService (UI) CustomerView ! (@Stateless) (@Stateless) CustomerService CustomerService Bean @Remote @Local @Stateless @Stateful @Singleton (@Stateless) CustomerService Bean Bean
  • 59.
  • 60.
    ! @Local! publicinterface CustomerService {! ! ! void storeCustomers(Collection<Customer> customers);! ! ! void removeCustomers(Collection<Customer> customers);! ! ! Collection<Customer> getAllCustomers()! ! ! Optional<Customer> getCustomerByName(String name);! }
  • 61.
    ! @Stateless! publicclass UserServiceBean implements ! ! UserService {! ! ! @PersistenceContext! ! private EntityManager em;! ! ! ! public void storeUsers(Collection<User> users) {! ! ! users.forEach(user -> storeUser(user));! ! }! ! ! public void storeUser(User user) {! ! ! em.merge(user);! ! }! }
  • 62.
    Context and Dependency Injection 1.1 (CDI)
  • 63.
    Context and Dependency Injection Instead of saying new say @Inject
  • 64.
    Context and Dependency Injection Instead of saying new say @Inject Decouples code and lets container manage dependencies
  • 65.
    Context and Dependency Injection Injections occur within a scope
  • 66.
    Context and Dependency Injection Injections occur within a scope @ApplicationScoped @SessionScoped @RequestScoped
  • 67.
    Context and Dependency Injection Injections occur within a scope @ApplicationScoped @SessionScoped @RequestScoped @UIScoped @ViewScoped
  • 68.
    @Stateless CustomerService _________________ ! @EJB InvoiceService invoices; @UIScoped AppUI _________________ ! @EJB CustomerService service; ! @Inject MainMenu mainMenu; ! @Inject User currentUser; @UIScoped MainMenu _________________ ! @Stateless InvoiceService @Inject Event<MenuEvent> menuEventSource;
  • 69.
    Integration to EEthrough Vaadin CDI
  • 70.
    UI and Viewas managed bean with @CDIUI and @CDIView
  • 71.
    UI and Viewas managed bean with @CDIUI and @CDIView Allows annotations like @Inject and @EJB to be processed inside Vaadin beans.
  • 72.
    UI and Viewas managed bean with @CDIUI and @CDIView Allows annotations like @Inject and @EJB to be processed inside Vaadin beans. Easily reference EE objects
  • 73.
    @CDIUI(“”)! public classAppUI extends UI {! ! }
  • 74.
    @CDIUI(“”)! public classAppUI extends UI {! ! ! ! @Inject! ! private MainMenu mainMenu;! ! ! @Inject! private User currentUser;! ! ! @Inject! ! private ViewManager viewManager;! ! ! ! public void init(VaadinRequest request) {! ! ! VerticalLayout layout = new VerticalLayout();! ! ! layout.addComponent(mainMenu);! ! ! ! setContent(layout);! ! }! }
  • 75.
    @UIScoped AppUI _____________________________ ! @Inject private MainMenu menu; ! @Inject private ViewManager viewMgr; ! @Inject private User loggedInUser; <<UIScope>> ! MainMenu ViewManager ! <<SessionScope>> ! User
  • 76.
    <<UIScope>> <<UIScope>> <<UIScope>> ! MenuBar Footer ViewManager ! <<SessionScope>> ! User @UIScoped AppUI _____________________________ ! @Inject private MenuBar menu; ! ! @Inject private ViewManager viewMgr; ! @Inject private User loggedInUser; <<UIScope>> ! MenuBar Footer ViewManager ! ! MenuBar Footer ViewManager ! ! MenuBar ViewManager !
  • 77.
  • 78.
  • 79.
    VaadinServlet 1 n 1 HttpSession VaadinSession 1 1 n
  • 80.
    VaadinServlet 1 HttpSessionVaadinSession UI 1 n 1 1 n 1 *
  • 81.
  • 82.
    @UIScoped Necessary toacquire UI specific instances
  • 83.
    @UIScoped Necessary toacquire UI specific instances CDI context to map beans per UI instance
  • 84.
    @UIScoped Necessary toacquire UI specific instances CDI context to map beans per UI instance @UIScoped
  • 85.
    @UIScoped! public classMainMenu extends CustomComponent {! ! }
  • 86.
    @UIScoped! public classMainMenu extends CustomComponent {! ! ! @Inject! ! private Event<NavigationEvent> eventSource;! ! ! ! protected void onMenuItemClicked(MenuItem item) {! ! ! eventSource.fireEvent(new NavigationEvent(item));! ! }! }
  • 87.
    @CDIUI(“”)! public classAppUI extends UI {! ! ! ! …! ! ! protected void onNavigationEvent(@Observes! ! ! ! ! ! ! ! ! ! ! ! ! ! ! NavigationEvent event) {! ! viewMgr.navigateTo(event.getView());! ! }! }
  • 88.
    Structuring Vaadin Appwith Model View Presenter
  • 89.
    Do you like spaghetti?
  • 90.
    Do you like spaghetti? Let’s clean it!
  • 91.
  • 92.
  • 93.
    Late 1970s OriginallyModel-View-Controller
  • 94.
    Late 1970s OriginallyModel-View-Controller SmallTalk-80
  • 95.
    Late 1970s OriginallyModel-View-Controller SmallTalk-80 Controller is mediator between end user and application
  • 96.
    Late 1970s OriginallyModel-View-Controller SmallTalk-80 Controller is mediator between end user and application Model notifies about changes with Observer pattern
  • 97.
  • 98.
  • 99.
    1980s Taligent Model-View-Presenter Influenced by SmallTalk-80
  • 100.
    1980s Taligent Model-View-Presenter Influenced by SmallTalk-80 Model, View, Presenter, Interactors, Commands, Selections
  • 101.
    1980s Taligent Model-View-Presenter Influenced by SmallTalk-80 Model, View, Presenter, Interactors, Commands, Selections Presenter orchestrates the structure, not the input
  • 102.
  • 103.
  • 104.
    late 1980s SimplifiedTaligent MVP Views handle input events initially
  • 105.
    late 1980s SimplifiedTaligent MVP Views handle input events initially Model may fire events
  • 106.
    late 1980s SimplifiedTaligent MVP Views handle input events initially Model may fire events Presenter handles the logic, not the user input control
  • 107.
    late 1980s SimplifiedTaligent MVP Views handle input events initially Model may fire events Presenter handles the logic, not the user input control
  • 108.
    UI layer MVPtargets Clean code best practices like…
  • 109.
    Single responsibility principle Class should change only for one reason
  • 110.
    Open closed principle Open for extension, closed for change
  • 111.
    Today many MVPvariations exists Here’s one…
  • 112.
  • 113.
  • 114.
    Model-View-Presenter View Impl Presenter View Model
  • 115.
    Example EditorView -Button saveButton;! - Button cancelButton;! - FieldGroup personFieldGroup; - saveButtonClicked()! - cancelButtonClicked() ClickListener + buttonClicked()
  • 116.
    Presenter + saveButtonClicked()! + cancelButtonClicked()! Example EditorView - Button saveButton;! - Button cancelButton;! - FieldGroup personFieldGroup; + commitChanges()! + discardChanges() ClickListener + buttonClicked()
  • 117.
    Presenter + saveButtonClicked()! + cancelButtonClicked()! + commitChanges()! + discardChanges() Example EditorViewImpl - Button saveButton;! - Button cancelButton;! - FieldGroup personFieldGroup; + commitChanges()! + discardChanges() ClickListener + buttonClicked() EditorView
  • 118.
  • 119.
    Benefits of MVP Separation of concerns simplifying code
  • 120.
    Benefits of MVP Separation of concerns simplifying code UI logic becomes separated from rendering providing testability
  • 121.
    Benefits of MVP Separation of concerns simplifying code UI logic becomes separated from rendering providing testability Keeps spaghetti in a cup by following clean code principles
  • 122.
  • 123.
    Client Browser View Business Persistency <<EJB>> Business Logic Server-side-UI Presenter <<JPA>> Persistency
  • 124.
  • 125.
    public interface CustomerViewextends ApplicationView<CustomerViewPresenter> { ! }
  • 126.
    public interface CustomerViewextends ApplicationView<CustomerViewPresenter> { ! void populateCustomers(Collection<Customer> customers); ! void openEditorFor(Customer customer); void closeEditor(); void removeTableSelection(); }
  • 127.
  • 128.
    @ViewScoped public classCustomerViewPresenter extends AbstractPresenter<CustomerView> { ! }
  • 129.
    @ViewScoped public classCustomerViewPresenter extends AbstractPresenter<CustomerView> { ! @EJB private CustomerService customerService; ! @Override protected void onViewEnter() { getView().populateCustomers(customerService.getAllCustomers()); } }
  • 130.
    @ViewScoped public classCustomerViewPresenter extends AbstractPresenter<CustomerView> { ! @EJB private CustomerService customerService; ! @Override protected void onViewEnter() { getView().populateCustomers(customerService.getAllCustomers()); } ! public void onCustomerSaved(@Observes CustomerSavedEvent event) { … } ! public void onCustomerRemoved(@Observes CustomerRemovedEvent event) { … } ! public void onCustomerSelected(@Observes CustomerSelectedEvent event) { … } }
  • 131.
    Client Browser View <<EJB>> Business Logic Server-side-UI Presenter Business
  • 132.
    @Local public interfaceCustomerService { ! void storeCustomers(Collection<Customer> customers); ! void removeCustomers(Collection<Customer> customers); ! Collection<Customer> getAllCustomers(); ! Optional<Customer> getCustomerByUsername(String username); }
  • 133.
    @Stateless @TransactionAttribute(Application TransactionAttributeType.REQUIRED) public Architecture class CustomerServiceBean implements CustomerService { ! @PersistenceContext(unitName = "appUnit") private EntityManager entityManager; ! @Override public void storeCustomers(Collection<Customer> customers) { customers.forEach(cu -> entityManager.merge(cu)); } ! @Override public Collection<Customer> getAllCustomers() { return entityManager.createQuery(query).getResultList(); } ! … }
  • 134.
    Client Browser View Business Persistency <<EJB>> Business Logic Server-side-UI Presenter <<JPA>> Persistency
  • 135.
    @Entity public Application Architecture class Customer { ! @Id @AutoGenerated private Long id; ! private String name; ! @Temporal(DATE) private Date birthDate; ! public boolean isPersisted() { return id != null; } ! … }
  • 136.
    <persistence-unit name="appUnit" transaction-type="JTA"> <jta-data-source>jdbc/app-backend</jta-data-source> <class>org.vaadin.example.backend.entity.Customer</class> ! <properties> <property name="…" … /> </properties> </persistence-unit>
  • 137.
  • 138.
  • 139.
  • 140.
    com.vaadin.cdi.access.AccessControl @RolesAllowed isUserSignedIn(), isUserInRole(String), getPrincipalName() isUserHavingAccessToView(ViewBean)
  • 141.
    com.vaadin.cdi.access.AccessControl @RolesAllowed isUserSignedIn(), isUserInRole(String), getPrincipalName() isUserHavingAccessToView(ViewBean) Can be replaced with @Alternative
  • 142.
    How to GetStarted?
  • 143.
    <dependency <groupId>com.vaadin</groupId> <artifactId>vaadin-cdi</artifactId> <version>1.0.0.beta4</version> </dependency> ! <repository> <id>vaadin-addons</id> <url>http://maven.vaadin.com/vaadin-addons</url> </repository>
  • 144.
    Eclipse Download plugin from Martketplace
  • 145.
  • 146.
    Netbeans Download plugin Netbeans Plugin Portal
  • 147.
  • 148.
  • 149.
    Lessons learned today 1. Java EE contains tons of specifications for enterprise apps
  • 150.
    Lessons learned today 1. Java EE contains tons of specifications for enterprise apps 2. Vaadin is a great way to assemble web apps with components
  • 151.
    Lessons learned today 1. Java EE contains tons of specifications for enterprise apps 2. Vaadin is a great way to assemble web apps with components 3. Combining Vaadin with Java EE works best through CDI
  • 152.
    Lessons learned today 1. Java EE contains tons of specifications for enterprise apps 2. Vaadin is a great way to assemble web apps with components 3. Combining Vaadin with Java EE works best through CDI 4. Following best practices makes your code maintainable
  • 153.
    Lessons learned today 1. Java EE contains tons of specifications for enterprise apps 2. Vaadin is a great way to assemble web apps with components 3. Combining Vaadin with Java EE works best through CDI 4. Following best practices makes your code maintainable 5. Vaadin is a Finnish word meaning Reindeer
  • 154.
    Download for Free vaadin.com/book PDF, ePub, HTML ISBN 978-952-93-1970-1 9789529319701 ISBN 978-952-93-1970-1 9789529319701 Vol 1 288 pages Vol 2 434 pages
  • 155.
    Questions or Comments? Peter Lehto Vaadin Expert @peter_lehto