JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

58
Vaadin 7 Enterprise integration Peter Lehto Vaadin Expert

description

Vaadin is Java framework for rapid development of highly interactive HTML5-based web applications. Because of server-driven nature Vaadin can easily be integrated with server-side Java EE features such as EJBs and JPA. During this speech we will look in detail on how multi-view Vaadin applications are built and coupled with Java EE based business systems using Context and Dependency Injection (CDI). Important topics covered within the session are the best practices of developing Model-View-Presenter (MVP) based Vaadin views as well as the as pointers and guidelines on how to use Vaadin with Java EE. Attending the speech does not require thorough understanding of Java EE or web technologies in general.

Transcript of JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Page 1: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Vaadin 7Enterprise integration

Peter Lehto Vaadin Expert

DEL
Typewritten Text
DEL
Typewritten Text
Page 2: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

MVP

pattern

Structuring

Vaadin

Application

Views with

Vaadin

Navigator

Page 3: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

UIProvider

CDIViewProviderVaadin-CDI

@Inject, @EJB

Page 4: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

How to

get started?

QA

Context and

Scope

Authentication

and

Authorization

Page 5: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

MVP pattern

Page 6: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Do you like spaghetti?

MVP pattern

Page 7: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

What is MVP?

MVP pattern

Page 8: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Originally Model-View-Controller

Originates from late 70’s

SmallTalk-80

Controller is mediator between end user and application

Model notifies about changes with Observer pattern

MVP pattern

Page 9: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Taligent Model-View-Presenter

Influenced by SmallTalk-80

Model, View, Presenter, Interactors, Commands, Selections

Presenter orchestrates the structure, not the input

MVP pattern

Page 10: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Dolphin Smalltalk Model-View-Presenter

Simplified Taligent MVP

Views handle input events initially

Model may fire events

Presenter handles the logic, not the user input control

MVP pattern

Page 11: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Model-View-Presenter

Presenter

View Model

Direct Association

Indirect Association

MVP pattern

Page 12: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

“That’s not how you use MVP!”

MVP pattern

Page 13: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Model-View-Presenter

Presenter

Model

View Impl

View

MVP pattern

Page 14: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

What is the role of the Presenter?

MVP pattern

Page 15: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Structuring

Vaadin

Application

Page 16: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Example

EditorView

- Button saveButton;!- Button cancelButton;!- FieldGroup personFieldGroup;

- saveButtonClicked()!- cancelButtonClicked()

ClickListener

+ buttonClicked()

Structuring

Vaadin

Application

Page 17: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

EditorView

- Button saveButton;!- Button cancelButton;!- FieldGroup personFieldGroup;

ClickListener

Presenter

+ saveButtonClicked()!+ cancelButtonClicked()!

+ buttonClicked()

+ commitChanges()!+ discardChanges()

ExampleStructuring

Vaadin

Application

Page 18: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

EditorView+ commitChanges()!+ discardChanges()

Example

EditorView

- Button saveButton;!- Button cancelButton;!- FieldGroup personFieldGroup;

ClickListener

Presenter

+ saveButtonClicked()!+ cancelButtonClicked()!

+ buttonClicked()

+ commitChanges()!+ discardChanges()

Structuring

Vaadin

Application

Page 19: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Benefits of MVP

Allows you to create unit and integration tests for your UI logic

Abstracts your code to be more reusable

Your UI logic is separated from the UI implementation

Structuring

Vaadin

Application

Page 20: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Views with

Vaadin

Navigator

Page 21: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Navigator

Support for bookmarkable views and browser navigation

Automatic URI fragment handling

Built-in switching between views

Views with

Vaadin

Navigator

Page 22: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

public class DashboardView extends CustomComponent implements

com.vaadin.navigator.View

Views with

Vaadin

Navigator

Page 23: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

public class MyNavigatorUI extends UI{!! @Override!! Protected void init(VaadinRequest request){!! ! Navigator navigator = new Navigator(this, this); !! ! navigator.addView(“dashboard”, new DashboardView());!! } !}!

Views with

Vaadin

Navigator

Page 24: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Activating a view programmatically

UI.getCurrent().getNavigator().navigateTo("customers");

Views with

Vaadin

Navigator

Page 25: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

http://localhost:8080/vaadin/#!customers !

#! : Navigator identifier customers : View name

Navigating to a view via URLViews with

Vaadin

Navigator

Page 26: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

...but where is the view rendered?

Views with

Vaadin

Navigator

Page 27: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Views with

Vaadin

Navigator

Page 28: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

public Navigator(UI ui, ComponentContainer container)public Navigator(UI ui, SingleComponentContainer container)

