Java EE 6 New features in practice Part 2 Java and all Java-based - - PowerPoint PPT Presentation

java ee 6 new features in practice part 2
SMART_READER_LITE
LIVE PREVIEW

Java EE 6 New features in practice Part 2 Java and all Java-based - - PowerPoint PPT Presentation

Java EE 6 New features in practice Part 2 Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. License for use and distribution This material is available for


slide-1
SLIDE 1

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.

Java EE 6 New features in practice Part 2

slide-2
SLIDE 2

September 2010 Java EE 6, new features in practice - Part 02

2

License for use and distribution This material is available for non-commercial use and can be derived and/or redistributed, as long as it uses an equivalent license.

Attribution-Noncommercial- Share Alike 3.0 Unported

http://creativecommons.org/licenses/by-nc-sa/3.0/

You are free to share and to adapt this work under the following conditions: (a) You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work); (b) You may not use this work for commercial purposes. (c) If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

slide-3
SLIDE 3

September 2010 Java EE 6, new features in practice - Part 02

3

About the author – Vítor Souza

Education:

Computer Science graduate, masters in Software Engineering – (UFES, Brazil), taking PhD at U. Trento.

Java:

Developer since 1999; Focus on Web Development; Co-founder and coordinator of ESJUG (Brazil).

Professional:

Substitute teacher at Federal University of ES; Engenho de Software Consulting & Development.

Contact: vitorsouza@gmail.com

slide-4
SLIDE 4

September 2010 Java EE 6, new features in practice - Part 02

4

JUG TAAS = JUG Trento + JUG Bolzano Website: http://www.jugtrento.org/ http://www.jugbz.org/ Mailing list (in Italian, mostly): http://groups.google.com/group/jugtaa If you're interested in Java, join and participate!

slide-5
SLIDE 5

September 2010 Java EE 6, new features in practice - Part 02

5

Agenda Quick summary of part 1; Facelets for page decoration; Criteria API; Conversations; AJAX support.

slide-6
SLIDE 6

September 2010 Java EE 6, new features in practice - Part 02

6

Quick summary of part 1 (1) Java EE 6 (JSR 316), released in December 2009;

Platform for development of enterprise applications (scalability, security, accessibility, etc.); New version focuses on flexibility, extensibility and ease of development;

The platform includes many other technologies: Profiles: standard, Web, more in the future?

Bean Validation, CDI, EJB, EL, JACC, JASPIC, Deployment API, Management API, JavaMail, JAX-RS, JAX-WS, JAXB, JCA, JMS, JPA, JSF, JSP, JSTL, JTA, Managed Beans, Servlet, Web Services Metadata.

slide-7
SLIDE 7

September 2010 Java EE 6, new features in practice - Part 02

7

Quick summary of part 1 (2) Tools: GlassFish V3, NetBeans 6.9; Example application: ADS; Domain objects: POJO + JPA Annotations; Bean Validation: annotations on domain classes provide validation across the platform.

public class Ambulance extends PersistentObjectImpl { @NotNull private int number; @NotNull @Size(min = 8, max = 8) private String licensePlate; /* ... */ }

Customized validation also possible!

slide-8
SLIDE 8

May 2010 Java EE 6, new features in practice - Part 01

8

Quick summary of part 1 (3) – CDI

public class Ambulance @Stateless public class AmbulanceDAOJPA { @PersistenceContext private EntityManager em; } @Stateful @SessionScoped public class AmbulanceCrudApp { @EJB private AmbulanceDAO ambulanceDAO; } @Model public class AmbulanceCrudAction { @EJB private AmbulanceCrudApp ambulanceCrudApp; } <html ...> #{ambulanceCrudAction}

1 1: every request 2 2: one per user (session) 3 3: SLSB pool (container- managed)

slide-9
SLIDE 9

May 2010 Java EE 6, new features in practice - Part 01

9

Facelets

slide-10
SLIDE 10

September 2010 Java EE 6, new features in practice - Part 02

10

Facelets Alternative to JSP for JSF pages (since 2005); JSF and JSP are incompatible (see [1]); Was framework, became standard in Java EE 6; Web pages written in XHTML (verifiable); Allows the construction of decorators for pages; Allows the creation of custom components.

