12.07.2015 Views

Ajax in Apache MyFaces - ApacheCon

Ajax in Apache MyFaces - ApacheCon

Ajax in Apache MyFaces - ApacheCon

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Ajax</strong> <strong>in</strong> <strong>Apache</strong> <strong>MyFaces</strong>A new Approach to Web Applications


Agenda• Introduction AJAX and Web 2.0• Integrat<strong>in</strong>g AJAX <strong>in</strong> JavaServer Faces• AJAX components <strong>in</strong> <strong>MyFaces</strong>• Discussion (or Question & Answer)1


The New Web - Web 2.0•desktop- vs. web applications•Web 2.0• general def<strong>in</strong>ition• fully-fledged comput<strong>in</strong>g platforms• serve web applications to end users• personal content, social networks• purely web based• RSS, Blogs, Wikis2


The New Web - Web 2.0• complex and evolv<strong>in</strong>g technology <strong>in</strong>frastructure• server software• client applications• content syndication• messag<strong>in</strong>g protocols• A Web 2.0 site is built of many techniques• one of these is AJAX3


What is AJAX?• a new approach to web applications• a term<strong>in</strong>ology affected by “Jesse JamesGarrett” from Adaptive Path <strong>in</strong> february 2005• short name for “Asynchronous JavaScript AndXML”• becomes a hype <strong>in</strong> 2005• popularity raised with the help of Google• Gmail, Google Maps, Google Calendar4


What is AJAX?• a bundle of common used techniques• HTML (or XHTML) and CSS• Document Object Model (DOM)• XML• JavaScript, XMLHttpRequest Object• hence not a new technology• former called “server <strong>in</strong>teraction viaXMLHttpRequest”5


AJAX Interaction• an AJAX application looks as if it residedon the user's mach<strong>in</strong>e• data is asynchronously fetched• JavaScript call to AJAX eng<strong>in</strong>e• HTTP - Request back to server• browser is updated with gathered<strong>in</strong>formation• not entirely refreshed6


7HTTP Request - Response


8HTTP Process Flow


9AJAX Request - Response


10AJAX Process Flow


XMLHttp Object• Http request to server us<strong>in</strong>g a JavaScriptobject• Microsoft:• XMLHttp s<strong>in</strong>ce IE 5.0 (ActiveX Object)• Instantiation:if (w<strong>in</strong>dow.ActiveXObject) // IE{http_request = new ActiveXObject("Microsoft.XMLHTTP");}11


XMLHttpRequest Object• Mozilla, Safari..• XMLHttpRequest• supports same methods and propertiesif (w<strong>in</strong>dow.XMLHttpRequest) // Mozilla, Safari{http_request = new XMLHttpRequest();}• function call after server response: (for both)http_request.onreadystatechange = function(){// do useful stuff};12


XMLHttpRequest Object• do the request:http_request.open('GET', URL, true);GET, POST, HEAD…http_request.send();if (http_request.readyState == 4)asynchronous/synchronousAJAX!!• response ok, cont<strong>in</strong>ue process<strong>in</strong>g:• check the status code:if (http_request.status == 200)13


JavaScript Libraries• abstraction layer• no handl<strong>in</strong>g of XMLHttpRequest object• libraries provide..• ..pla<strong>in</strong> AJAX support• ..visual effects• ..widgets (dojo toolkit)• ..ease of JavaScript handl<strong>in</strong>g14


JavaScript Libraries• prototype (Ruby on Rails)• open-source JavaScript framework• used <strong>in</strong> Tobago project• former used <strong>in</strong> <strong>MyFaces</strong> AJAX components• also used by script.aculo.us• open rico• drag and drop management• dojo toolkit framework• widgets• namespace structure15


Pros of AJAX• richness and <strong>in</strong>teractivity• web application -> desktop application• AJAX effects look great• only parts of the page are changed• less redundant data• faster update of the page• no stall<strong>in</strong>g of user’s <strong>in</strong>teraction with theapplication• No additional plug-<strong>in</strong> like flash is needed16


Pros of AJAX• platform <strong>in</strong>dependent• works across all browsers if JavaScript isenabled• works across all operat<strong>in</strong>g systems17


Cons of AJAX• stress<strong>in</strong>g the server• more requests, data• web application can feel <strong>in</strong>active to the user• visualization• state handl<strong>in</strong>g• Browsers „back“ button• bookmark<strong>in</strong>g• need of JavaScript support• differences of browsers -> more test<strong>in</strong>g• not a big issue with JavaScript framework18


Agenda• Introduction AJAX and Web 2.0• Integrat<strong>in</strong>g AJAX <strong>in</strong> JavaServer Faces• AJAX handl<strong>in</strong>g <strong>in</strong> <strong>MyFaces</strong>• AJAX components <strong>in</strong> <strong>MyFaces</strong>• Discussion (or Question & Answer)19


