Digital Java EE 7 with JSF
Conversations, Flows, and
CDI Scopes
Peter Pilgrim
JavaOne - CON5211
29th October 2015
Digital Java EE 7
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 2
About me
▪ Java Champion
▪ Java programming since 1998
▪ Independent contractor, LTD UK
▪ Java EE developer, designer and book author
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 3
Agenda
▪ Java EE 7 modern architecture
▪ CDI Scopes
▪ Conversation scoped beans
▪ Faces-Flows and flow scoped beans
▪ Bootstrap your enterprise
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 4
"Nobody is madder than me
about the fact that the
website isn't working”
President Obama
Rose Garden Speech, 21th October 2013
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 5
Demonstration
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 6
Digital Java EE application
Digital Java EE 7 Platform
▪ Java EE 7 is standard architecture for
building enterprise Java solutions
▪ Transactions, persistence, management
▪ Dynamic content: Servlets, JSP, JSF and
MVC
▪ Remote endpoints EJB, JAX-RS, JMS
WebSocket and JCA
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 7
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 8
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 9
EJB Container
Poolable managed remote end points with transactions and
concurrency , which are application wide
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 10
EJB Beans
@Stateless
class ProductQueryService { ... }
@Stateful
class ShoppingCartDao { ... }
@MessageDriven
class OrderProcessor implements
MessageListener { ... }
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 11
CDI Container
Dependency Injection with lifecycle and contextual information
managing POJO instances in an enterprise web application
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 12
CDI Beans
@Named @RequestScoped
class LivePriceController { ... }
@Named @SessionScoped
class ShoppingCart implements Serializable {
... }
@ApplicationScoped
class Utility implements Serializable { ... }
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 13
Servlet Container
A specific container to manage Java Servlets, Filters and Context
Listeners for a web application HTTP
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 14
Web Infrastructure Beans
@WebServlet(“/prices/live”)
class LivePriceServlet extends HttpServlet {
... }
@WebFilter(“/prices”)
class PriceEnricher implements Filter { ... }
@WebListener
class UserSecurityRemover implements
HttpSessionListener,
HttpSessionAttributeListener { ... }
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 15
Request Scope
Session Scope
Application
Scope
Arrow ofTime
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 16
Context & Dependency
Injection 1.1
▪ Different lifespans for stateful objects
▪ Typesafe dependency injection
▪ Can decorate injected objects
▪ Can interpret methods
▪ Event notification model
▪ Portable extensions mechanism through SPI
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 17
Injection from CDI container
interface PaymentIssuer {
void allocate( int id );
}
@ApplicationScoped
public class CreditCardTicketTrackerImpl {
@Inject PaymentIssuer issuer;
/* ... */
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 18
CDI Qualifiers
import java.inject.Qualifier;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.FIELD;
@Qualifier
@Retention(RUNTIME)
@Target({FIELD}
public @interface Premier { }
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 19
Use CDI Qualifiers #1
@Premier
class StellarPaymentImpl implements PaymentIssuer {
void allocate( int id ) { ... }
}
@Standard
class BasicPaymentImpl implements PaymentIssuer {
void allocate( int id ) { ... }
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 20
Use CDI Qualifiers #2
@ApplicationScoped
public class SmartCreditCardTicketTracker() {
@Premium @Inject PaymentIssuer issuer1;
@Standard @Inject PaymentIssuer issuer2;
public void processTickets() {
if (nightTime) { ... } else { ... }
}
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 21
Injection from EJB container
@Stateless public class OrderDAO {
List<Order> findByCustomer( int custId ) {...}
}
@SessionScoped
public class CustomerOrderProcessor {
@EJB OrderDao orderDao;
/* ... */
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 22
Injection from Servlet
container
class LivePriceServlet
extends HttpServlet { ... }
@Named @RequestScoped
class EntryController {
@Inject LivePriceServlet servlet;
// This does not work!
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 23
CDI: Inject a long lifespan
bean to a short lifespan bean
@ApplicationScoped class Utility { ...
}
@Named @RequestScoped
class DeliveryController {
@Inject Utility utility;
/* ... */
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 24
CDI: Inject a long lifespan
bean to a short lifespan bean
@ApplicationScoped class Utility
implements Serializable { ... }
@Named @SessionScoped
class ShoppingCartController implements
Serializable {
@Inject Utility utility;
/* ... */
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 25
CDI: Inject a short lifespan
bean to a long lifespan bean
@RequestScoped class SecureDomain {
... }
@Named @SessionScoped
class ShoppingCartController ... {
@Inject SecureDomain secDomain;
public void doAction() { ... }
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 26
CDI: Inject a short lifespan
bean to a long lifespan bean
@RequestScoped class SecureDomain { ... }
@Named @SessionScoped
class ShoppingCartController ... {
@Inject SecureDomain secDomain;
public void doAction() {
secDomain.invoke(...);
/* YMMV - it might work because of proxies */
}
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 27
CDI Events – Define a domain
value object
public class LivePriceEvent {
private String product;
private double price;
public LivePriceEvent(String product, double price)
{ ... }
public String getProduct() { ... }
public double getPrice() { ... }
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 28
CDI Event Source
@ApplicationScoped
public class PriceUpdater {
@Inject Event<LivePriceEvent> events;
public void announce() {
events.fire(new LivePriceEvent(
"Digital Java EE", 45.0 ));
}
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 29
CDI Event Target
@ApplicationScoped public class PriceListener {
public void listenAndReport(
@Observes LivePriceEvent event ){
System.out.printf("event.product = %sn",
event.getProduct());
System.out.printf("event.price = %sn",
event.getPrice());
}
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 30
Demonstration
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 31
View Scoped Form
JSF View Scoped Controller #1
▪ @ViewScoped belongs to JSF specification
▪ Retains the scope lifespan for current page view
▪ If the controller navigates away to a different page
view the bean is de-scoped
▪ Therefore view-scope is great for form validation
and richAJAX request and response sequences!
▪ #Fail for HTTP POST-REDIRECT-GET idiom
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 32
JSF View Scoped Controller #2
@Named @ViewScoped class ContactDetailController {
@EJB ContactDetailService dao;
ContactDetail contactDetail = new ContactDetail();
public String createContact() {
dao.add(contactDetail);
contactDetail = new ContactDetail();
return "index.xhtml";
}
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 33
Flash Scope #1
▪ JSF supports a flash map collection for controllers
▪ Only accessible through FacesContext
1. JSF places the flash data temporarily into the
HttpSession before it creates the Faces Response.
2. A special flash cookie is set before sending a
redirect HTTP response.
3. A HTTP client resends the flash cookie in a Faces
Request. JSF removes the flash cookie.
4. JSF copies the flash session data to request scope
and them removes it from the session scope.
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 34
Flash Scope #2
@Named @ViewScoped class FrenchSuitController {
private String firstName, lastName;
public String doAction() {
Flash flash = FacesContext.getCurrentInstance().
getExternalContext().getFlash();
flash.put("firstName",firstName);
flash.put("lastName",lastName);
return "/jsf-validation/french-suit-
complete?redirect=true";
} ...
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 35
Flash Scope #3
<ui:define name="mainContent">
<h1> House of Cards with JSF Validation</h1>
<div class="jumbotron">
Terrific! You completed the French suite action.
Your first name is <b>#{flash['firstName']}</b> and
your last name is <b>#{flash['lastName']}</b> and
you chose <b>#{flash['suit']}</b> as the suit.
</div> ...
</ui:define>
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 36
JSF 2.2 supports HTML5 content!
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 37
HTML5 friendly markup in JSF
2.2
▪ In JSF 2.2, by default XHTML are rendered with
HTML5 <DOCTYPE html>
▪ Pass-through attributes
▪ Pass-through elements
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 38
Pass-through attributes
<html xmlns="http://www.w3.org/1999/xhtml" ...
xmlns:p="http://xmlns.jcp.org/jsf/passthrough">
<div class="col-sm-9">
<h:inputText p:type="date"
class="form-control" id="birthday"
value="#{customerController.birthday}"
p:placeholder="1977-10-25” />
</div>
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 39
Pass-through elements
<html xmlns="http://www.w3.org/1999/xhtml” ...
xmlns:jsf="http://xmlns.jcp.org/jsf">
<head jsf:id="head"> ... </head>
<body jsf:id="body">
<form jsf:id="vacationForm">
...
<input type="text" jsf:id="companyId"
placeholder="Your company identity number”
jsf:value="#{vacationController.companyNo}” />
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 40
Conversations
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 41
Demonstration
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 42
Instant Smart Secure
Conversation scope
▪ A lifespan sits between a Http Request scope and
Http Session scope
▪ Maintains state for the unique interaction
▪ Works for individual tab in web browsers
▪ Better than @ViewScoped bean controllers
▪ Application defined lifespan
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 43
Conversation scoped bean #1
public interface Conversation {
void begin();
void begin(String id);
void end();
String getId();
long getTimeout();
void setTimeout(long milliseconds);
boolean isTransient();
} // import javax.enterprise.context.*;
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 44
Conversation scoped bean #2
@Named @ConversationScoped
public class LendingController
implements Serializable {
@EJB ApplicantService applicantService;
@Inject Conversation conversation;
@Inject Utility utility;
public String doConfirm() { ... }
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 45
Conversation scoped bean #3
@Named @ConversationScoped class LendingController ... {
public void checkAndStart() {
if ( conversation.isTransient()) conversation.begin();
recalculatePMT();
}
public void checkAndEnd() {
if (!conversation.isTransient()) conversation.end();
}
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 46
Conversation scoped bean #4
@Named @ConversationScoped
class LendingController ... {
public String doLandingPage() {
checkAndStart();
return "your-details?faces-redirect=true";
}
...
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 47
Conversation scoped bean #5
@Named @ConversationScoped
class LendingController ... {
public String doCancel() {
checkAndEnd();
return "index.xhtml?faces-redirect=true";
}
...
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 48
Conversation scope
conclusions
Pros
▪ Conversation scope is great for “wizards”
▪ Linear sequence of pages
▪ Conversation is unique per tab
▪ Application defines the entry & exit points
Cons
▪ Explicitly termination of conversation
▪ Customer jumps away and back again
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 49
JSF 2.2 Faces Flows
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 50
Flow scope #1
▪ Brand new scope in JSF 2.2 and Java EE 7
▪ Modular in execution
▪ Concept of nodes
▪ Associated with a URI path
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 51
Flow scope #2
▪ Flows can nested; a flow can invoke another
▪ Parameters and data can pass to and back through
Flow scope map collection
▪ package javax.faces.flow.*
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 52
Flow scope #3
▪ A Flow is a folder name in your web context
application
▪ A Flow must have a Flow Definition XML file that
matches the folder name
▪ <Flow-Name>/<Flow-Name>-flow.xml
▪ This file can be blank – implicit flow navigation
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 53
Flow node types
▪ View – represents any JSF page view
▪ Method Call – method invocation through EL
(expression language)
▪ FlowCall – invocation of another flow with inbound
and outbound parameters
▪ Flow Return – return to the calling flow
▪ Switch – navigation selection through EL
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 54
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 55
sector-flow
carbon-footprint-flow
Start
End
Demonstration
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 56
Flow scope application
Flow layout project
src/main/webapp/sector/
src/main/webapp/sector/sector-flow.xml
src/main/webapp/sector/sector.xhtml
src/main/webapp/sector/sector-page-1a.xthml
src/main/webapp/sector/sector-page-1b.xthml
src/main/webapp/sector/sector-page-1c.xthml
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 57
Flow layout project #2
src/main/webapp/footprint/
src/main/webapp/footprint/footprint-flow.xml
src/main/webapp/footprint/footprint.xhtml
src/main/webapp/footprint/footprint-page-1a.xml
src/main/webapp/endflow.xhtml
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 58
Flow scoped controller #1
@Named @FlowScoped("sector")
public class SectorFlow
implements Serializable {
@Inject UtilityHelper utilityHelper;
@Inject CarbonFootprintService service;
public SectorFlow() { ... }
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 59
Flow scoped controller #2
@Named @FlowScoped("sector")
public class SectorFlow
implements Serializable {
@Inject UtilityHelper utilityHelper;
@Inject CarbonFootprintService service;
public SectorFlow() { ... }
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 60
Flow scoped controller #4
public class SectorFlow ...{
@PostConstruct
public void initialize() {
footprint.setApplicationId(
utilityHelper.getNextApplicationId());
}
...
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 61
Flow scoped controller #5
public class SectorFlow ...{
public String gotoEndFlow() {
return "/endflow.xhtml";
}
public String saveFootprintRecord() {
service.add(footprint);
return "sector-page-1c.xhtml";
}
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 62
Flow definition XML file #1
<faces-config version="2.2" ...>
<flow-definition id="sector">
<flow-return id="goHome">
<from-outcome>/index</from-outcome>
</flow-return>
...
</flow-definition>
</faces-config>
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 63
Flow definition XML file #2
<faces-config version="2.2" ...>
<flow-definition id="sector">
<flow-return id="endFlow">
<from-outcome>#{sectorFlow.gotoEndFlow()}
</from-outcome>
</flow-return>
<flow-call id="callFootprintFlow”>...</flow-call>
</flow-definition> ...
</faces-config>
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 64
Flow definition XML file #3
<flow-call id="callFootprintFlow">
<flow-reference><flow-id>footprint</flow-id>
</flow-reference> ...
<outbound-parameter>
<name>param3FromSectorFlow</name>
<value>#{sectorFlow.footprint}</value>
</outbound-parameter>
</flow-call>
</flow-definition>
</faces-config>
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 65
Nested flow scope #1
@Named @FlowScoped("footprint")
public class FootprintFlow implements Serializable {
private CarbonFootprint footprint;
@PostConstruct public void initialize() {}
Map<Object,Object> flowMap =
FacesContext.getCurrentInstance()
.getApplication().getFlowHandler()
.getCurrentFlowScope();
footprint = (CarbonFootprint) flowMap.get("param3Value");
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 66
Nested flow scope #2
@Named @FlowScoped("footprint")
public class FootprintFlow ... {
public String exitFromFootprintFlow() {
return ”/sector";
}
. . .
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 67
Nested flow scope #3
@Named @FlowScoped("footprint")
public class FootprintFlow ... {
. . .
public String gotoPage2() {
return "footprint-page-1a";
}
}
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 68
Nested Flow definition XML
file #1
<faces-config version="2.2" ...>
<flow-definition id="footprint-flow">
<flow-return id="goHome">
<from-outcome>/index</from-outcome>
</flow-return>
...
</faces-config>
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 69
Nested Flow definition XML
file #2
<faces-config version="2.2" ...>
<flow-definition id="footprint"> ...
<flow-return id=”exitToCallerFlow">
<from-outcome>#{footprintFlow.exitFromFootprintFlow}
</from-outcome>
</flow-return>
<flow-return id="exitToSectorFlow">
<from-outcome>/sector-flow</from-outcome>
</flow-return>
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 70
Nested Flow definition XML
file #3
<flow-definition id="footprint"> ...
<inbound-parameter>
<name>param1FromSectorFlow</name>
<value>#{flowScope.param1Value}</value>
</inbound-parameter> ...
<inbound-parameter>
<name>param3FromSectorFlow</name>
<value>#{flowScope.param3Value}</value>
</inbound-parameter>
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 71
Flow scope bean conclusions
Pros
▪ Conversation scope is great for “wizards”
▪ Flow node demarcated by URI folder convention
▪ Nestable flow customer journeys
Cons
▪ Handling REDIRECT is difficult (JSF 2.3 perhaps?)
▪ Flow scope parameter passing can be peculiar
▪ Only one START NODE and not configurable
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 72
Choosing a scope for a
managed bean controller
▪ Sharing data, utility and information for all users,
prefer @ApplicationScoped
▪ Sharing information and behavior for an individual
customer, prefer @SessionScoped
▪ Customer journey restricted to a specific set of page
views, choose @FlowScoped
▪ Defined customer journey and custom application
entry and exit points, choose @ConversationScoped
▪ Basic rich validation entry and exit point, prefer
@ViewScoped
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 73
Adding Finesse
▪ Consider CSS framework such as Bootstrap
(http://getbootstrap.com) or Foundation
(http://foundation.zurb.com/ )
▪ Consider SASS or LESS for CSS templates
▪ Learn JavaScript programming
▪ Maybe learn aboutTranspilers
▪ Consider RequireJS (http://requirejs.org/) for
organising JavaScript modules
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 74
Design considerations
▪ Evolution of object oriented JavaScript leads to rich
frameworks inside the client’s web browser
▪ JSF lends itself to page view navigation
▪ Hybrid design is possible with JSF and adopting
JavaScript controller
▪ JSF 2.2 Faces Flows is a major feature worthy of
investigation
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 75
Non-Functional Requirement
HealthCare.gov site must
handle 10,000 customers at
once.
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 76
It failed with just a few
hundred users.
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 77
HealthCare.gov (USA)
Website cost $300 million price tag
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 78
Don’t worry USA!
British NHS IT system was abandoned after 10 years
and cost the tax payer £10bn so far
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 79
▪ Innovate with optionality
▪ Constraints
▪ Build fast, fail fast
▪ Communication
▪ Technology == evolution
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 80
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 81
Executive Summary
Hints and tips and where to go next
Conclusions
▪ Leverage Java EE 7 platform
▪ Choose scope by lifespan of the bean
▪ Prefer Faces Flows for digital customer journeys
▪ Otherwise Conversation-Scoped beans
▪ Prefer HTML5 friendly markup of Facelets views
▪ Prefer CDI
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 82
Conclusions #2
▪ Protect your from UX changes
▪ Keep action controller methods short
▪ Delegate complex logic to business logic
▪ Use DOMAIN MODEL CONTEXT objects
▪ Avoid methods with long parameter lists!
▪ Inject DOMAIN MODEL LOGIC
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 83
Contacts
▪ Follow me onTwitter @peter_pilgrim
▪ Read my blog http://xenonique.co.uk/blog/
▪ Books from Packt Publishing
–Digital Java EE 7
–Java EE 7 Developer Handbook
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 84
Photo credits (cc) 2016
▪ by Joshua Earle https://unsplash.com/joshuaearle
▪ By Lili Popper https://unsplash.com/lili_popper
▪ By Rowan Heuvel https://unsplash.com/insolitus
▪ By David Marcu https://stocksnap.io/author/302
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 85
Photo credits (cc) 2016
▪ By Gemma Bou (hand fractal)
https://www.flickr.com/photos/gemmabou/
▪ By Nicole Hanusek (luggage)
https://www.flickr.com/photos/nhanusek/
▪ By Navaneeth KN (group photo painted stork)
https://www.flickr.com/photos/navaneethkn/
▪ By Ruben Schade (container ship in the Straits of
Singapore) https://www.flickr.com/photos/rubenerd/
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 86
Photo credits (cc) 2016
▪ Joel Cooper (big face 1)
https://www.flickr.com/photos/origamijoel/
▪ Nullfy from nullfy.com! (Ring of Power!)
https://www.flickr.com/photos/130197050@N04/
▪ philippe leroyer (Student Demonstration (46) - 27Nov07, Paris
(France)) https://www.flickr.com/photos/philippeleroyer/
▪ philippe leroyer (Pupil's Demonstration (10) - 10Apr08, Paris
(France)) https://www.flickr.com/photos/philippeleroyer/
JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 87

JavaOne 2015 CON5211 Digital Java EE 7 with JSF Conversations, Flows, and CDI Scopes

  • 1.
    Digital Java EE7 with JSF Conversations, Flows, and CDI Scopes Peter Pilgrim JavaOne - CON5211 29th October 2015
  • 2.
    Digital Java EE7 JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 2
  • 3.
    About me ▪ JavaChampion ▪ Java programming since 1998 ▪ Independent contractor, LTD UK ▪ Java EE developer, designer and book author JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 3
  • 4.
    Agenda ▪ Java EE7 modern architecture ▪ CDI Scopes ▪ Conversation scoped beans ▪ Faces-Flows and flow scoped beans ▪ Bootstrap your enterprise JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 4
  • 5.
    "Nobody is madderthan me about the fact that the website isn't working” President Obama Rose Garden Speech, 21th October 2013 JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 5
  • 6.
    Demonstration JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 6 Digital Java EE application
  • 7.
    Digital Java EE7 Platform ▪ Java EE 7 is standard architecture for building enterprise Java solutions ▪ Transactions, persistence, management ▪ Dynamic content: Servlets, JSP, JSF and MVC ▪ Remote endpoints EJB, JAX-RS, JMS WebSocket and JCA JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 7
  • 8.
    JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 8
  • 9.
    JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 9
  • 10.
    EJB Container Poolable managedremote end points with transactions and concurrency , which are application wide JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 10
  • 11.
    EJB Beans @Stateless class ProductQueryService{ ... } @Stateful class ShoppingCartDao { ... } @MessageDriven class OrderProcessor implements MessageListener { ... } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 11
  • 12.
    CDI Container Dependency Injectionwith lifecycle and contextual information managing POJO instances in an enterprise web application JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 12
  • 13.
    CDI Beans @Named @RequestScoped classLivePriceController { ... } @Named @SessionScoped class ShoppingCart implements Serializable { ... } @ApplicationScoped class Utility implements Serializable { ... } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 13
  • 14.
    Servlet Container A specificcontainer to manage Java Servlets, Filters and Context Listeners for a web application HTTP JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 14
  • 15.
    Web Infrastructure Beans @WebServlet(“/prices/live”) classLivePriceServlet extends HttpServlet { ... } @WebFilter(“/prices”) class PriceEnricher implements Filter { ... } @WebListener class UserSecurityRemover implements HttpSessionListener, HttpSessionAttributeListener { ... } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 15
  • 16.
    Request Scope Session Scope Application Scope ArrowofTime JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 16
  • 17.
    Context & Dependency Injection1.1 ▪ Different lifespans for stateful objects ▪ Typesafe dependency injection ▪ Can decorate injected objects ▪ Can interpret methods ▪ Event notification model ▪ Portable extensions mechanism through SPI JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 17
  • 18.
    Injection from CDIcontainer interface PaymentIssuer { void allocate( int id ); } @ApplicationScoped public class CreditCardTicketTrackerImpl { @Inject PaymentIssuer issuer; /* ... */ } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 18
  • 19.
    CDI Qualifiers import java.inject.Qualifier; importjava.lang.annotation.*; import static java.lang.annotation.ElementType.FIELD; @Qualifier @Retention(RUNTIME) @Target({FIELD} public @interface Premier { } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 19
  • 20.
    Use CDI Qualifiers#1 @Premier class StellarPaymentImpl implements PaymentIssuer { void allocate( int id ) { ... } } @Standard class BasicPaymentImpl implements PaymentIssuer { void allocate( int id ) { ... } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 20
  • 21.
    Use CDI Qualifiers#2 @ApplicationScoped public class SmartCreditCardTicketTracker() { @Premium @Inject PaymentIssuer issuer1; @Standard @Inject PaymentIssuer issuer2; public void processTickets() { if (nightTime) { ... } else { ... } } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 21
  • 22.
    Injection from EJBcontainer @Stateless public class OrderDAO { List<Order> findByCustomer( int custId ) {...} } @SessionScoped public class CustomerOrderProcessor { @EJB OrderDao orderDao; /* ... */ } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 22
  • 23.
    Injection from Servlet container classLivePriceServlet extends HttpServlet { ... } @Named @RequestScoped class EntryController { @Inject LivePriceServlet servlet; // This does not work! } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 23
  • 24.
    CDI: Inject along lifespan bean to a short lifespan bean @ApplicationScoped class Utility { ... } @Named @RequestScoped class DeliveryController { @Inject Utility utility; /* ... */ } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 24
  • 25.
    CDI: Inject along lifespan bean to a short lifespan bean @ApplicationScoped class Utility implements Serializable { ... } @Named @SessionScoped class ShoppingCartController implements Serializable { @Inject Utility utility; /* ... */ } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 25
  • 26.
    CDI: Inject ashort lifespan bean to a long lifespan bean @RequestScoped class SecureDomain { ... } @Named @SessionScoped class ShoppingCartController ... { @Inject SecureDomain secDomain; public void doAction() { ... } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 26
  • 27.
    CDI: Inject ashort lifespan bean to a long lifespan bean @RequestScoped class SecureDomain { ... } @Named @SessionScoped class ShoppingCartController ... { @Inject SecureDomain secDomain; public void doAction() { secDomain.invoke(...); /* YMMV - it might work because of proxies */ } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 27
  • 28.
    CDI Events –Define a domain value object public class LivePriceEvent { private String product; private double price; public LivePriceEvent(String product, double price) { ... } public String getProduct() { ... } public double getPrice() { ... } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 28
  • 29.
    CDI Event Source @ApplicationScoped publicclass PriceUpdater { @Inject Event<LivePriceEvent> events; public void announce() { events.fire(new LivePriceEvent( "Digital Java EE", 45.0 )); } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 29
  • 30.
    CDI Event Target @ApplicationScopedpublic class PriceListener { public void listenAndReport( @Observes LivePriceEvent event ){ System.out.printf("event.product = %sn", event.getProduct()); System.out.printf("event.price = %sn", event.getPrice()); } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 30
  • 31.
    Demonstration JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 31 View Scoped Form
  • 32.
    JSF View ScopedController #1 ▪ @ViewScoped belongs to JSF specification ▪ Retains the scope lifespan for current page view ▪ If the controller navigates away to a different page view the bean is de-scoped ▪ Therefore view-scope is great for form validation and richAJAX request and response sequences! ▪ #Fail for HTTP POST-REDIRECT-GET idiom JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 32
  • 33.
    JSF View ScopedController #2 @Named @ViewScoped class ContactDetailController { @EJB ContactDetailService dao; ContactDetail contactDetail = new ContactDetail(); public String createContact() { dao.add(contactDetail); contactDetail = new ContactDetail(); return "index.xhtml"; } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 33
  • 34.
    Flash Scope #1 ▪JSF supports a flash map collection for controllers ▪ Only accessible through FacesContext 1. JSF places the flash data temporarily into the HttpSession before it creates the Faces Response. 2. A special flash cookie is set before sending a redirect HTTP response. 3. A HTTP client resends the flash cookie in a Faces Request. JSF removes the flash cookie. 4. JSF copies the flash session data to request scope and them removes it from the session scope. JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 34
  • 35.
    Flash Scope #2 @Named@ViewScoped class FrenchSuitController { private String firstName, lastName; public String doAction() { Flash flash = FacesContext.getCurrentInstance(). getExternalContext().getFlash(); flash.put("firstName",firstName); flash.put("lastName",lastName); return "/jsf-validation/french-suit- complete?redirect=true"; } ... JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 35
  • 36.
    Flash Scope #3 <ui:definename="mainContent"> <h1> House of Cards with JSF Validation</h1> <div class="jumbotron"> Terrific! You completed the French suite action. Your first name is <b>#{flash['firstName']}</b> and your last name is <b>#{flash['lastName']}</b> and you chose <b>#{flash['suit']}</b> as the suit. </div> ... </ui:define> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 36
  • 37.
    JSF 2.2 supportsHTML5 content! JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 37
  • 38.
    HTML5 friendly markupin JSF 2.2 ▪ In JSF 2.2, by default XHTML are rendered with HTML5 <DOCTYPE html> ▪ Pass-through attributes ▪ Pass-through elements JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 38
  • 39.
    Pass-through attributes <html xmlns="http://www.w3.org/1999/xhtml"... xmlns:p="http://xmlns.jcp.org/jsf/passthrough"> <div class="col-sm-9"> <h:inputText p:type="date" class="form-control" id="birthday" value="#{customerController.birthday}" p:placeholder="1977-10-25” /> </div> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 39
  • 40.
    Pass-through elements <html xmlns="http://www.w3.org/1999/xhtml”... xmlns:jsf="http://xmlns.jcp.org/jsf"> <head jsf:id="head"> ... </head> <body jsf:id="body"> <form jsf:id="vacationForm"> ... <input type="text" jsf:id="companyId" placeholder="Your company identity number” jsf:value="#{vacationController.companyNo}” /> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 40
  • 41.
    Conversations JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 41
  • 42.
    Demonstration JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 42 Instant Smart Secure
  • 43.
    Conversation scope ▪ Alifespan sits between a Http Request scope and Http Session scope ▪ Maintains state for the unique interaction ▪ Works for individual tab in web browsers ▪ Better than @ViewScoped bean controllers ▪ Application defined lifespan JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 43
  • 44.
    Conversation scoped bean#1 public interface Conversation { void begin(); void begin(String id); void end(); String getId(); long getTimeout(); void setTimeout(long milliseconds); boolean isTransient(); } // import javax.enterprise.context.*; JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 44
  • 45.
    Conversation scoped bean#2 @Named @ConversationScoped public class LendingController implements Serializable { @EJB ApplicantService applicantService; @Inject Conversation conversation; @Inject Utility utility; public String doConfirm() { ... } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 45
  • 46.
    Conversation scoped bean#3 @Named @ConversationScoped class LendingController ... { public void checkAndStart() { if ( conversation.isTransient()) conversation.begin(); recalculatePMT(); } public void checkAndEnd() { if (!conversation.isTransient()) conversation.end(); } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 46
  • 47.
    Conversation scoped bean#4 @Named @ConversationScoped class LendingController ... { public String doLandingPage() { checkAndStart(); return "your-details?faces-redirect=true"; } ... } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 47
  • 48.
    Conversation scoped bean#5 @Named @ConversationScoped class LendingController ... { public String doCancel() { checkAndEnd(); return "index.xhtml?faces-redirect=true"; } ... } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 48
  • 49.
    Conversation scope conclusions Pros ▪ Conversationscope is great for “wizards” ▪ Linear sequence of pages ▪ Conversation is unique per tab ▪ Application defines the entry & exit points Cons ▪ Explicitly termination of conversation ▪ Customer jumps away and back again JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 49
  • 50.
    JSF 2.2 FacesFlows JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 50
  • 51.
    Flow scope #1 ▪Brand new scope in JSF 2.2 and Java EE 7 ▪ Modular in execution ▪ Concept of nodes ▪ Associated with a URI path JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 51
  • 52.
    Flow scope #2 ▪Flows can nested; a flow can invoke another ▪ Parameters and data can pass to and back through Flow scope map collection ▪ package javax.faces.flow.* JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 52
  • 53.
    Flow scope #3 ▪A Flow is a folder name in your web context application ▪ A Flow must have a Flow Definition XML file that matches the folder name ▪ <Flow-Name>/<Flow-Name>-flow.xml ▪ This file can be blank – implicit flow navigation JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 53
  • 54.
    Flow node types ▪View – represents any JSF page view ▪ Method Call – method invocation through EL (expression language) ▪ FlowCall – invocation of another flow with inbound and outbound parameters ▪ Flow Return – return to the calling flow ▪ Switch – navigation selection through EL JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 54
  • 55.
    JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 55 sector-flow carbon-footprint-flow Start End
  • 56.
    Demonstration JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 56 Flow scope application
  • 57.
  • 58.
    Flow layout project#2 src/main/webapp/footprint/ src/main/webapp/footprint/footprint-flow.xml src/main/webapp/footprint/footprint.xhtml src/main/webapp/footprint/footprint-page-1a.xml src/main/webapp/endflow.xhtml JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 58
  • 59.
    Flow scoped controller#1 @Named @FlowScoped("sector") public class SectorFlow implements Serializable { @Inject UtilityHelper utilityHelper; @Inject CarbonFootprintService service; public SectorFlow() { ... } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 59
  • 60.
    Flow scoped controller#2 @Named @FlowScoped("sector") public class SectorFlow implements Serializable { @Inject UtilityHelper utilityHelper; @Inject CarbonFootprintService service; public SectorFlow() { ... } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 60
  • 61.
    Flow scoped controller#4 public class SectorFlow ...{ @PostConstruct public void initialize() { footprint.setApplicationId( utilityHelper.getNextApplicationId()); } ... } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 61
  • 62.
    Flow scoped controller#5 public class SectorFlow ...{ public String gotoEndFlow() { return "/endflow.xhtml"; } public String saveFootprintRecord() { service.add(footprint); return "sector-page-1c.xhtml"; } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 62
  • 63.
    Flow definition XMLfile #1 <faces-config version="2.2" ...> <flow-definition id="sector"> <flow-return id="goHome"> <from-outcome>/index</from-outcome> </flow-return> ... </flow-definition> </faces-config> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 63
  • 64.
    Flow definition XMLfile #2 <faces-config version="2.2" ...> <flow-definition id="sector"> <flow-return id="endFlow"> <from-outcome>#{sectorFlow.gotoEndFlow()} </from-outcome> </flow-return> <flow-call id="callFootprintFlow”>...</flow-call> </flow-definition> ... </faces-config> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 64
  • 65.
    Flow definition XMLfile #3 <flow-call id="callFootprintFlow"> <flow-reference><flow-id>footprint</flow-id> </flow-reference> ... <outbound-parameter> <name>param3FromSectorFlow</name> <value>#{sectorFlow.footprint}</value> </outbound-parameter> </flow-call> </flow-definition> </faces-config> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 65
  • 66.
    Nested flow scope#1 @Named @FlowScoped("footprint") public class FootprintFlow implements Serializable { private CarbonFootprint footprint; @PostConstruct public void initialize() {} Map<Object,Object> flowMap = FacesContext.getCurrentInstance() .getApplication().getFlowHandler() .getCurrentFlowScope(); footprint = (CarbonFootprint) flowMap.get("param3Value"); } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 66
  • 67.
    Nested flow scope#2 @Named @FlowScoped("footprint") public class FootprintFlow ... { public String exitFromFootprintFlow() { return ”/sector"; } . . . } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 67
  • 68.
    Nested flow scope#3 @Named @FlowScoped("footprint") public class FootprintFlow ... { . . . public String gotoPage2() { return "footprint-page-1a"; } } JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 68
  • 69.
    Nested Flow definitionXML file #1 <faces-config version="2.2" ...> <flow-definition id="footprint-flow"> <flow-return id="goHome"> <from-outcome>/index</from-outcome> </flow-return> ... </faces-config> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 69
  • 70.
    Nested Flow definitionXML file #2 <faces-config version="2.2" ...> <flow-definition id="footprint"> ... <flow-return id=”exitToCallerFlow"> <from-outcome>#{footprintFlow.exitFromFootprintFlow} </from-outcome> </flow-return> <flow-return id="exitToSectorFlow"> <from-outcome>/sector-flow</from-outcome> </flow-return> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 70
  • 71.
    Nested Flow definitionXML file #3 <flow-definition id="footprint"> ... <inbound-parameter> <name>param1FromSectorFlow</name> <value>#{flowScope.param1Value}</value> </inbound-parameter> ... <inbound-parameter> <name>param3FromSectorFlow</name> <value>#{flowScope.param3Value}</value> </inbound-parameter> JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 71
  • 72.
    Flow scope beanconclusions Pros ▪ Conversation scope is great for “wizards” ▪ Flow node demarcated by URI folder convention ▪ Nestable flow customer journeys Cons ▪ Handling REDIRECT is difficult (JSF 2.3 perhaps?) ▪ Flow scope parameter passing can be peculiar ▪ Only one START NODE and not configurable JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 72
  • 73.
    Choosing a scopefor a managed bean controller ▪ Sharing data, utility and information for all users, prefer @ApplicationScoped ▪ Sharing information and behavior for an individual customer, prefer @SessionScoped ▪ Customer journey restricted to a specific set of page views, choose @FlowScoped ▪ Defined customer journey and custom application entry and exit points, choose @ConversationScoped ▪ Basic rich validation entry and exit point, prefer @ViewScoped JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 73
  • 74.
    Adding Finesse ▪ ConsiderCSS framework such as Bootstrap (http://getbootstrap.com) or Foundation (http://foundation.zurb.com/ ) ▪ Consider SASS or LESS for CSS templates ▪ Learn JavaScript programming ▪ Maybe learn aboutTranspilers ▪ Consider RequireJS (http://requirejs.org/) for organising JavaScript modules JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 74
  • 75.
    Design considerations ▪ Evolutionof object oriented JavaScript leads to rich frameworks inside the client’s web browser ▪ JSF lends itself to page view navigation ▪ Hybrid design is possible with JSF and adopting JavaScript controller ▪ JSF 2.2 Faces Flows is a major feature worthy of investigation JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 75
  • 76.
    Non-Functional Requirement HealthCare.gov sitemust handle 10,000 customers at once. JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 76
  • 77.
    It failed withjust a few hundred users. JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 77
  • 78.
    HealthCare.gov (USA) Website cost$300 million price tag JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 78
  • 79.
    Don’t worry USA! BritishNHS IT system was abandoned after 10 years and cost the tax payer £10bn so far JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 79
  • 80.
    ▪ Innovate withoptionality ▪ Constraints ▪ Build fast, fail fast ▪ Communication ▪ Technology == evolution JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 80
  • 81.
    JavaOne 2015 -CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 81 Executive Summary Hints and tips and where to go next
  • 82.
    Conclusions ▪ Leverage JavaEE 7 platform ▪ Choose scope by lifespan of the bean ▪ Prefer Faces Flows for digital customer journeys ▪ Otherwise Conversation-Scoped beans ▪ Prefer HTML5 friendly markup of Facelets views ▪ Prefer CDI JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 82
  • 83.
    Conclusions #2 ▪ Protectyour from UX changes ▪ Keep action controller methods short ▪ Delegate complex logic to business logic ▪ Use DOMAIN MODEL CONTEXT objects ▪ Avoid methods with long parameter lists! ▪ Inject DOMAIN MODEL LOGIC JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 83
  • 84.
    Contacts ▪ Follow meonTwitter @peter_pilgrim ▪ Read my blog http://xenonique.co.uk/blog/ ▪ Books from Packt Publishing –Digital Java EE 7 –Java EE 7 Developer Handbook JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 84
  • 85.
    Photo credits (cc)2016 ▪ by Joshua Earle https://unsplash.com/joshuaearle ▪ By Lili Popper https://unsplash.com/lili_popper ▪ By Rowan Heuvel https://unsplash.com/insolitus ▪ By David Marcu https://stocksnap.io/author/302 JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 85
  • 86.
    Photo credits (cc)2016 ▪ By Gemma Bou (hand fractal) https://www.flickr.com/photos/gemmabou/ ▪ By Nicole Hanusek (luggage) https://www.flickr.com/photos/nhanusek/ ▪ By Navaneeth KN (group photo painted stork) https://www.flickr.com/photos/navaneethkn/ ▪ By Ruben Schade (container ship in the Straits of Singapore) https://www.flickr.com/photos/rubenerd/ JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 86
  • 87.
    Photo credits (cc)2016 ▪ Joel Cooper (big face 1) https://www.flickr.com/photos/origamijoel/ ▪ Nullfy from nullfy.com! (Ring of Power!) https://www.flickr.com/photos/130197050@N04/ ▪ philippe leroyer (Student Demonstration (46) - 27Nov07, Paris (France)) https://www.flickr.com/photos/philippeleroyer/ ▪ philippe leroyer (Pupil's Demonstration (10) - 10Apr08, Paris (France)) https://www.flickr.com/photos/philippeleroyer/ JavaOne 2015 - CON5211 - P.E.A.T UK LTD (C) follow me on Twitter @peter_pilgrim 87

Editor's Notes

  • #28 You probably need to use Java Web Filter mechanism to add the security domain to your request scope