[1] = onjava.com/pub/a/onjava/2004/06/09/jsf.html

slide-11
SLIDE 11

September 2010 Java EE 6, new features in practice - Part 02

11

Facelets decorators

<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE ...> <html ...> <h:head> <link href="#{facesContext.externalContext. requestContextPath}/arquivos/estilos/style.css" rel="stylesheet" type="text/css" media="screen" /> <title><h:outputText value="ADS :: " /> <ui:insert name="title" /></title> </h:head> <h:body> <!-- Header... --> <ui:insert name="content">Default text</ui:insert> <!-- Footer... --> </h:body> </html>

slide-12
SLIDE 12

September 2010 Java EE 6, new features in practice - Part 02

12

Using Facelets decorators

<!DOCTYPE ...> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" template="/resources/templates/decorador.xhtml"> <ui:define name="title">Welcome</ui:define> <ui:define name="content"> <h1>Welcome to the ADS</h1> <p>Some information...</p> </ui:define> </ui:composition>

slide-13
SLIDE 13

September 2010 Java EE 6, new features in practice - Part 02

13

Custom components with Facelets

<ui:composition ...> <table align="center" border="0" cellpadding="3"> <ui:insert /> </table> </ui:composition> <ui:composition ...> <tr> <td align="right" valign="top"> <ui:insert name="fieldName" />: </td> <td><ui:insert /></td> </tr> </ui:composition> <ui:composition ...> <tr> <td colspan="2" align="right"> <ui:insert /> </td> </tr> </ui:composition>

field.xhtml button.xhtml form.xhtml

slide-14
SLIDE 14

September 2010 Java EE 6, new features in practice - Part 02

14

Using custom components

<ui:decorate template="/resources/templates/form.xhtml"> <h:form> <ui:decorate template="/resources/templates/field.xhtml"> <ui:define name="fieldName">Username</ui:define> <h:inputText size="15" /> </ui:decorate> <ui:decorate template="/resources/templates/field.xhtml"> <ui:define name="fieldName">Password</ui:define> <h:inputSecret size="15" /> </ui:decorate> <ui:decorate template="/resources/templates/button.xhtml"> <h:commandButton value="Log in" /> </ui:decorate> </h:form> </ui:decorate>

slide-15
SLIDE 15

September 2010 Java EE 6, new features in practice - Part 02

15

Example of a complex component

<ui:composition ...> <table border="0" class="formField #{(fieldName == null

  • r empty facesContext.getMessageList(fieldName)) ? '' :

'formFieldError'}"> <tr> <td class="label #{(fieldName == null or empty facesContext.getMessageList(fieldName)) ? '' : 'labelError'}" valign="top"> <ui:insert name="label" /><h:panelGroup styleClass="star" rendered="#{(fieldName != null and facesContext.viewRoot.findComponent(fieldName).required)}">* </h:panelGroup>: </td> <td class="spacing"></td> <td class="field #{(fieldName == null or empty facesContext.getMessageList(fieldName)) ? '' : 'fieldError'}"> <h:messages for="#{fieldName}" layout="table" rendered="#{fieldName != null}" /> <ui:insert /> </td></tr></table></ui:composition>

slide-16
SLIDE 16

September 2010 Java EE 6, new features in practice - Part 02

16

JPA 2.0 Criteria API

slide-17
SLIDE 17

September 2010 Java EE 6, new features in practice - Part 02

17

Criteria API New in JPA 2.0; Before there was JPQL only; Similar to Hibernate Criteria API (like JPQL is similar to HQL); Allows programmatic construction of queries;

Uses objects instead of Strings; Thus, can be verified at compile time.

Two modes: static and dynamic.

slide-18
SLIDE 18

September 2010 Java EE 6, new features in practice - Part 02

18

Criteria API – Dynamic mode

public Employee retrieveByUsername(String username) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> cq = cb.createQuery(Employee.class); Root<Employee> root = cq.from(Employee.class); EntityType<Funcionario> model = root.getModel(); cq.where(cb.equal(root.get(model.getSingularAttribute( "login", String.class)), username)); Funcionario funcionario = null; try { funcionario = em.createQuery(cq).getSingleResult(); } catch (RuntimeException e) { /* Do something... */ return null; } return funcionario; }