AJAX handl<strong>in</strong>g <strong>in</strong> <strong>MyFaces</strong>• <strong>Ajax</strong>PhaseListeners (<strong>Ajax</strong>DecodePhaseListener,PPRPhaseListener)• listen<strong>in</strong>g for an <strong>in</strong>com<strong>in</strong>g AJAX request• tagged through Request Parameters which provideadditional Information for the Request• seeks the affected components <strong>in</strong> the JSF tree• further process<strong>in</strong>g delegated to component/renderer• quits JSF life cycle after Response has been sent20


<strong>Ajax</strong>DecodePhaseListener(ApplyRequestValues)21


PPRPhaseListener(RenderResponse)22


AJAX handl<strong>in</strong>g <strong>in</strong> <strong>MyFaces</strong> :problems• No concurrent access on UIViewRootallowed• StateSav<strong>in</strong>g• Browser dependent memory leaks23


Agenda• Introduction AJAX and Web 2.0• Integrat<strong>in</strong>g AJAX <strong>in</strong> JavaServer Faces• AJAX components <strong>in</strong> <strong>MyFaces</strong>• Dojo`s toolkit• Tomahawk• Discussion (or Question & Answer)24


The Dojo Toolkit• <strong>MyFaces</strong> has used prototype -> now Dojo!• advantages• huge library• abstracts common js handl<strong>in</strong>g• AJAX api• animation• event handl<strong>in</strong>g• widgets!• clean separation through namespac<strong>in</strong>g• avoids nam<strong>in</strong>g conflicts• supports lazy-load<strong>in</strong>g of js-modules• compression of js code25


Dojo`s Widgets• client side js components• encapsulated js objects• represent an abstraction layer• no need for html/dom handl<strong>in</strong>g• well def<strong>in</strong>ed <strong>in</strong>terface: only one tag!• hence easy to use• eg. • not well-formed XHTML• can be also done programatically26


Dojo`s Widgets• fast widget author<strong>in</strong>g• client side widgets -> server side JSFcomps• ease of render<strong>in</strong>g• former: , , , js-code…• now: only one widget tag!• under the hood• one widget = many html tags at runtime27


Dojo`s Widgets• examples• ComboBox (InputSuggest<strong>Ajax</strong>)• Menues (FishEyeList)• Accordion• Tree• Editor• TabbedPane• Wizard28


Tomahawk Examples: Suggest<strong>Ajax</strong>• sandbox components• autosuggest control with AJAX• InputSuggest<strong>Ajax</strong>• shows suggested list of items• completely based upon dojo`s comboBox widget• TableSuggest<strong>Ajax</strong>• suggests a html table• column value -> DOM node• based upon dojo`s js abstraction• dojo`s AJAX API (dojo.io.b<strong>in</strong>d)29


Suggest<strong>Ajax</strong>• suggestedItemsMethod• method of back<strong>in</strong>g bean• delivers preview data• realized with JSF’s MethodB<strong>in</strong>d<strong>in</strong>g• call order• 1.) <strong>Ajax</strong> request• 2.) <strong>Ajax</strong>DecodePhaseListener calls Suggest<strong>Ajax</strong> comp• 3.) delegates encod<strong>in</strong>g to renderer• 4.) renderer calls suggestionMethod <strong>in</strong> bean (MethodB<strong>in</strong>d<strong>in</strong>g)• 5.) comput<strong>in</strong>g of result list• 6.) result sent back to client; dojo control showssuggestion drop down30


InputSuggest<strong>Ajax</strong>: .jsp31


InputSuggest<strong>Ajax</strong> :suggestedItemsMethodpublic List getSuggestedProducts(Str<strong>in</strong>g prefix){List suggestedNames = new ArrayList();Session session = getSession();Criteria crit = session.createCriteria(Product.class).add(Restrictions.like("name","%"+prefix+"%"));List tempProds = crit.list();for(Product prod : tempProds)suggestedNames.add(prod.getName());}return suggestedNames;32


TableSuggest<strong>Ajax</strong>: .jsp…33


AutoUpdateDataTable• sandbox component• frequency controlled updated DataTable• uses prototype.js• <strong>Ajax</strong>.PeriodicalUpdater(…)34


AutoUpdateDataTable: .jsp35


AJAX Form Components• Server side validation through AJAX• Model updat<strong>in</strong>g36


PPRPanelGroup• Implementation of Partial Page Render<strong>in</strong>g<strong>in</strong> <strong>Apache</strong> <strong>MyFaces</strong>• Each AJAX-Request Response fullfills acomplete JSF-Request-Lifecycle (exceptRenderResponse which returns XML<strong>in</strong>stead of HTML)• Knows which Components trigger AJAX-Requests and which do not37


PPRPanelGroup: .jsp38


L<strong>in</strong>ks and books• <strong>MyFaces</strong> 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/<strong>Ajax</strong>_Frameworks• http://www.ajaxdeveloper.org39


Questions ?•Answers!40


F<strong>in</strong>ishThank you for your attention!41

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!