public Navigator(UI ui, ViewDisplay viewDisplay)

Views with

Vaadin

Navigator

Page 29: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Vaadin-CDI

Page 30: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Context and Dependency Injection

(JSR-299)

Vaadin-CDI

Page 31: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Injection

!

Context and Scope

!

Events

!

Decoupling

CDIVaadin-CDI

Page 32: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Features

Vaadin UI based context scope

!

Vaadin Navigator Integration

!

Easy JAAS Integration

!

Role Based View Management

Vaadin-CDI

Page 33: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

@CDIUI public class MyVaadinUI extends UI

Vaadin-CDI

http://localhost:8080/vaadin/

Page 34: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

@CDIUI(“myui”) public class MyVaadinUI extends UI

Vaadin-CDI

http://localhost:8080/vaadin/myui

Page 35: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

@CDIUI public class MyVaadinUI extends UI { @Inject private MyBean bean; … }

Vaadin-CDI

Page 36: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

@CDIUI public class MyVaadinUI extends UI { !

@EJB private MyServiceEJB service; … }

Vaadin-CDI

Page 37: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

@CDIUI public class MyVaadinUI extends UI { !

@Inject private javax.enterprise.event.Event<MyEvent> myEvent; … myEvent.fire(new MyEvent()); }

Vaadin-CDI

Page 38: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

@CDIUI public class MyVaadinUI extends UI { … !

protected void onMyEvent(@Observes MyEvent myEvent) { } }

Vaadin-CDI

Page 39: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

@CDIView(“customers”) public class CustomerView extends CustomComponent implements View

Vaadin-CDI

http://localhost:8080/vaadin/#!customers

Page 40: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

UIProvider

CDIViewProvider

Page 41: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

@CDIUI public class MyVaadinUI extends UI { !

@Inject private CDIViewProvider viewProvider; … navigator.addProvider(viewProvider); }

UIProvider

CDIViewProvider

Page 42: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

UIProvider

CDIViewProvider

public class CDIViewProvider implements ViewProvider {! @Inject private BeanManager beanManager;! @Inject private AccessControl accessControl;! …! public String getViewName(String) { … } public View getView(String) { … }}

Page 43: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

UIProvider

CDIViewProvider

public class CDIUIProvider extends DefaultUIProvider {! public UI createInstance(UICreateEvent event) { … } public Class<? extends UI> getUIClass (UIClassSelectionEvent event) { … }}

Page 44: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

UIProvider

CDIViewProvider ContextDeployer

@WebListener

!

Is capable of bootstrapping Vaadin Servlet with CDIUIProvider

!

Will validate deployment

Page 45: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Context and

Scope

Page 46: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Context and

Scope VaadinServlet

HttpSession VaadinSession

UI

1

n

1 1

1

n

1

*

EJB

1*

View/Presenter

1

*

Page 47: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Context and

Scope UIScope

Necessary to acquire UI specific component injections

!

CDI context to map beans per UI instance

!

@UIScoped

Page 48: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Context and

Scope UIScope

@CDIUI and @CDIView are @Stereotypes with @UIScoped

!

CDI events are sent within the scope

!

MVP Presenters and UI specific resources are marked as @UIScoped

Page 49: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Authentication

and

Authorization

Page 50: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Authentication

and

Authorization

com.vaadin.cdi.access. AccessControl

@RolesAllowed

!

isUserSignedIn(), isUserInRole(String),

getPrincipalName()

!

Can be replaced with @Alternative

Page 51: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

How to

get started?

Page 52: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

<dependency <groupId>com.vaadin</groupId> <artifactId>vaadin-cdi</artifactId> <version>1.0.0.alpha2</version></dependency>!<dependency org="com.vaadin" name="vaadin-cdi” rev=“1.0.0.alpha2” conf="default->default" />

How to

get started?

Page 53: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Eclipse

Download plugin from Martketplace

How to

get started?

Page 54: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

IntelliJ IDEA

Built-in support

How to

get started?

Page 55: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Netbeans

Download plugin Netbeans Plugin Portal

How to

get started?

Page 56: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

mvn archetype:generate

-DarchetypeGroupId=

com.vaadin

-DarchetypeArtifactId=

vaadin-archetype-application

-DarchetypeVersion=

7.1.15

Maven

How to

get started?

Page 57: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

Questions or Comments?

Page 58: JavaCro'14 - Vaadin web application integration for Enterprise systems – Peter Lehto

3

Vaadin: A Familiar Way to Build Web Apps with Java

DZone, Inc. | www.dzone.comFigure 4: The Class Diagram presents all user interface component classes and the most important interfaces, relationships, and methods.

[email protected]

Questions or Comments?