Dynamic

slide-19
SLIDE 19

September 2010 Java EE 6, new features in practice - Part 02

19

Criteria API – Static mode

public Employee retrieveByUsername(String username) { /* Same stuff before... */ cq.where(cb.equal(root.get(EmployeeJPAMetamodel.login), username)); /* Same stuff after... */

Static

package it.unitn.disi.ads.core.persistence; import it.unitn.disi.ads.core.domain.Employee; import it.unitn.disi.ads.core.domain.EmployeeType; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.StaticMetamodel; @StaticMetamodel(Employee.class) public class EmployeeJPAMetamodel { public static volatile SingularAttribute<Employee, String> name; public static volatile SingularAttribute<Employee, String> login; public static volatile SingularAttribute<Employee, String> password; public static volatile SingularAttribute<Employee, EmployeeType> type; }

slide-20
SLIDE 20

September 2010 Java EE 6, new features in practice - Part 02

20

Dynamic x Static

EntityType<Funcionario> model = root.getModel(); cq.where(cb.equal(root.get(model.getSingularAttribute( "login", String.class)), username));

Dynamic

cq.where(cb.equal(root.get(EmployeeJPAMetamodel.login), username));

Static

Dynamic model uses String (prone to error); Static model requires an extra class (meta-model);

Code generators could easily help here...

slide-21
SLIDE 21

September 2010 Java EE 6, new features in practice - Part 02

21

Conversations in CDI

slide-22
SLIDE 22

September 2010 Java EE 6, new features in practice - Part 02

22

CDI Scopes Determine when beans exist and are bound; Are extensible: create your own scope; Five scopes already provided:

Defined by the Servlet API: Request, Session, Application; Dependent scope: the bean's scope is the same as the bean that injected it; Conversation scope: a collection of requests within a session, established programmatically.

slide-23
SLIDE 23

September 2010 Java EE 6, new features in practice - Part 02

23

Conversations Are transient by default; Transient conversations begin/end with the request; Can be programmatically changed to long- running:

public class ReceiveCallAction { @Inject private Conversation conversation; public void someMethod() { if (conversation.isTransient()) conversation.begin(); /* ... */ } }

slide-24
SLIDE 24

September 2010 Java EE 6, new features in practice - Part 02

24

Conversations Long-running conversations last until programmatically ended: Objects with conversation scope are bound to that context until the conversation ends.

public class ReceiveCallAction { @Inject private Conversation conversation; public void someOterMethod() { /* ... */ If (! conversation.isTransient()) conversation.end(); } }

slide-25
SLIDE 25

September 2010 Java EE 6, new features in practice - Part 02

25

Conversations Propagation:

If navigation is done through JSF (e.g. <h:commandLink />), propagation is automatic; Otherwise, you can use ?cid=X in the URL;

Conversation management:

Conversation ids can be changed (give it a name); Conversations can be stored in a collection of a session-scoped bean; User can change the active conversation using the cid parameter in the URL.

slide-26
SLIDE 26

September 2010 Java EE 6, new features in practice - Part 02

26

AJAX Support

slide-27
SLIDE 27

September 2010 Java EE 6, new features in practice - Part 02

27

Support for AJAX in JSF 2.0 New tag <f:ajax />; Attributes:

event: which event should trigger the request (action, blur, change, click, ...); listener: method to execute when the event

  • ccurs;

execute: data to submit in the request (@all, @none, @this, @form, component IDs); render: what should be redrawn (@all, @none, @this, @form, component IDs).

slide-28
SLIDE 28

September 2010 Java EE 6, new features in practice - Part 02

28

AJAX Examples

<h:form id="form"> <h:commandButton action="#{myBean.doSomething()}" value="Do Something"> <f:ajax render=":form:anotherComponent" /> </h:commandButton> <h:panelGroup id="anotherComponent"> <!-- ... --> </h:panelGroup> </h:form>

Full name reference

slide-29
SLIDE 29

September 2010 Java EE 6, new features in practice - Part 02

29

AJAX Examples

<h:form id="form"> <h:inputText id="value" value="#{myBean.value}" /> <h:commandButton action="#{myBean.doSomething()}" value="Do Something"> <f:ajax render=":form:anotherComponent" execute="value" /> </h:commandButton> <h:panelGroup id="anotherComponent"> <h:outputText value="#{myBean.value}" /> </h:panelGroup> </h:form>

Local name reference

slide-30
SLIDE 30

September 2010 Java EE 6, new features in practice - Part 02

30

AJAX Examples

<h:form id="form"> <h:inputText id="name" value="#{myBean.obj.name}"> <f:ajax event="blur" render="acronym" listener="#{myBean.suggestAcronym}" /> </h:inputText> <h:inputText id="acronym" value="#{myBean.obj.acronym}" /> <!-- ... --> </h:form> @Named public class MyBean { private DomainObject obj = new DomainObject(); public DomainObject getObj() { return obj; } public void suggestAcronym(AjaxBehaviorEvent event) { String acronym = /* Calculate an acronym. */

  • bj.setAcronym(acronym);

} }

slide-31
SLIDE 31

September 2010 Java EE 6, new features in practice - Part 02

31

AJAX and Bean Validation 1 - Add validation annotation to your entities; 2 - Use the entity to exchange data with JSF (model-driven); 3 - Use a Facelets custom component to put fields in the form; 4 - Add <f:ajax /> to form fields, blur event, re- render the whole Facelets component; 5 - In your Facelets component code, check for JSF messages on that field and change color if there are errors.

slide-32
SLIDE 32

May 2010 Java EE 6, new features in practice - Part 01

32

Remembering Bean Validation

public class Ambulance extends PersistentObjectImpl { @NotNull private int number; @NotNull @Size(min = 8, max = 8) private String licensePlate; /* ... */ }

slide-33
SLIDE 33

September 2010 Java EE 6, new features in practice - Part 02

33

Remembering Facelets components

<ui:composition ...> <table border="0" class="formField #{(fieldName == null

  • r empty facesContext.getMessageList(fieldName)) ? '' :

