javacro'14 - building interactive web applications with vaadin – peter lehto

73
Vaadin 7 Building web applications Peter Lehto Vaadin Expert

Category:

Technology


3 download

DESCRIPTION

Vaadin is quickly popularizing Java framework for developing rich and interactive server-driven web applications. Vaadin is built around core Servlet and Google Web Toolkit (GWT) technologies and it strives to developer productivity by providing large library of components and ready made functionality that hides the hard parts of web development allowing developers to concentrate to the real business problem at hand. During this session we’ll cover the basics of building Vaadin based web applications: layouting, data binding, application deployment and Vaadin Touckit integration for mobile devices. Attending the speech does not require thorough understanding of web technologies in general, session will include demonstration and live coding.

TRANSCRIPT

Page 1: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Vaadin 7Building web applications

Peter Lehto Vaadin Expert

Page 2: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

What is

Vaadin?

App

lifecycle

Why

Vaadin?

Page 3: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Structuring

the App

Building

Responsive

Layouts

Page 4: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

How to get

started?

QAField Data binding

Server @Push

Page 5: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

?

Page 6: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
Page 7: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
Page 8: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

User interface framework for Rich Internet Applications

with pure Java

Page 9: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Servlet backend GWT frontend

Page 10: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Vaadin +=

GWT

Page 11: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Why?

Page 12: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Developer

Productivity

Rich

UX

Why?

Page 13: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

JavaScript DOM AJAX JSON …

htmljava

Why?

Page 14: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

App Lifecycle

Page 15: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

http://localhost:8080/Vaadin/

App Lifecycle

Page 16: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

New session

Loader page

Page 17: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Vaadin

Loader page

Loads theme

Loads widgetset

Fetch root configuration

Page 18: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

New UI instance is created

UI.init() is called

Page 19: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

public class MyVaadinUI extends UI {! @WebServlet(value = "/*", asyncSupported = true) @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class) public static class Servlet extends VaadinServlet { }! @Override protected void init(VaadinRequest request) { }}

Page 20: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

public class MyVaadinUI extends UI {! @WebServlet(value = "/*", asyncSupported = true) @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class) public static class Servlet extends VaadinServlet { }! @Override protected void init(VaadinRequest request) { VerticalLayout layout = new VerticalLayout();! final TextField textField = new TextField("Name"); final Button button = new Button("Greet");! layout.addComponents(textField, button);! setContent(layout); }}

Page 21: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

public class MyVaadinUI extends UI {! @WebServlet(value = "/*", asyncSupported = true) @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class) public static class Servlet extends VaadinServlet { }! @Override protected void init(VaadinRequest request) { VerticalLayout layout = new VerticalLayout();! final TextField textField = new TextField("Name"); final Button button = new Button("Greet");! layout.addComponents(textField, button);! setContent(layout);! button.addClickListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { Notification.show("Hello " + textField.getValue()); } }); }}

Page 22: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Vaadin

UIDL

• UI is added to the DOM

Page 23: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Vaadin

Page 24: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Vaadin

User activity

Page 25: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

public class MyVaadinUI extends UI {! …! @Override protected void init(VaadinRequest request) { …! button.addClickListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { Notification.show("Hello " + textField.getValue()); } }); }}

Page 26: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Vaadin

UIDL

Page 27: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Vaadin

Heart beats

Page 28: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

No user activity?

3 heartbeats are missed?

UI is detached

Page 29: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

closeIdleSession=true

Session timeout expires after last non-heartbeat request

Session and all of its UIs are closed

Page 30: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

VaadinServlet

HttpSession VaadinSession

n active users

UI

1

n

1 1

1

n

Resources

1

**

1

1 servlet

Page 31: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

UI instance can be preserved between refreshes with @PreserveOnRefresh annotation

Page 32: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Structuring the App

Page 33: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

View: any logical collection of components

Structuring the App

Page 34: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Navigation between views is done by exchanging the content of the UI or by replacing components in a layout

Structuring the App

Page 35: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

public class CustomersView extends CustomComponent implements com.vaadin.navigator.View {! @Override public void enter(ViewChangeEvent event) { }!}

Structuring the App

public class InvoicesView extends CustomComponent implements com.vaadin.navigator.View {! @Override public void enter(ViewChangeEvent event) { }!}

Page 36: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Structuring the App

@Overrideprotected void init(VaadinRequest request) { VerticalLayout layout = new VerticalLayout(); setContent(layout); Navigator navigator = new Navigator(this, layout); navigator.addView("customers", CustomerView.class); navigator.addView("invoices", InvoicesView.class);! …}

Page 37: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Activating a view programmatically

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

Structuring the App

Page 38: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

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

#! : Navigator identifier customers : View name

Navigating to a view via URL

Page 39: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Building Responsive

Layouts

Page 40: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Building Responsive

Layouts

