12.07.2015 Views

Ajax in Apache MyFaces - ApacheCon

Ajax in Apache MyFaces - ApacheCon

Ajax in Apache MyFaces - ApacheCon

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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


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


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)2


The New Web - Web 2.0•Highly dynamic and <strong>in</strong>teractive Websites•Openness (Interaction with otherSystems)•Break the Limitations of traditionalWebsites3


Web 2.0 - HistoryWeb 1.0Static HTML PagesWeb 1.5Dynamically generated HTMLWeb 2.0WebSites which dynamically<strong>in</strong>teract with the Server4


AJAX – a comb<strong>in</strong>ation of technologies• HTML (or XHTML) and CSS• JavaScript / DOM• XML / JSON (JavaScript objectorientedNotation)• JavaScript, XMLHttpRequest Object6


AJAX Interaction• an AJAX application looks as if it residedon the user's mach<strong>in</strong>e• JavaScript call to AJAX eng<strong>in</strong>e– HTTP - Request back to server• browser is updated with gathered<strong>in</strong>formation7


HTTP Request - Response8


HTTP Process Flow9


AJAX Request - Response10


AJAX Process Flow11


XMLHttp Object• Http request to server us<strong>in</strong>g aJavaScript object• 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");}12


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};13


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


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>g15


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 structure16


Why 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 needed17


Why AJAX? (cont<strong>in</strong>ued)• platform <strong>in</strong>dependent– works across all browsers if JavaScript isenabled– works across all operat<strong>in</strong>g systems18


Why not?• stress<strong>in</strong>g the server– more requests• state handl<strong>in</strong>g can be difficult– 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 framework19


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


What is JSF?• JSF is a ...• ... Java-Technology for Web-Apps• ... component-based framework• ... event-driven framework• ... RAD• ... <strong>in</strong>dustrial standard21


<strong>Ajax</strong> <strong>in</strong>tegrated <strong>in</strong>to JSF• Custom Servlets• Filters• PhaseListeners22


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 tocomponent/renderer– quits JSF life cycle after Response has been sent23


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


PPRPhaseListener(RenderResponse)25


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 leaks26


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)27


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 code28


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 programatically29


Dojo`s Widgets (cont<strong>in</strong>ued)• 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 runtime30


Dojo`s Widgets (cont<strong>in</strong>ued)• examples– ComboBox (InputSuggest<strong>Ajax</strong>)– Menues (FishEyeList)– Accordion– Tree– Editor– TabbedPane– Wizard31


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)32


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 down33


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


InputSuggest<strong>Ajax</strong>:suggestedItemsMethodpublic List getSuggestedProducts(Str<strong>in</strong>g prefix){List suggestedNames = newArrayList();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;35


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


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


AutoUpdateDataTable: .jsp38


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


PPRPanelGroup• Implementation of Partial PageRender<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 not40


PPRPanelGroup - Attributes• partialTriggers – List of Component Ids whichtrigger a partial update• partialTriggerPattern – Regular Expression: allmatch<strong>in</strong>g ClientIds trigger a partial update• <strong>in</strong>l<strong>in</strong>eLoad<strong>in</strong>gMessage – While updat<strong>in</strong>gPPRPanelGroup is replaced with Message41


PPRPanelGroup: .jsp


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.org43


Questions?Answers!!!44


F<strong>in</strong>ishThank you for yourAttention!45

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

Saved successfully!

Ooh no, something went wrong!