apacheconus2006 ajax in apache myfaces€¦ · the new web -web 2.0 •highly dynamic and...

46
0 Ajax in Apache MyFaces A new Approach to Web Applications

Upload: others

Post on 21-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

0

Ajax in Apache MyFaces

A new Approach to Web Applications

Page 2: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

1

Agenda

• Introduction AJAX and Web 2.0

• Integrating AJAX in JavaServer Faces

• AJAX components in MyFaces

• Discussion (or Question & Answer)

Page 3: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

2

Agenda

• Introduction AJAX and Web 2.0

• Integrating AJAX in JavaServer Faces

• AJAX components in MyFaces

• Discussion (or Question & Answer)

Page 4: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

3

The New Web - Web 2.0

•Highly dynamic and interactive Websites

•Openness (Interaction with other Systems)

•Break the Limitations of traditional Websites

Page 5: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

4

Web 2.0 - History

WebSites which dynamically interact with the Server

Web 2.0

Dynamically generated HTMLWeb 1.5

Static HTML PagesWeb 1.0

Page 6: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

5

What is AJAX?• a terminology affected by “Jesse James Garrett” from Adaptive Path in february2005

• short name for “Asynchronous JavaScript And XML”

• became a hype in 2005• popularity raised with the help of Google– Gmail, Google Maps, Google Calendar

Page 7: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

6

AJAX – a combination of technologies

• HTML (or XHTML) and CSS

• JavaScript / DOM

• XML / JSON (JavaScript objectorientedNotation)

• JavaScript, XMLHttpRequest Object

Page 8: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

7

AJAX Interaction

• an AJAX application looks as if it resided on the user's machine

• JavaScript call to AJAX engine– HTTP - Request back to server

• browser is updated with gathered information

Page 9: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

8

HTTP Request - Response

Page 10: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

9

HTTP Process Flow

Page 11: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

10

AJAX Request - Response

Page 12: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

11

AJAX Process Flow

Page 13: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

12

XMLHttp Object• Http request to server using a JavaScript object

• Microsoft:

– XMLHttp since IE 5.0 (ActiveX Object)

– Instantiation:if (window.ActiveXObject) //IE

{

http_request = new ActiveXObject

("Microsoft.XMLHTTP");

}

Page 14: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

13

XMLHttpRequest Object• Mozilla, Safari..

– XMLHttpRequest

– supports same methods and propertiesif (window.XMLHttpRequest) // Mozilla, Safari

{

http_request = new XMLHttpRequest();

}

http_request.onreadystatechange = function(){

// do useful stuff

};

• function call after server response: (for both)

Page 15: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

14

XMLHttpRequest Object• do the request:http_request.open('GET', URL, true);

if (http_request.readyState == 4)

GET, POST, HEAD…

http_request.send();

asynchronous/synchronous

AJAX!!

• response ok, continue processing:

if (http_request.status == 200)

• check the status code:

Page 16: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

15

JavaScript Libraries

• abstraction layer

– no handling of XMLHttpRequest object

• libraries provide…

– ...plain AJAX support

– ...visual effects

– ...widgets (dojo toolkit)

– ...ease of JavaScript handling

Page 17: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

16

JavaScript Libraries

• prototype (Ruby on Rails)– open-source JavaScript framework– used in Tobago project– former used in MyFaces AJAX components– also used by script.aculo.us

• open rico– drag and drop management

• dojo toolkit framework– widgets– namespace structure

Page 18: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

17

Why AJAX?

• richness and interactivity– web application -> desktop application

– AJAX effects look great

• only parts of the page are changed– less redundant data

– faster update of the page

• no stalling of user’s interaction with the application

• No additional plug-in like flash is needed

Page 19: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

18

Why AJAX? (continued)

• platform independent

– works across all browsers if JavaScript is enabled

– works across all operating systems

Page 20: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

19

Why not?

• stressing the server– more requests

• state handling can be difficult– Browsers „back“ button

– bookmarking

• need of JavaScript support

• differences of browsers -> more testing– not a big issue with JavaScript framework

Page 21: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

20

Agenda

• Introduction AJAX and Web 2.0

• Integrating AJAX in JavaServerFaces

– AJAX handling in MyFaces

• AJAX components in MyFaces

• Discussion (or Question & Answer)

Page 22: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

21

What is JSF?

• JSF is a ...

• ... Java-Technology for Web-Apps

• ... component-based framework

• ... event-driven framework

• ... RAD

• ... industrial standard

Page 23: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

22

Ajax integrated into JSF

• Custom Servlets

• Filters

• PhaseListeners

Page 24: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

23

AJAX handling in MyFaces• AjaxPhaseListeners(AjaxDecodePhaseListener, PPRPhaseListener)– listening for an incoming AJAX request

• tagged through Request Parameters which provide additional Information for the Request

– seeks the affected components in the JSF tree

– further processing delegated to component/renderer

– quits JSF life cycle after Response has been sent

Page 25: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

24

AjaxDecodePhaseListener(ApplyRequestValues)

Page 26: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

25

PPRPhaseListener(RenderResponse)

Page 27: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

26

AJAX handling in MyFaces : problems

• No concurrent access on UIViewRootallowed

• StateSaving

• Browser dependent memory leaks

Page 28: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

27

Agenda

• Introduction AJAX and Web 2.0

• Integrating AJAX in JavaServer Faces

• AJAX components in MyFaces

