tag:blogger.com,1999:blog-40121990538116473282026-04-16T15:02:00.861-07:00JSF 2.2 InceptTechnologieshttp://www.blogger.com/profile/02454707019611908154noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-4012199053811647328.post-74634354870837098302014-08-07T15:23:00.002-07:002014-08-07T15:23:59.774-07:00Flash ScopeFlash Scope provides a way to pass objects between various JSF pages or the user views generated by the faces lifecycle. The objects put in Flash scope are available only in the next view and then are cleared out.<br /> <br /> <br /> In JSF pages you can put or get value from Flash scope in the following ways<br /> <br /> <b>1) Put a value in using &lt;c:set&gt; </b><script src="https://gist.github.com/incepttechnologies/7258a013accd81fe0f5c.js"></script><br /> <b>2) Put a user entered value in flash scope.</b><br /> This comes in handy when we don't want to map a form field to a backing bean but still want it to make it available in the backing bean or to the next view. <script src="https://gist.github.com/incepttechnologies/4fec7686ec83c5d02ae0.js"></script> <br /> <b>3) Read/Print value from Flash Scope in JSF&nbsp;</b><script src="https://gist.github.com/incepttechnologies/2582520adf728648c6c6.js"></script> "<span style="color: red;">.keep</span>" makes the variable available in the next view as well. <br /> <br /> <b>3) Read/Print value from Flash Scope in Backing Bean</b><script src="https://gist.github.com/incepttechnologies/0c3d43c8cd697881f39c.js"></script> To <span style="color: red;">keep</span>" flash variables available to next view - fl.setKeepMessages(true); <br /> To put a new object in flash scope - fl.put("Greetings", "Hello")<br /> <br /> <a href="https://github.com/incepttechnologies/FlashExample" target="_blank"><span style="color: #0b5394; font-size: large;">Download Code</span></a>InceptTechnologieshttp://www.blogger.com/profile/02454707019611908154noreply@blogger.com87tag:blogger.com,1999:blog-4012199053811647328.post-46210234929672544682014-08-06T18:35:00.003-07:002014-08-12T23:42:50.428-07:00How to call a method during various JSF lifecycle phasesIn order to invoke a method during various lifecycle phases on JSF, you can either use JSF 2.0 <b>&lt;f:viewAction&gt;</b>tag. or <b>&lt;f:event&gt; </b>tag.<br /> The functioning of both tags is very similar, both need a definition for <b>trigger</b> mechanism(when to invoke method) and action mechanism(which method to invoke). These tags are defined in in the meta definition section of the page as follows<br /> <br /> <h3> <span style="color: #cc0000; font-size: large;"><b>&lt;f:viewAction&gt;</b></span></h3> This action component specifies an application-specific command (or action), using an EL method expression, to be invoked during one of the JSF lifecycle phases,<br /> <br /> &lt;f:metadata&gt;<br /> &nbsp; &lt;f:viewAction phase="PROCESS_VALIDATIONS" action="#{person.init()}"/&gt;<br /> &lt;/f:metadata&gt;<br /> <br /> The attributes <b>"action"</b><b> </b>and <b>"phase"</b><b> </b>define which method to invoke for an event.<br /> &nbsp;<b>action: </b>EL method expression i.e. method to invoke. e.g.#{person.init()}<br /> &nbsp;<b>phase: </b>Name of the event for which to install a listener. <br /> <br /> <br /> The above tag will result in person.init() method invoked <b>before </b>the "Process Validation" JSF lifecycle phase.<br /> <br /> The init method (in the "Person" BackingBean) can be as follows:<br /> <br /> public String init() { &nbsp; &nbsp; &nbsp; <br /> &nbsp; // do something like get List of persons from database<br /> }<br /> <br /> The "<b>phase</b>" attribute can have one of the possible values.<br /> <br /> <ol> <li>APPLY_REQUEST_VALUES</li> <li>PROCESS_VALIDATIONS&nbsp;</li> <li>UPDATE_MODEL_VALUES&nbsp;</li> <li>INVOKE_APPLICATION&nbsp;</li> </ol> <br /> The default is INVOKE_APPLICATION.<br /> <br /> <br /> <h3> <span style="color: #cc0000; font-size: large;"><b>&lt;f:event&gt;</b>&nbsp;</span></h3> This specifies an application-specific action, using an EL method expression, to be invoked during one of the JSF lifecycle phases.<br /> <div> <br /> &lt;f:metadata&gt;</div> <div> &nbsp; &lt;f:event type="preRenderView" listener="#{person.init()}"/&gt;</div> <div> &lt;/f:metadata&gt;</div> <br /> The attributes <b>"listener"</b><b> </b>and <b>"type"</b><b> </b>of <b>&lt;f:event&gt;</b> define which method to invoke for an event.<br /> &nbsp;<b>listener: </b>EL method expression i.e. method to invoke. e.g.#{person.init()}<br /> &nbsp;<b>type: </b>Name of the event for which to install a listener. <br /> <br /> <div> As a result of the above <b>&lt;f:event&gt; </b>declaration, JSF run-time invokes person.init() prior to rendering the components.<br /> <br /> The init method (in the "Person" BackingBean) is shown as follows:</div> <div> <br /></div> <div> public void init() { &nbsp; &nbsp; &nbsp; &nbsp;</div> <div> &nbsp; // do something like get List of persons from database</div> }<br /> <br /> <br /> The "type" attribute can have one of the possible values.<br /> <ol> <li>preRenderComponent</li> <li>preRenderView </li> <li><span class="changed_modified_2_0_rev_a">postAddToView</span> </li> <li>preValidate&nbsp;&nbsp;</li> <li>postValidate</li> </ol> <br /> <br /> <h3> <span style="color: blue;">&lt;f:viewAction&gt; differs from &lt;f:event&gt; in the following manner</span></h3> <br /> <ul> <li>View actions, by default is not invoked in post-backs; where as in f: event, post-backs needs to be programmatically checked.</li> <li>View actions have navigation capabilities, View actions support both implicit and explicit navigation. &nbsp;(action method returns backs a view or in xml), where as in f:event one needs to manage navigation programmatically using response.sendRedirect or using NavigationHandler</li> <li>View actions can be triggered early on, before a full component tree is built, resulting in a lighter weight call.</li> </ul> <br /> <br /> <br /> <br />InceptTechnologieshttp://www.blogger.com/profile/02454707019611908154noreply@blogger.com22tag:blogger.com,1999:blog-4012199053811647328.post-18646382832035492152014-08-06T18:02:00.002-07:002014-08-12T23:22:19.966-07:00 Call a method in Backing Bean prior to page rendering<br /> <div> A view is rendered by JSF engine in "Render Response Phase". In order to invoke a method right before a View is rendered in the "Render Response Phase", you can use jsf 2.0 <span style="font-size: large;"><b>&lt;f:event&gt;</b></span>. This tag is defined in in the meta definition section of a page as follows</div> <div> <br /></div> <div> <div> &lt;f:metadata&gt;</div> <div> &nbsp; &lt;f:event type="preRenderView" listener="#{person.init()}"/&gt;</div> <div> &lt;/f:metadata&gt;</div> <div> <br /> The attributes <b>"listener"</b><b> </b>and <b>"type"</b><b> </b>of <b>&lt;f:event&gt;</b> define which method to invoke for an event.<br /> &nbsp;<b>listener: </b>EL method expression i.e. method to invoke. e.g.#{person.init()}<br /> &nbsp;<b>type: </b>Name of the event for which to install a listener. <br /> <br /></div> <div> As a result of the above <b>&lt;f:event&gt; </b>declaration, JSF run-time invokes person.init() prior to rendering the components.<br /> <br /> The init method (in the "Person" BackingBean) is shown as follows:</div> <div> <br /></div> <div> public void init() { &nbsp; &nbsp; &nbsp; &nbsp;</div> <div> &nbsp; // do something like get List of persons from database</div> <div> }<br /> <br /> The JSF run-time invokes the method defined by <b>listener</b>, as per the event <b>"type"</b>. "<b>type</b>" attribute can take one of the following values<br /> <br /> <ol> <li>postAddToView: &nbsp;runs right after the component is added to view during view build time (which is usually during restore view phase, but can also be during render response phase, e.g. navigation).</li> <li>preValidate: &nbsp;runs right before the component is to be validated (which is usually during validations phase, but can also be apply request values phase if immediate="true").</li> <li>postValidate: runs right after the component is been validated (which is usually during validations phase, but can also be apply request values phase if immediate="true").</li> <li>preRenderView: runs right before the view is rendered during render response phase.</li> <li>preRenderComponent: runs right before the component is rendered during render response phase.</li> </ol> <div> <span style="font-size: x-small;">Note: Above list is from Balusc response on StackOverFlow(http://stackoverflow.com/questions/13999099/jsf-execution-order-of-fevents)</span><br /> <span style="font-size: x-small;"><br /></span> <br /> <h4> </h4> </div> </div> </div> InceptTechnologieshttp://www.blogger.com/profile/02454707019611908154noreply@blogger.com14tag:blogger.com,1999:blog-4012199053811647328.post-2180189379950057362014-08-05T15:56:00.002-07:002014-08-05T15:56:47.443-07:00Pass bean properties to JavaScript function<div> <br /></div> <div> In the JavaScript function call include bean properties in quotes</div> <div> <br /></div> <div> for example:</div> <div> <br /></div> <div> <span style="color: #0b5394;">&lt;h:form&gt;</span></div> <div> <span style="color: #0b5394;">&nbsp; &lt;h:commandButton type="button" value="JavaScript Call" onclick="printInfo('#{person.firstName}', '#{person.lastName}')"/&gt;</span></div> <div> <span style="color: #0b5394;">&lt;/h:form&gt;</span></div> <div> <br /></div> <div> The JavaScript function for this is shown in the following code:</div> <div> <br /></div> <div> <span style="color: #0b5394;">&lt;script type="text/javascript"&gt;</span></div> <div> <span style="color: #0b5394;">&nbsp; function printInfo(name, lastName) {</span></div> <div> <span style="color: #0b5394;">&nbsp; &nbsp; alert("Name: " + name + " " + lastName);</span></div> <div> <span style="color: #0b5394;">&nbsp; }</span></div> <div> <span style="color: #0b5394;">&lt;/script&gt;</span></div> InceptTechnologieshttp://www.blogger.com/profile/02454707019611908154noreply@blogger.com15tag:blogger.com,1999:blog-4012199053811647328.post-70914252532179396112013-05-20T18:03:00.001-07:002013-12-14T15:25:58.502-08:00Contexts and Dependency Injection(CDI) - Producer<br /> @Produces is a CDI mechanism which allows defining a source for injection. One can use Producer Method or Producer field to generate objects.<br /> <br /> <br /> Here is brief explanation from JEE 6 tutorial:<br /> <br /> A <b>producer method</b> generates an object that can then be injected. Typically, you use producer methods in the following situations:<br /> <ul> <li>When you want to inject an object that is not itself a bean<br /> </li> <li>When the concrete type of the object to be injected may vary at runtime<br /> </li> <li>When the object requires some custom initialization that the bean constructor does not perform</li> </ul> <div> (Code snippet from attached Source)<br /> <script src="https://gist.github.com/incepttechnologies/7966142.js"></script> </div> <div class="separator" style="clear: both; text-align: center;"> </div> <br /> <div class="separator" style="clear: both; text-align: center;"> </div> <br /> A <b>producer field</b> is a simpler alternative to a producer method; it is a field of a bean that generates an object. It can be used instead of a simple getter method. Producer fields are particularly useful for declaring Java EE resources such as data sources, JMS resources, and web service references.<br /> <br /> (Code snippet from attached Source)<br /> <script src="https://gist.github.com/incepttechnologies/7966187.js"></script> <div class="separator" style="clear: both; text-align: center;"> </div> <br /> This is how an Object in Injected<br /> <br /> (Code snippet from attached Source)<br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir-_LCnMjIHrUMyIPupmq_D1PmLVCLhEWUI-inGyejYk8sCPFukeliaJ5SmAWhog5GwRyiBJiq76d8_8JgUO04X866XdIeUMf6k9IbNHZu62ZtCXupOiTTBZNLZd9ljbFeJfyR8B4YBeq8/s1600/Field-Method-Injection.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir-_LCnMjIHrUMyIPupmq_D1PmLVCLhEWUI-inGyejYk8sCPFukeliaJ5SmAWhog5GwRyiBJiq76d8_8JgUO04X866XdIeUMf6k9IbNHZu62ZtCXupOiTTBZNLZd9ljbFeJfyR8B4YBeq8/s1600/Field-Method-Injection.bmp" /></a></div> <br /> <br /> This example shows both ways of declaring a producer and Injection.<br /> <br /> <a href="https://docs.google.com/file/d/0BzkDWZRDU0enWDdFajY1RWVuUDQ/edit?usp=sharing">Click here to download source code</a><br /> <br /> <br /> Prasanna BhaleInceptTechnologieshttp://www.blogger.com/profile/02454707019611908154noreply@blogger.com4tag:blogger.com,1999:blog-4012199053811647328.post-45838332104942330322013-05-15T18:22:00.002-07:002013-05-21T17:59:18.559-07:00JSF2.2 ViewScope Using CDIJSF 2.0 introduced annotation @ViewScoped; A bean annotated with this scope maintained its state as long as the user stays on the same view(reloads or navigation - no intervening views).<br /> <br /> One problem with the @ViewScoped is that it doesn't work with CDI (@Named); so one had to annotate bean as @ManagedBean to be ViewScoped.<br /> <br /> JSF2.2 has introduced a NEW annotation @ViewScoped in javax.faces.view.ViewScoped (NOT reusing the existing <em>javax.faces.bean.ViewScoped) </em>which is CDI compatible.<br /> <br /> Here is a working sample using Glassfish B4 v72.<br /> <br /> <a href="https://docs.google.com/file/d/0BzkDWZRDU0enZmZYemFyTXQ2MkU/edit?usp=sharing">Click here to download source code</a><br /> <br /> <br /> <br /> <br /> Prasanna Bhale<br /> <br /> <br />InceptTechnologieshttp://www.blogger.com/profile/02454707019611908154noreply@blogger.com7tag:blogger.com,1999:blog-4012199053811647328.post-50912427182066721682013-05-14T19:02:00.001-07:002013-05-21T17:59:03.717-07:00JSF 2.2 Faces Flow <span style="color: #cc0000;"><b>JSF 2.2 Faces Flow &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</b></span><br /> <span style="color: #cc0000;"><b><br /></b></span> As the API for JSF 2.2 is evolving, I struggled a bit to get Arun Gupta's Faces Flow sample code (from blog <a href="https://blogs.oracle.com/arungupta/entry/jsf_2_2_faces_flow">JSF 2.2 Faces Flow</a>&nbsp;)working.<br /> <br /> I have finally got it to work after some minor API/code changes and so, here with sharing with rest of developer's community.<br /> <br /> Download the source code from here<br /> <br /> <a href="https://docs.google.com/file/d/0BzkDWZRDU0enVlBIcFl6OENKZEE/edit?usp=sharing">Click here to download source code</a><br /> <br /> <br /> Prasanna Bhale<br /> <br /> <br /> <br />InceptTechnologieshttp://www.blogger.com/profile/02454707019611908154noreply@blogger.com33