'formFieldError'}"> <tr> <td class="label #{(fieldName == null or empty facesContext.getMessageList(fieldName)) ? '' : 'labelError'}" valign="top"> <ui:insert name="label" /><h:panelGroup styleClass="star" rendered="#{(fieldName != null and facesContext.viewRoot.findComponent(fieldName).required)}">* </h:panelGroup>: </td> <td class="spacing"></td> <td class="field #{(fieldName == null or empty facesContext.getMessageList(fieldName)) ? '' : 'fieldError'}"> <h:messages for="#{fieldName}" layout="table" rendered="#{fieldName != null}" /> <ui:insert /> </td></tr></table></ui:composition>

slide-34
SLIDE 34

September 2010 Java EE 6, new features in practice - Part 02

34

Finally, the form

<ui:decorate template="/templates/form.xhtml"> <h:form id="form"> <h:panelGroup id="numberField"> <ui:decorate template="/templates/field.xhtml"> <ui:param name="fieldName" value="form:number" /> <ui:define name="label">Number</ui:define> <h:inputText id="number" value="#{ambulanceCrudBean.ambulance.number}"> <f:ajax event="blur" render="numberField" /> </h:inputText> </ui:decorate> </h:panelGroup> <!-- ... --> </h:form> </ui:decorate>

slide-35
SLIDE 35

September 2010 Java EE 6, new features in practice - Part 02

35

Conclusions Facelets can provide decorator templates and custom components for forms and etc.; The Criteria API allows us to check our queries at compile time in exchange for increased complexity in the code; Conversations provide a new scope to which

  • bjects can be bound, also allowing for

management of multiple conversations; JSF 2.0 comes with built-in AJAX support.

slide-36
SLIDE 36

September 2010 Java EE 6, new features in practice - Part 02

36

Would you like to know more? Part 3, if you're interested, talks about:

JAAS, the Java Authentication and Authorization Services – manage users in the Application Server; Servlets 3.0: what's new in the oldest specification of Java for the Web; More JPA 2.0: new JPQL commands; EJB enhancements: no-interface EJBs, singleton EJBs, asynchronous methods.

slide-37
SLIDE 37

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.

Java EE 6 New features in practice Part 2