– Dojo`s toolkit

– Tomahawk

• Discussion (or Question & Answer)

Page 29: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

28

The Dojo Toolkit• MyFaces has used prototype -> now Dojo!• advantages

– huge library• abstracts common js handling• AJAX api• animation• event handling• widgets!

– clean separation through namespacing• avoids naming conflicts• supports lazy-loading of js-modules

– compression of js code

Page 30: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

29

Dojo`s Widgets• client side js components

• encapsulated js objects

• represent an abstraction layer– no need for html/dom handling

– well defined interface: only one tag!

• hence easy to use– eg. <input dojoType=“comboBox” dataUrl=“…”>

• not well-formed XHTML – can be also done programatically

Page 31: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

30

Dojo`s Widgets (continued)

• fast widget authoring

• client side widgets -> server side JSF comps

• ease of rendering– former: <div/>, <input>, <table/>, js-code,…

– now: only one widget tag!

• under the hood– one widget = many html tags at runtime

Page 32: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

31

Dojo`s Widgets (continued)

• examples– ComboBox (InputSuggestAjax)

– Menues (FishEyeList)

– Accordion

– Tree

– Editor

– TabbedPane

– Wizard

Page 33: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

32

Tomahawk Examples: SuggestAjax

• sandbox components– autosuggest control with AJAX

• InputSuggestAjax– shows suggested list of items– completely based upon dojo`s comboBox widget

• TableSuggestAjax– suggests a html table– column value -> DOM node– based upon dojo`s js abstraction– dojo`s AJAX API (dojo.io.bind)

Page 34: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

33

SuggestAjax• suggestedItemsMethod

– method of backing bean – delivers preview data – realized with JSF’s MethodBinding

• call order – 1.) Ajax request– 2.) AjaxDecodePhaseListener calls SuggestAjax comp– 3.) delegates encoding to renderer– 4.) renderer calls suggestionMethod in bean

(MethodBinding)– 5.) computing of result list – 6.) result sent back to client; dojo control shows

suggestion drop down

Page 35: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

34

InputSuggestAjax: .jsp<h:form><h:panelGrid columns="3">

<x:outputLabelvalue="#{label.title_product}"/>

<s:inputSuggestAjaxsuggestedItemsMethod="#{product.getSuggestedProducts}"

value="#{product.productNameToSearch}"/>

<x:commandButtonvalue="#{label.productButton}"action="#{product.searchForProducts}"/>

</h:panelGrid></h:form>

Page 36: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

35

InputSuggestAjax:suggestedItemsMethod

public List getSuggestedProducts(String prefix){

List<String> suggestedNames = new ArrayList<String>();

Session session = getSession();Criteria crit = session.createCriteria(Product.class).add(Restrictions.like("name", prefix+"%"));

List<Product> tempProds = crit.list();

for(Product prod : tempProds)suggestedNames.add(prod.getName());

return suggestedNames;}

Page 37: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

36

TableSuggestAjax: .jsp<s:tableSuggestAjax var="address"

startRequest="2“value="#{inputSuggestAjax.suggestValue}"

suggestedItemsMethod=

"#{inputSuggestAjax.getAddrList}">

<t:column>

<f:facet name="header">

<s:outputText value="city"/>

</f:facet>

<s:outputText for="cityField"

label="#{address.city}"/>

</t:column>

</s:tableSuggestAjax>

Page 38: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

37

AutoUpdateDataTable

• sandbox component

• frequency controlled updated DataTable

• uses prototype.js

– Ajax.PeriodicalUpdater(…)

Page 39: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

38

AutoUpdateDataTable: .jsp<h:form>

<s:autoUpdateDataTablevar="bids" value="#{dataTableData.bids}"preserveDataModel="true"frequency="3"><h:column><f:facet name="header"><h:outputText escape="false"

value="Bid"/></f:facet><h:outputText value="#{bids.bidData}" />

</h:column></s:autoUpdateDataTable>

</h:form>

Page 40: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

39

AJAX Form Components

• Server side validation through AJAX

• Model updating

Page 41: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

40

PPRPanelGroup

• Implementation of Partial Page Rendering in Apache MyFaces

• Each AJAX-Request Response fullfills a complete JSF-Request-Lifecycle (except RenderResponse which returns XML instead of HTML)

• Knows which Components trigger AJAX-Requests and which do not

Page 42: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

41

PPRPanelGroup - Attributes

• partialTriggers – List of Component Ids which trigger a partial update

• partialTriggerPattern – Regular Expression: all matching ClientIds trigger a partial update

• inlineLoadingMessage – While updating PPRPanelGroup is replaced with Message

Page 43: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

42

PPRPanelGroup: .jsp<h:form id="mainform">

<h:inputText value="#{exampleBean.suggestValue}"/>

<h:commandButton value="update" id=„button" />

<s:pprPanelGroup id="suggestValue"

partialTriggers="button">

<h:outputText

value="#{exampleBean.suggestValue}"/>

</s:pprPanelGroup>

</h:form>

Page 44: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

43

Links and books

• MyFaces AJAX examples– http://www.irian.at/myfaces.jsf

(sandbox components)

– http://tobago.atanion.net/tobago-example-demo

• AJAX web resources– http://www.script.aculo.us

– http://www.adaptivepath.com

– http://www.dojotoolkit.org

– http://www.ajaxpatterns.org/Ajax_Frameworks

– http://www.ajaxdeveloper.org

Page 45: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

44

Questions?

Answers!!!

Page 46: ApacheConUs2006 Ajax in Apache MyFaces€¦ · The New Web -Web 2.0 •Highly dynamic and interactive Websites •Openness ... HTTP Request -Response. 9 HTTP Process Flow. 10 AJAX

45

Finish

Thank you for your Attention!