public class DashBoard extends CssLayout implementscom.vaadin.navigator.View {!public DashBoard() { setSizeFull(); addStyleName("dashboard"); Responsive.makeResponsive(this);}

Page 41: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Building Responsive

Layouts

.stylename[width-range~="300-500px"]

.stylename[height-range~="300-500px"]

Page 42: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Server @Push

Page 43: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Request-response cycle

ServerClient

State

Click

Server @Push

Page 44: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Request-response cycle with polling

ServerClient

Poll

Poll

State

Click

Progress Indicator

Server @Push

Page 45: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Request-response cycle with push

ServerClient

State

Click

Progress Indicator

Server @Push

Page 46: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

To use push

Always use locking with

UI.access(Runnable) or

VaadinSession.lock() when modifying the UI from an external thread

Annotate UI with @Push

Include vaadin-push JAR in your project

Server @Push

Page 47: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Technical details

Manually managed pushing also available

Fallback to the best supported push method: Long polling or Streaming

Implemented with the Atmosphere library using WebSockets

Server @Push

Page 48: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Field Data binding

Page 49: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Examples of FieldsField

Data binding

Page 50: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Fields are bound to Properties which are simple objects with type and value

Page 51: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

public class Customer { private String name; private Date birthDate; private Address address;! public String getName() { return name; }! public void setName(String name) { this.name = name; } public Date getBirthDate() { return birthDate; }! public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }! public Address getAddress() { return address; }! public void setAddress(Address address) { this.address = address; }}

Page 52: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Customer customer = getCustomer(); TextField nameField = new TextField(); MethodProperty<String> nameProperty = new MethodProperty<String>(customer, "name");!nameField.setPropertyDataSource(nameProperty);

Field Data binding

Page 53: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

BeanItem<Customer>

Field Data binding

Page 54: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Customer customer = getCustomer();!TextField nameField = new TextField(); BeanItem<Customer> customerBean = new BeanItem<Customer>(customer);nameField.setPropertyDataSource(customerBean.getItemProperty("name"));

Field Data binding

Page 55: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

FieldGroup is used to bind multiple properties from Item

Field Data binding

Page 56: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Customer customer = getCustomer(); BeanItem<Customer> customerItem = new BeanItem<Customer>(customer); FieldGroup fieldGroup = new FieldGroup(customerItem);fieldGroup.bind(new TextField(), "name");fieldGroup.bind(new PopupDateField(), "birthDate");

Field Data binding

Page 57: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Fields are Buffered and can be Committed and Discarded

Field Data binding

Page 58: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Customer customer = getCustomer(); BeanItem<Customer> customerItem = new BeanItem<Customer>(customer); FieldGroup fieldGroup = new FieldGroup(customerItem);fieldGroup.bind(new TextField(), "name");fieldGroup.bind(new PopupDateField(), “birthDate");!fieldGroup.commit();

Field Data binding

Page 59: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

commit()

Buff

eri

ng

Data source

UI Logic

Field

Button

Browser

TextField value change

Save button pressed

ClickEvent

Page 60: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
Page 61: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
Page 62: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Field Data binding

Collection<Customer> customers = getCustomers();BeanItemContainer<Customer> customerContainer = new BeanItemContainer<Customer>(Customer.class, customers); Table customersTable = new Table();!customersTable.setContainerDataSource(customerContainer, Arrays.asList("name", "birthDate"));

Page 63: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

How do I get started ?

Page 64: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Eclipse

Download plugin from Marketplace

How do I get started ?

Page 65: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

IntelliJ IDEA

Built-in support

How do I get started ?

Page 66: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Netbeans

Download plugin Netbeans Plugin Portal

How do I get started ?

Page 67: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

mvn archetype:generate

-DarchetypeGroupId=

com.vaadin

-DarchetypeArtifactId=

vaadin-archetype-application

-DarchetypeVersion=

7.1.15

Maven

How do I get started ?

Page 68: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
Page 69: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Download for Freevaadin.com/book

> 600 pages

9 789529 319701

ISBN 978-952-93-1970-1

PDF, ePub, HTML

Page 70: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Ohloh #2 used

Java Web Framework

Community of 100.000+

Page 71: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

By Marko Grönroos

ABOUT VAADIN

ww

w.d

zone

.co

m

G

et M

ore

Ref

card

z! V

isit

ref

card

z.co

m

#85

Getting Started with VaadinCONTENTS INCLUDE:�� About Vaadin�� Creating An Application�� Components �� Layout Components�� Themes�� Data Binding and more...

Vaadin is a server-side Ajax web application development framework that allows you to build web applications just like with traditional desktop frameworks, such as AWT or Swing. An application is built from user interface components contained hierarchically in layout components.

In the server-driven model, the application code runs on a server, while the actual user interaction is handled by a client-side engine running in the browser. The client-server communications and any client-side technologies, such as HTML and JavaScript, are invisible to the developer. As the client-side engine runs as JavaScript in the browser, there is no need to install plug-ins. Vaadin is released under the Apache License 2.0.

Figure 1: Vaadin Client-Server Architecture

If the built-in selection of components is not enough, you can develop new components with the Google Web Toolkit (GWT)

Figure 2: Architecture for Vaadin Applications

Hot Tip

You can get a reference to the application object from any component attached to the application with ��$!!����$� ���

Event ListenersIn the event-driven model, user interaction with user interface components triggers server-side events, which you can handle

Web BrowserClient-Side Engine

JavaWeb Server

VaadinUIComponents

YourJavaApplication

WebService

EJB

DB

Servlet Container

UserApplication

EventListener

DataModel

ApplicationThemes

ApplicationResources

DefaultTheme

FileResources

ExternalResources

Database

DataBinding

Inherits Events Changes

AJAX Requests

Inherits

UIComponent

JavaServlet

ApplicationClass

Web BrowserClient-Side Engine

brought to you by...

Page 72: JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto

Questions or Comments?

Page 73: JavaCro'14 - Building interactive web applications with Vaadin – 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?