tag:blogger.com,1999:blog-63585399981924015092024-02-07T14:49:19.413-08:00 OmniFaces & JSF FansAnonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.comBlogger621125tag:blogger.com,1999:blog-6358539998192401509.post-60414182527147011732016-11-07T03:58:00.001-08:002016-11-07T04:00:55.673-08:00JSF 2.3 comes with @ManagedProperty compatible with CDIThe JSF managed bean annotations will be deprecated in JSF 2.3. This includes <span style="font-family: "courier new"; font-size: 9pt;">@ManagedProperty </span>as well. But, compared to other annotations, this one has got an implementation that can be used with CDI managed beans. So, in CDI managed beans we can use the new, <span style="font-family: "courier new"; font-size: 9pt;">javax.faces.annotation.ManagedProperty</span>. Let's see an example of injecting an instance of <span style="font-family: "courier new"; font-size: 9pt;">SourceBean </span>into <span style="font-family: "courier new"; font-size: 9pt;">TargetBean,</span> and a property of <span style="font-family: "courier new"; font-size: 9pt;">SourceBean </span>into <span style="font-family: "courier new"; font-size: 9pt;">TargetBean:</span><br />
<span style="font-family: "courier new"; font-size: 9pt;"><br /></span>
<span style="font-family: "courier new"; font-size: 9pt;">@Named</span><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
SourceBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private String source;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @PostConstruct<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void init(){<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> source = "SourceBean";<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public String getSource() {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return source;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void setSource(String source) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> this.source = source;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }
<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;">import
javax.faces.annotation.ManagedProperty;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;">...<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
TargetBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;"> @Inject
@ManagedProperty("#{sourceBean}")<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private SourceBean sourceBean;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;"> @Inject
@ManagedProperty("#{sourceBean.source}")<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private String source;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void targetAction(){<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> System.out.println("Injected bean:
" + sourceBean);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> System.out.println("Injected property
(via injected bean): " + sourceBean.getSource());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> System.out.println("Injected property:
" + source);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
The complete example is available <a href="https://github.com/AnghelLeonard/JSF-2.3/tree/master/JSF23ManagedProperty">here</a>.</div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com3tag:blogger.com,1999:blog-6358539998192401509.post-19192278313826321612016-11-06T01:31:00.000-07:002016-11-06T01:32:19.351-08:00JSF 2.3 - The JSF managed bean annotations deprecatedStarting with JSF 2.3 the JSF "native" managed bean annotations are <a href="http://arjan-tijms.omnifaces.org/p/jsf-23.html#1417">officially</a> deprecated. So, there is no doubt now that CDI is the road to follow :)<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIazm9M6uwCZbbLt6HZCCRVgoL3y71gbyVkIKzrf6E67pa81LxNmKEFX2RAXvyxmDAJeF5toMdtMU3ABQvgfB5H7CBrVcle7fN757gLOksdmDVE9CJedADSER-fb5OeQfA95Yb48dOS6SV/s1600/jsf+manged+beans+are+deprecated.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="125" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIazm9M6uwCZbbLt6HZCCRVgoL3y71gbyVkIKzrf6E67pa81LxNmKEFX2RAXvyxmDAJeF5toMdtMU3ABQvgfB5H7CBrVcle7fN757gLOksdmDVE9CJedADSER-fb5OeQfA95Yb48dOS6SV/s320/jsf+manged+beans+are+deprecated.png" width="320" /></a></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com1tag:blogger.com,1999:blog-6358539998192401509.post-84141859157124678612016-08-21T22:30:00.000-07:002016-08-21T22:30:23.245-07:00PrimeFaces & OmniFaces Powers Combined Final Release<div class="separator" style="clear: both; text-align: center;">
<a href="https://leanpub.com/PrimeFaces-OmniFaces-Powers-Combined"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzWzalAE8mKeyGiEYoqDUryajjYtCJfPiXZZnokW3X8Dg5D8Ojo5TRKWlm2P9l_NthG2dKsTjEmZ3XbtXfnTcE7DPLTJ9WNqnqtLOA1KE4fOPwnHeU2RnfNqpaDIsczu1QWwgTRtARxlN0/s1600/primefaces+and+omnifaces+powers+combined.png" /></a></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-34107876924451502102016-08-04T20:54:00.005-07:002016-08-04T20:57:15.910-07:00JavaServer Faces 2.3: What's Coming by Arjan Tijms<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.javamagazine.mozaicreader.com/JulyAug2016#&pageSet=17&page=0"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSGVs_GTLFp_Rz7E0lBDhEzafJQSVQGGoszXRm5QUO5FEg9z8Az2Qx4xSCrlIsh2O6C-EE61Abm5DXtxCrgM1FRfg2PB_USlaozn7mPOmQRMbbUQDW4tWxXlJWRBcc8ybHn9JK9HcK3Y7U/s1600/jsf+2.3.png" /></a></div>
<div style="text-align: center;">
<span style="background-color: #eeeeee; color: #666666; font-family: "raleway" , "helvetica neue" , sans-serif; font-size: 20px; line-height: 34px;">If you like this article, I think you are going to like <a href="http://www.omnifaces-fans.org/p/jsf-23-tutorial.html">JSF 2.3 Tutorial</a> as well.</span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com2tag:blogger.com,1999:blog-6358539998192401509.post-89233132635384175032016-08-04T20:44:00.004-07:002016-08-04T20:45:22.247-07:00New video course available at Packt<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.packtpub.com/web-development/jsf-fundamentals-video"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh11lbSqZIWwx6fhuNQGcIj8aVnNzAMLM88SNHfyQXrl3E6LuhlkpW8zyxcvK2z5PyPS2R2RqN2QpkoBmIdLjC2vi-KlIrQ-BrZjBmd0hlY9U8tw1msthLz1o6QRj7hpJyWGsFe8DCXS53u/s1600/jsf+fundamentals+packt.png" /></a></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-72428553212038152122016-07-31T07:30:00.000-07:002016-07-31T07:37:38.582-07:00[OmniFaces utilities (2.4)] Format the given number to nearest 10^n (rounded to thousands)<a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-format-given.html" id="number_6_24" name="number_6_24"></a>
<hr />
[OmniFaces utilities] The <code>formatThousands()</code> formats the given number to nearest 10n (rounded to thousands), immediately suffixed (without space) with metric unit (k, M, G, T, P or E), rounding half up with a precision of 3 digits, whereafter trailing zeroes in fraction part are stripped. Numbers lower than thousand are not affected. The format locale will be set to the one as obtained by <code>Faces#getLocale()</code>. If the value is <code>null</code>, <code>NaN</code> or infinity, then this will return <code>null</code>.<br />
<b><span style="color: #990000;"><br /></span></b>
<b><span style="color: #990000;">This <a href="http://www.omnifaces-fans.org/2016/02/omnifaces-utilities-23-format-given.html">function</a> is available from OmniFaces 2.3, but in
OmniFaces 2.4, the incorrectly trimmed trailing zeroes from non-fractional
integer numbers was fixed and now also supports cutting down fractions of
values lower than thousand.</span></b><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p></o:p></div>
<hr />
<b>Function:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihKEevhA-shiCRAbSnULp-hQnHfbP9tRbWUMhPrLb5v0wB1da01S6yCYZfvJBw4qRuwEZ2ytjKVeOgwByPko77mfaCKm_TGBPF8iKbX0kNndo9WfBx5ssO0LyNyr3tDRgguyK79mPe0m6M/s1600/omnifaces+numbers+6+-+1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihKEevhA-shiCRAbSnULp-hQnHfbP9tRbWUMhPrLb5v0wB1da01S6yCYZfvJBw4qRuwEZ2ytjKVeOgwByPko77mfaCKm_TGBPF8iKbX0kNndo9WfBx5ssO0LyNyr3tDRgguyK79mPe0m6M/s400/omnifaces+numbers+6+-+1.png" width="330" /></a></div>
<div class="separator" style="background-color: white; clear: both; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; text-align: center;">
<span style="font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">See also: </span><span style="font-family: monospace; font-size: 13.2px; line-height: 18.48px;"><a href="http://www.omnifaces-fans.org/2015/03/omnifaces-utilities-20-getset-locales.html" style="color: #7b9fcf; text-decoration: none;">Faces#getLocale()</a> | </span><span style="font-family: "courier new"; font-size: 12px; line-height: 16.8px;"><a href="http://www.omnifaces-fans.org/2016/02/omnifaces-utilities-23-format-given.html" style="color: #7b9fcf; text-decoration: none;">of:formatThousands()</a></span></div>
<div class="separator" style="background-color: white; clear: both; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; text-align: center;">
<br /></div>
<div class="separator" style="background-color: white; clear: both; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;">
<b>Usage:</b></div>
<div class="separator" style="background-color: white; clear: both; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">// 9.99 k<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">#{of:formatThousandsUnit(9994)}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">// 10 M<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">#{of:formatThousandsUnit(9995000)}<o:p></o:p></span><br />
<span style="font-family: "courier new"; font-size: 9pt;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">// 532 k<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">#{of:formatThousandsUnit(532230.6483)}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<br /></div>
<div class="separator" style="background-color: white; clear: both; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;">
</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"><a href="http://omnifaces.org/docs/javadoc/2.4/" style="color: #7b9fcf; text-decoration: none;">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/2.4/src/main/java/org/omnifaces/el/functions/Numbers.java" style="color: #7b9fcf; text-decoration: none;">GH</a></span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-63011052374122252522016-07-23T06:11:00.003-07:002016-07-31T07:32:35.883-07:00[OmniFaces utilities (2.4)] Format the given number to nearest 10^n, suffixed with a space, the metric unit prefix and the given unit<a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-format-given.html" id="number_7_24" name="number_7_24"></a>
<hr />
[OmniFaces utilities] The <code>formatThousandsUnit()</code> formats the given number to nearest 10<sup>n</sup> (rounded to thousands), suffixed with a space, the metric unit prefix (k, M, G, T, P or E) and the given unit, rounding half up with a precision of 3 digits, whereafter trailing zeroes in fraction part are stripped. The format locale will be set to the one as obtained by <code>Faces#getLocale()</code>. If the value is <code>null</code>, <code>NaN</code> or infinity, then this will return <code>null</code>.The given unit used in the format is of type <code>B</code> for Bytes, <code>W</code> for Watt, etc. If the unit is <code>null</code>, then this method will behave exactly as described in <code>#formatThousands(Number)</code>.<br />
<b><span style="color: #990000;"><br /></span></b>
<b><span style="color: #990000;">This <a href="http://www.omnifaces-fans.org/2016/02/omnifaces-utilities-23-format-given_9.html">function</a> is available from OmniFaces 2.3, but in
OmniFaces 2.4, the incorrectly trimmed trailing zeroes from non-fractional
integer numbers was fixed and now also supports cutting down fractions of
values lower than thousand.</span></b><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p></o:p></div>
<hr />
<b>Function:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpEbh2rYiEsxsVYiTVLl9CpZnUw52Ia-ZkUTeuccJQ_5-i9PSuONvR1aiHrbRVIdoWUCGwfDXF3EODSgBAv6x95ifmwP75crk1ME7vwDKmrwMZH9vH4-O4xlP6khcvc8XtWPMjAtEZvVNx/s1600/omnifaces+numbers+7+24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpEbh2rYiEsxsVYiTVLl9CpZnUw52Ia-ZkUTeuccJQ_5-i9PSuONvR1aiHrbRVIdoWUCGwfDXF3EODSgBAv6x95ifmwP75crk1ME7vwDKmrwMZH9vH4-O4xlP6khcvc8XtWPMjAtEZvVNx/s400/omnifaces+numbers+7+24.png" width="356" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">See also: </span><span style="background-color: white; color: #666666; font-family: monospace; font-size: 13.2px; line-height: 18.48px;"><a href="http://www.omnifaces-fans.org/2015/03/omnifaces-utilities-20-getset-locales.html" style="color: #7b9fcf; text-decoration: none;">Faces#getLocale()</a> | </span><span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 12px; line-height: 16.8px;"><a href="http://www.omnifaces-fans.org/2016/02/omnifaces-utilities-23-format-given.html" style="color: #7b9fcf; text-decoration: none;">of:formatThousands()</a></span></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<b>Usage:</b></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">// 9.99 kB<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">#{of:formatThousandsUnit(9994 , "B")}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">// 10 MW<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">#{of:formatThousandsUnit(9995000
, "W")}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">// 532 kFOO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">#{of:formatThousandsUnit(532230.6483
, "FOO")}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="separator" style="clear: both;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><a href="http://omnifaces.org/docs/javadoc/2.4/">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/2.4/src/main/java/org/omnifaces/el/functions/Numbers.java">GH</a></span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-89566368271690644232016-07-18T04:28:00.001-07:002016-07-18T04:35:27.197-07:00PrimeFaces vs OmniFaces exception handlers<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhryanRH2S8__vNqzEuMoJ2S7OWWNX1Vo11Ee7E-dCHC05MmcTv9s2iSwRfBQbcInJ7ZKfry0W2Dq4_oDYMU_JxlwmBbLLws6BF1IecUDobaQ89R-zSYetlsWcmZJLF894CljXh18eVbO1Y/s1600/figure_137.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhryanRH2S8__vNqzEuMoJ2S7OWWNX1Vo11Ee7E-dCHC05MmcTv9s2iSwRfBQbcInJ7ZKfry0W2Dq4_oDYMU_JxlwmBbLLws6BF1IecUDobaQ89R-zSYetlsWcmZJLF894CljXh18eVbO1Y/s1600/figure_137.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-7607688349504631802016-07-17T03:28:00.001-07:002016-07-17T03:51:17.604-07:00[OmniFaces utilities (2.4)] Get all request query string or view parameters appended with all child UIParameter components of the given parent component<a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-get-all-request.html" id="components_244" name="components_244"></a>
<hr />
[OmniFaces utilities] The <code>getParams()</code> returns an unmodifiable map with all request query string or view parameters, appended with all child <code>UIParameter</code> components (<code><f|o:param></code>) of the given parent component. Those with <code>disabled=true</code> or an empty name or an empty value are skipped. The <code><f|o:param></code> will override any included view or request parameters on the same name.<br />
<hr />
<b>Method:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjga1pBBIKMzgdlweQoZI7b-pqbi__C33ljSasRE76CNQowuKq0XTVqhPUgX0HWbwJDLTVT5XZJXsvpSCcPattpXsMSQxmfX33qm-uLF1L2A8btng__b5BzFaMjDA2ldophivDRr5eYoTtT/s1600/omnifaces+components+37.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="218" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjga1pBBIKMzgdlweQoZI7b-pqbi__C33ljSasRE76CNQowuKq0XTVqhPUgX0HWbwJDLTVT5XZJXsvpSCcPattpXsMSQxmfX33qm-uLF1L2A8btng__b5BzFaMjDA2ldophivDRr5eYoTtT/s400/omnifaces+components+37.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
See also: <span style="font-family: "courier new"; font-size: 12px;"><a href="http://www.omnifaces-fans.org/2015/06/omnifaces-utilities-20-get-http-request_4.html">Servlets.getRequestQueryStringMap()</a> | <a href="http://www.omnifaces-fans.org/2015/03/omnifaces-utilities-20-get-view.html">Faces.getViewParameterMap()</a> | <a href="http://www.omnifaces-fans.org/2015/06/omnifaces-utilities-20-get-unmodifiable.html">Components.getParams()</a></span></div>
<b>Usage:</b><br />
<br />
Let's suppose that we have the following page:<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><f:metadata><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> <f:viewParam
name="playernameparam"
value="#{playersBean.playerName}"/> <o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> <f:viewParam
name="playersurnameparam"
value="#{playersBean.playerSurname}"/> <o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></f:metadata><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><h:head><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <title></title><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></h:head> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><h:body> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:panelGrid columns="2"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:form id="form"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:commandButton id="submit"
value="Get all params"
action="#{playersBean.allParams()}"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">
<f:param name="ageParam" value="29"/><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">
<o:param name="playParam" value="left"/><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:commandButton><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:form> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:panelGrid><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></h:body><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Further, we can use the <span style="font-family: "courier new"; font-size: 9.0pt;">Components#getParams()</span>
utility to programmatically collect the <span style="font-family: "courier new"; font-size: 9.0pt;">UIParameter</span>s
(<span style="font-family: "courier new"; font-size: 9.0pt;"><f|o:param/></span>)
nested in the above <span style="font-family: "courier new"; font-size: 9.0pt;"><h:commandButton/>
</span>and the view parameters. This can be accomplish in different JSF
artifacts; for example, in a bean (obviously, there are several ways to
identify the parent <span style="font-family: "courier new"; font-size: 9.0pt;">UIComponent</span>
of the <span style="font-family: "courier new"; font-size: 9.0pt;">UIParameter</span>s
that we want to collect - the <span style="font-family: "courier new"; font-size: 9.0pt;">UIComponent</span>
that should be passed to the <span style="font-family: "courier new"; font-size: 9.0pt;">#getParams()</span>
should be obtained in a convenient manner depending on your circumstances;
below, we simply used the <span style="font-family: "courier new"; font-size: 9.0pt;"><a href="http://www.omnifaces-fans.org/2015/06/omnifaces-utilities-20-get-ui-component.html">Components#findComponent()</a></span>
utility):</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">import
org.omnifaces.util.Components;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">...<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public void
allParams() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> UIComponent uc = Components.findComponent("form:submit");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> Map<String, List<String>>
paramsMap = Components.getParams(uc, false, true);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> for (Map.Entry<String,
List<String>> entry : paramsMap.entrySet()) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> System.out.println("Param: "+
entry.getKey() + "/" + entry.getValue());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Let's suppose that we start the application with an URL like this:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">http://<i>myapp</i>/?playernameparam=rafael&playersurnameparam=nadal<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
After we press the <span style="font-family: "courier new"; font-size: 9.0pt;">Get
all params</span> button the server log will reveal the following output:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">Param:
playernameparam/[rafael]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">Param:
playersurnameparam/[nadal]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">Param:
ageParam/[29]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">Param:
playParam/[left]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><a href="http://omnifaces.org/docs/javadoc/2.4/">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/master/src/main/java/org/omnifaces/util/Components.java">GH</a><o:p></o:p></span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-13605171269050364272016-07-09T04:52:00.000-07:002016-07-09T08:28:43.117-07:00[OmniFaces utilities (2.4)] Get the HTTP request URL with query string, regardless of any forward<a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-get-http-request_9.html" id="servlets_2a" name="servlets_2a"></a>
<hr />
[OmniFaces utilities] The <code>getRequestURL()</code> method returns the HTTP request URL with query string, regardless of any forward. This is the full request URL without query string as the enduser sees in browser address bar.<br />
<hr />
<div class="separator" style="clear: both; text-align: left;">
<b>Method:</b></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNL4lWM1J_tvp4hcROie4OkwzxvZ_uH2JdvLwChyphenhyphen2tNS2FVa9zqNo545GTVsrc7TFbPSy3VuYPjDJWA4OpkjyrFFgw9t8r7Gn0MCOn7_KJEPKc1pkH2nnSgdMf9xNM9c0KT0-B1eV3xkjS/s1600/omnifaces+servlets+822ef.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="67" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNL4lWM1J_tvp4hcROie4OkwzxvZ_uH2JdvLwChyphenhyphen2tNS2FVa9zqNo545GTVsrc7TFbPSy3VuYPjDJWA4OpkjyrFFgw9t8r7Gn0MCOn7_KJEPKc1pkH2nnSgdMf9xNM9c0KT0-B1eV3xkjS/s400/omnifaces+servlets+822ef.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
See also: <span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 12px; line-height: 13.8px;"><a href="http://www.omnifaces-fans.org/2015/06/omnifaces-utilities-20-returns-http.html">Faces.getRequestDomainURL()</a> | <a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-get-http-request_4.html">Faces.getRequestURI()</a></span></div>
<br />
<b>Usage:</b><br />
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">import org.omnifaces.util.Servlets;<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">...<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@WebServlet("/MyServlet")<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class MyServlet extends HttpServlet {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; margin-bottom: 0in;">
<span style="color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> // e.g. h</span><span style="background-color: transparent; font-size: 12px; line-height: 13.8px;"><span style="color: #666666; font-family: "courier new";">ttp://localhost:8080/MyApp/MyServlet</span></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> String req = Servlets.getRequestURL(request);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> ...</span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}</span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><a href="http://omnifaces.org/docs/javadoc/2.4/">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/2.4/src/main/java/org/omnifaces/util/Servlets.java">GH</a></span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-80406195478603196672016-07-06T06:00:00.000-07:002016-07-17T03:31:30.189-07:00[OmniFaces utilities (2.4)] Get the HTTP request query string, regardless of any forward<a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-get-http-request_6.html" id="servlets_1_255" name="servlets_1_255"></a>
<hr />
[OmniFaces utilities] The <code>getRequestQueryString()</code> method returns the HTTP request query string, regardless of any forward.<br />
<hr />
<b>Method:</b><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<table align="center" style="text-align: center;"><tbody>
<tr><td><b>Use this one outside JSF context</b><br />
<b style="background-color: white; color: #666666; font-family: "trebuchet ms", trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;">(</b><b style="background-color: white; color: #666666; font-family: "courier new"; font-size: 12px;"><span style="color: #c00000;">Servlets#getRequestQueryString()</span></b><b style="background-color: white; color: #666666; font-family: "trebuchet ms", trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;">)</b><br />
<b style="background-color: white; color: #666666; font-family: "trebuchet ms", trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;"><span style="font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; font-weight: normal; line-height: 18.48px;">See also: </span><a href="http://www.omnifaces-fans.org/2015/04/omnifaces-utilities-20-get-first-non.html" style="color: #7b9fcf; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; font-weight: normal; line-height: 18.48px; text-decoration: none;"><span style="color: #666666; font-family: "courier new"; font-size: 12px; line-height: 13.8px;">Utils.coalesce()</span></a></b></td><td><b>Use this one in JSF context</b><br />
<b style="background-color: white; color: #666666; font-family: "trebuchet ms", trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;">(</b><b style="background-color: white; color: #666666; font-family: "courier new"; font-size: 12px;"><span style="color: #c00000;">Faces#getRequestQueryString()</span></b><b style="background-color: white; color: #666666; font-family: "trebuchet ms", trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;">)</b><br />
<b style="background-color: white; color: #666666; font-family: "trebuchet ms", trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;"><span style="font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; font-weight: normal; line-height: 18.48px;">See also: </span><span style="font-family: "courier new"; font-size: 12px; font-weight: normal; line-height: 13.8px;"><a href="http://www.omnifaces-fans.org/2015/03/omnifaces-utilities-20-get-http-servlet.html" style="color: #7b9fcf; text-decoration: none;">Faces.getRequest()</a> | <a href="http://www.omnifaces-fans.org/2015/03/omni-util-shortcuts-for-obtaining.html">Faces.getContext()</a></span></b></td></tr>
<tr><td><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdxlRq7V5BInzl71FIeimBlQEfOKC9ZB15HOEoItXW4ATcANThAsfU8fxEdPP_9Rynu8redHjbRgbIw0QQWmjaYMwvnFgVk5FaBmaarigZ7-MztjvoR1YWq99MEcHY-icKUXr7oaHK7awe/s1600/omnifaces+servlets+1+25.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="35" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdxlRq7V5BInzl71FIeimBlQEfOKC9ZB15HOEoItXW4ATcANThAsfU8fxEdPP_9Rynu8redHjbRgbIw0QQWmjaYMwvnFgVk5FaBmaarigZ7-MztjvoR1YWq99MEcHY-icKUXr7oaHK7awe/s320/omnifaces+servlets+1+25.png" width="320" /></a></div>
</td><td><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdWqXXDrpMQFBbl_kIh8DRGszD5rSVMcFuFogpCfb_nHQOCS7JIRu0ERcF6qI7_flKG8HdB5CL8SW6DdWJ-JwXlOnpb8Xu8Y7Ggc4YRFRmJlyT0dpWQuh0pLi9j_LGvyL9x2TzvU0JyfQH/s1600/omnifaces+faces+60+24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="121" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdWqXXDrpMQFBbl_kIh8DRGszD5rSVMcFuFogpCfb_nHQOCS7JIRu0ERcF6qI7_flKG8HdB5CL8SW6DdWJ-JwXlOnpb8Xu8Y7Ggc4YRFRmJlyT0dpWQuh0pLi9j_LGvyL9x2TzvU0JyfQH/s400/omnifaces+faces+60+24.png" width="400" /></a></div>
</td></tr>
</tbody></table>
<b>Usage:</b><br />
<b><br /></b>
<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">Some use cases are available right in OmniFaces source code. For example, OmniFaces uses </span><span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">getRequestQueryString() </span><span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">to </span><span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">get</span><span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;"> </span><a href="http://www.omnifaces-fans.org/2015/06/omnifaces-utilities-20-get-http-request_96.html" style="color: #7b9fcf; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; text-decoration: none;">the HTTP request URI with query string, regardless of any forward</a><span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">:</span><br />
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">public static String getRequestURIWithQueryString(HttpServletRequest request) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> String requestURI = getRequestURI(request);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> String queryString = </span><b style="font-family: "courier new"; font-size: 12px; line-height: 18.48px;"><span style="color: #c00000;">getRequestQueryString</span></b><span style="font-family: "courier new"; font-size: 9pt;">(request);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> return (queryString == null) ? requestURI : (requestURI + "?" + queryString);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">}</span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
<a href="http://omnifaces.org/docs/javadoc/2.4/" style="color: #7b9fcf; font-family: "courier new"; font-size: 12px; line-height: 16.8px; text-decoration: none;">API</a><span style="font-family: "courier new"; font-size: 12px; line-height: 16.8px;"> </span><a href="https://github.com/omnifaces/omnifaces/blob/2.4/src/main/java/org/omnifaces/util/Servlets.java" style="color: #7b9fcf; font-family: "courier new"; font-size: 12px; line-height: 16.8px; text-decoration: none;">GH</a></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-80544227258714619502016-07-04T05:29:00.001-07:002016-07-06T06:09:08.567-07:00[OmniFaces utilities (2.4)] Get the HTTP request URI, regardless of any forward or error dispatch<a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-get-http-request_4.html" id="servlets_1_24" name="servlets_1_24"></a>
<hr />
[OmniFaces utilities] The <code>getRequestURI()</code> method returns the HTTP request URI, regardless of any forward or error dispatch. This is the part after the domain in the request URL, including the leading slash.<br />
<hr />
<b>Method:</b><br />
<table align="center" style="text-align: center;"><tbody>
<tr><td><b>Use this one in JSF context</b><br />
<b>(</b><b style="font-family: "courier new"; font-size: 12px; text-align: start;"><span style="color: #c00000;">Faces#getRequestURI()</span></b><b>)</b><br />
See also: <span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 12px; line-height: 13.8px;"><a href="http://www.omnifaces-fans.org/2015/03/omnifaces-utilities-20-get-http-servlet.html">Faces.getRequest()</a></span> <b style="background-color: white; color: #666666; font-family: "trebuchet ms", trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;"><span style="font-family: "courier new"; font-size: 12px; font-weight: normal; line-height: 13.8px;">| <a href="http://www.omnifaces-fans.org/2015/03/omni-util-shortcuts-for-obtaining.html">Faces.getContext()</a></span></b></td><td><b>Use this one outside JSF context</b><br />
<b>(</b><b style="font-family: "courier new"; font-size: 12px; text-align: start;"><span style="color: #c00000;">Servlets#getRequestURI()</span></b><b>)</b><br />
See also: <a href="http://www.omnifaces-fans.org/2015/04/omnifaces-utilities-20-get-first-non.html"><span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 12px; line-height: 13.8px;">Utils.coalesce()</span></a></td></tr>
<tr><td><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLSOiun8CrkNKjmNpsPOD9eFv1mOyACFWDoUadd6Wh0FsLhdBHGo_jOd_DOBspZGz0kD9ou1Bvie7j-p8glPAVXCAgy5QNrKF2sSqQlrw5cMxUrXkcJD7x8drtqT6b0BByNUQRP796k48r/s1600/omnifaces+faces+1+24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="130" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLSOiun8CrkNKjmNpsPOD9eFv1mOyACFWDoUadd6Wh0FsLhdBHGo_jOd_DOBspZGz0kD9ou1Bvie7j-p8glPAVXCAgy5QNrKF2sSqQlrw5cMxUrXkcJD7x8drtqT6b0BByNUQRP796k48r/s400/omnifaces+faces+1+24.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
</td><td><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0N17RpNuMUT03CvJIhfoibJ3R-4PyA2cUax47UCDOvwtR4LE1lvgbFA8A-FvvmHO2CgnSIlK-S_qSRFJpZmAbpisZIMdES_vDmKtXCb4Rz3fzI-QzRfSrLPdI-x8akjQ1j8DPHhW5TBk7/s1600/omnifaces+servlets+1+24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="37" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0N17RpNuMUT03CvJIhfoibJ3R-4PyA2cUax47UCDOvwtR4LE1lvgbFA8A-FvvmHO2CgnSIlK-S_qSRFJpZmAbpisZIMdES_vDmKtXCb4Rz3fzI-QzRfSrLPdI-x8akjQ1j8DPHhW5TBk7/s400/omnifaces+servlets+1+24.png" width="400" /></a></div>
</td></tr>
</tbody></table>
<div class="separator" style="clear: both; text-align: center;">
</div>
<b>Usage:</b><br />
<b><br /></b>
Some use cases are available right in OmniFaces source code. For example, OmniFaces uses <span style="font-family: "courier new"; font-size: 9pt; line-height: 115%;">getRequestURI() </span>to <a href="http://www.omnifaces-fans.org/2015/06/omnifaces-utilities-20return-true-if.html">check if the given HTTP servlet request is a JSF resource request</a>:<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public static
boolean isFacesResourceRequest(HttpServletRequest request) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return <b><span style="color: #c00000;">getRequestURI</span></b>(request).startsWith(request.getContextPath()
+ ResourceHandler.RESOURCE_IDENTIFIER + "/");</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Or to get <a href="http://www.omnifaces-fans.org/2015/06/omnifaces-utilities-20-get-http-request_96.html">the HTTP request URI with query string, regardless of any forward</a>:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public static
String getRequestURIWithQueryString(HttpServletRequest request) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> String requestURI = <b><span style="color: #c00000;">getRequestURI</span></b>(request);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> String queryString =
getRequestQueryString(request);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return (queryString == null) ? requestURI :
(requestURI + "?" + queryString);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><a href="http://omnifaces.org/docs/javadoc/2.4/">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/2.4/src/main/java/org/omnifaces/util/Servlets.java">GH</a></span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-37153973943615159272016-07-03T05:10:00.001-07:002016-07-04T05:33:27.375-07:00[OmniFaces utilities (2.4)] Get a list of all action expressions and listeners associated with given component<a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-get-list-of-all.html" id="components_35_24" name="components_35_24"></a>
<hr />
[OmniFaces utilities] The <code>getActionExpressionsAndListeners()</code> method returns a list of all action expressions and listeners associated with given component. This covers expressions in <code>action</code> attribute of command components and <code>listener</code> attribute of ajax components. Any method expressions are in format <code>#{bean.method}</code> and any action listeners are added as fully qualified class names. This list is primarily useful for logging postback actions in a phase listener. You can use <code>#getCurrentActionSource()</code> to obtain the current action source.<br />
<hr />
<b>Method:</b><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9lLCg7q8V4aRz2Hr_J4VHcfK-c5tfd7e6Cj0LtuKUZhLNhO-Bt2Q_NgIcBH6Y4ig-qVegcoFsCRJSa_okaAtmcW9kBNqt81y79FxdLeir_JMHTMj3ip0_dOaTrLIVye02v1SQTe0NQ5IH/s1600/omnifaces+components+33+24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9lLCg7q8V4aRz2Hr_J4VHcfK-c5tfd7e6Cj0LtuKUZhLNhO-Bt2Q_NgIcBH6Y4ig-qVegcoFsCRJSa_okaAtmcW9kBNqt81y79FxdLeir_JMHTMj3ip0_dOaTrLIVye02v1SQTe0NQ5IH/s640/omnifaces+components+33+24.png" width="624" /></a></div>
<b><br /></b>
<b>Usage:</b><br />
<br />
Let's
suppose the following code:<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:commandButton value="Foo" <b><span style="color: #c00000;">actionListener</span></b>="#{fooBean.fooActionListener()}"
<b><span style="color: #c00000;">action</span></b>="#{fooBean.fooAction()}"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:<b><span style="color: #c00000;">setPropertyActionListener</span></b> id="foo"
target="#{fooBean.foo}" value="Foo"/> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:ajax execute="@form"
render="@form" <b><span style="color: #c00000;">listener</span></b>="#{fooBean.ajaxFooListener()}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:commandButton><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now,
programmatically (for example in a phase listener) we can do this:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">import
org.omnifaces.util.Components;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">...<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">// use <a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-get-source-of.html">getCurrentActionSource()</a>
to get the source of the currently invoked action<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">UIComponent
command = Components.getCurrentActionSource();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">List<String>
ael = <b><span style="color: #c00000;">Components.getActionExpressionsAndListeners(command);</span></b><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">// or, use <a href="http://www.omnifaces-fans.org/2015/02/omni-util-get-currently-invoked-ui.html">getCurrentCommand()</a>
to get the currently invoked UI command component<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">UIComponent
command = Components.getCurrentCommand();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">List<String>
ael = <b><span style="color: #c00000;">Components.getActionExpressionsAndListeners(command);<o:p></o:p></span></b></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Both
examples will return a <span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">List<String></span><span style="font-family: "calibri" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"> </span> that contains the following items:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">[<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> #{fooBean.fooAction()}, <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> javax.faces.event.MethodExpressionActionListener,
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> com.sun.faces.facelets.tag.jsf.core.SetPropertyActionListenerHandler$SetPropertyListener,
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> #{fooBean.ajaxFooListener()}</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">]</span></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEdpUwwA1qsv7mWsBT-UvEmTGcTuGNFrwLUQDbl36K5q7aOhvAxgYkl8e4UvVGTwcWY7tGUAUlxwl9EXI9-D00Eh0vXnUVdIqBa3ryzr2M48AsdwpmLrlxZ2dzqNcJWdIuA1xqhvQuiqoe/s1600/OmniFaces++getActionExpressionsAndListeners.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEdpUwwA1qsv7mWsBT-UvEmTGcTuGNFrwLUQDbl36K5q7aOhvAxgYkl8e4UvVGTwcWY7tGUAUlxwl9EXI9-D00Eh0vXnUVdIqBa3ryzr2M48AsdwpmLrlxZ2dzqNcJWdIuA1xqhvQuiqoe/s1600/OmniFaces++getActionExpressionsAndListeners.png" /></a></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<a href="http://omnifaces.org/docs/javadoc/2.4/">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/2.4/src/main/java/org/omnifaces/util/Components.java">GH</a></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-1688043310667592102016-07-03T03:47:00.001-07:002016-07-03T03:51:50.725-07:00[OmniFaces utilities (2.4)] Get the source of the currently invoked action<a href="http://www.omnifaces-fans.org/2016/07/omnifaces-utilities-24-get-source-of.html" id="components_34_24" name="components_34_24"></a>
<hr />
[OmniFaces utilities] The <code>getCurrentActionSource()</code> method returns the source of the currently invoked action, or <code>null</code> if there is none, which may happen when the current request is not a postback request at all, or when the view has been changed by for example a successful navigation. If the latter is the case, you'd better invoke this method before navigation.<br />
<hr />
<b>Method:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMeZyb-vo1MqLM3_37uU0VPOr46cM8wTAJ-JRSHsyOdyqdtZYdXkmYLR-bFlFmTHMr3xzTcSqA0_XzdIeMrePG-_e3SX1O_ApTdTUW2avMgghM7H_SZ6WP_yh0ByUJUUNnGLeHToD1IGRq/s1600/omnifaces+components+34+24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="397" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMeZyb-vo1MqLM3_37uU0VPOr46cM8wTAJ-JRSHsyOdyqdtZYdXkmYLR-bFlFmTHMr3xzTcSqA0_XzdIeMrePG-_e3SX1O_ApTdTUW2avMgghM7H_SZ6WP_yh0ByUJUUNnGLeHToD1IGRq/s400/omnifaces+components+34+24.png" width="400" /></a></div>
<b>Usage:</b><br />
<span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span>
<span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">import org.omnifaces.util.Components;</span><br />
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">...<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; margin-bottom: 0in;">
<span style="color: #666666; font-family: "courier new";"><span style="font-size: 9pt; line-height: 13.8px;">UIComponent action = Components.</span><span style="font-size: 12px; line-height: 13.8px;">getCurrentActionSource</span><span style="font-size: 9pt; line-height: 13.8px;">();</span></span></div>
<div class="MsoNormal" style="background-color: white; margin-bottom: 0in;">
<span style="color: #666666; font-family: "courier new";"><span style="font-size: 9pt; line-height: 13.8px;"><br /></span></span></div>
<div class="MsoNormal" style="background-color: white; margin-bottom: 0in;">
You can see several examples of using this method as:</div>
<div class="MsoNormal" style="background-color: white; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; margin-bottom: 0in;">
- check whether the given component has invoked the form submit (<span style="font-family: "courier new"; font-size: 12px;"><a href="http://www.omnifaces-fans.org/2015/02/omni-util-get-whether-given-component.html#components_15">hasInvokedSubmit()</a></span>):</div>
<div class="MsoNormal" style="background-color: white; margin-bottom: 0in;">
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public static
boolean hasInvokedSubmit(UIComponent component) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> UIComponent source = getCurrentActionSource();<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return source != null &&
source.equals(component);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
- get the currently invoked UI command component (<span style="font-family: "courier new"; font-size: 12px; line-height: 13.8px;"><a href="http://www.omnifaces-fans.org/2015/02/omni-util-get-currently-invoked-ui.html#components_9">getCurrentCommand()</a></span>):</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">public
static UICommand getCurrentCommand() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"> UIComponent source = getCurrentActionSource();<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"> return source instanceof UICommand ?
(UICommand) source : null;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">}</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><a href="http://omnifaces.org/docs/javadoc/2.4/">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/2.4/src/main/java/org/omnifaces/util/Components.java">GH</a></span></div>
</div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-73476269890049702632016-07-02T07:50:00.001-07:002016-07-02T07:50:41.190-07:00OmniFaces 2.4 is released!<div class="separator" style="clear: both; text-align: center;">
<a href="http://balusc.omnifaces.org/2016/07/omnifaces-24-revives-curl-improves.html"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZu4f7rdiNdFD7U576XGt-qSkNvR-wbOaWzaOKdvRmaTgyaAC_mK1y0vL6Y-OR6Zi4iVqKchoM8_SI_jsuHOuPd2U6ldLNj7N-0UOb_jPb3lpVl4LIIG-Dq8LwEyzRkOwTEeVJankws-qh/s1600/omnifaces+2.4.png" /></a></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-68123021011270600072016-06-28T08:37:00.001-07:002016-06-28T08:42:19.675-07:00CDI-JSF: Beans discovery modes<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
When we write applications that involves CDI we need to activate CDI by
adding in our project a file named <span style="font-family: "courier new"; font-size: 9.0pt;">beans.xml</span>. Usually, this file will look like below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><?xml
version="1.0" encoding="UTF-8"?><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <b><span style="color: #c00000;">bean-discovery-mode="all"</span></b>> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></beans><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
In this post, we are interested in the part highlighted in red. If you
never notice that part before, then this quick post is for you. Let's see what
is happening when we have <b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">bean-discovery-mode="all"</span></b>,
and for this let's consider a simple interface with two implementations, as
follows:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public
interface IFoo {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void fooSlaveAction();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooImplAnnotated implements IFoo {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final Logger LOG =
Logger.getLogger(FooImplAnnotated.class.getName()); <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void fooSlaveAction() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
LOG.info("FooImplAnnotated#fooSlaveAction() invoked ...");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> } <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooImplNoAnnotation implements IFoo {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final Logger LOG =
Logger.getLogger(FooImplNoAnnotation.class.getName()); <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void fooSlaveAction() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
LOG.info("FooImplNoAnnotation#fooSlaveAction() was invoked
...");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> } <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
As you can see, the main difference between these two implementations
consist in the fact that <span style="font-family: "courier new"; font-size: 9.0pt;">FooImplAnnotated
</span>is annotated with a CDI annotation, <span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped</span>. Well, now let's inject these
two implementation in a third CDI bean:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> // inject the annotated bean<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private FooImplAnnotated fooAnnotated; <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> // inject the no-annotation bean<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private FooImplNoAnnotation fooNoAnnotation;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void fooMasterAction(){<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> // call fooSlaveAction() of the annotated
bean<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> fooAnnotated.fooSlaveAction();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> // call fooSlaveAction() of the no annotation
bean<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> fooNoAnnotation.fooSlaveAction();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> } <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
If we test this application (for example by calling the <span style="font-family: "courier new"; font-size: 12px;">fooMasterAction() </span>method via a simple EL as <span style="font-family: "courier new"; font-size: 12px;">#{fooBean.</span><span style="font-family: "courier new"; font-size: 12px;">fooMasterAction()</span><span style="font-family: "courier new"; font-size: 12px;">}</span>), we will get the following messages in the server log:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">FooImplAnnotated#fooSlaveAction()
invoked ...<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">FooImplNoAnnotation#fooSlaveAction()
was invoked ...<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Ok, this is the effect of using <b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">bean-discovery-mode="all"</span></b>
who tells CDI to discover all beans. No, let's alter the <span style="font-family: "courier new"; font-size: 9.0pt;">beans.xml</span> as switch
the value of <span style="font-family: "courier new"; font-size: 9.0pt;">bean-discovery-mode
</span>from<span style="font-family: "courier new"; font-size: 9.0pt;"> all </span>to
<span style="font-family: "courier new"; font-size: 9.0pt;">annotated</span>:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><?xml
version="1.0" encoding="UTF-8"?><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <b><span style="color: #c00000;">bean-discovery-mode="annotated"</span></b>> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></beans><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
This time the test will fail with an error of type <span style="font-family: "courier new"; font-size: 9.0pt;">org.jboss.weld.exceptions.DeploymentException:
WELD-001408: Unsatisfied dependencies for type FooImplNoAnnotation with
qualifiers @Default</span>. Well, the problem is that the <span style="font-family: "courier new"; font-size: 9.0pt;">FooImplNoAnnotation </span>is
not discoverable and it cannot be injected in <span style="font-family: "courier new"; font-size: 9.0pt;">FooBean</span>. Since we are looking only for
annotated implementation, we can re-write our <span style="font-family: "courier new"; font-size: 9.0pt;">FooBean</span> like below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> // inject the annotated bean<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private IFoo fooAnnotated;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void fooMasterAction() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> // call
fooSlaveAction() of the annotated bean<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> fooAnnotated.fooSlaveAction();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now, the test will pass again, and the server log will reveal this
message: <span style="font-family: "courier new"; font-size: 9.0pt;">FooImplAnnotated#fooSlaveAction()
invoked ...</span>Since the <span style="font-family: "courier new"; font-size: 9.0pt;">FooImplNoAnnotation
</span>is not discoverable, CDI has choose
the single available implementation, <span style="font-family: "courier new"; font-size: 9.0pt;">FooImplAnnotated.</span> Of course, if we make the <span style="font-family: "courier new"; font-size: 9.0pt;">FooImplNoAnnotation </span>discoverable
by adding a CDI annotation to it, then CDI will cause <a href="http://www.omnifaces-fans.org/2016/06/cdi-jsf-avoid-common-mistakes-that.html">ambiguous
dependencies errors</a>.</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Ok, finally, let's switch the value of <span style="font-family: "courier new"; font-size: 9.0pt;">bean-discovery-mode </span>from <span style="font-family: "courier new"; font-size: 9.0pt;">annotated</span> to <span style="font-family: "courier new"; font-size: 9.0pt;">none</span>:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><?xml
version="1.0" encoding="UTF-8"?><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <b><span style="color: #c00000;">bean-discovery-mode="none"</span></b>> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></beans><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
This time the test will not cause any error and no messages will be
available in the server log. We just told CDI to not try to discover any bean.</div>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The complete example is available <a href="https://github.com/AnghelLeonard/CDI-JSF/tree/master/BeansDiscovery">here</a>.<span style="font-family: "courier new"; font-size: 9.0pt;"><o:p></o:p></span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-59150410059738399232016-06-28T05:24:00.005-07:002016-06-28T05:33:09.777-07:00[OmniFaces utilities (2.4)] Create a copy of the array with items in reversed order<a href="http://www.omnifaces-fans.org/2016/06/omnifaces-utilities-24-create-copy-of.html" id="arrays_124" name="arrays_124"></a>
<hr />
[OmniFaces utilities] The <code>reverseArray()</code> function returns a copy of the array with items in reversed order.<br />
<hr />
<b>Function:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_4NMDXnG5zae1-Nf4MwYB7NfGlt1d9RxOlgipUjOGgKrZTGHwqkvxTroOarxHBJKJ9OMLopNA7ZCMAOfJNmbnR-6DtoJXUuvdLtZq8VC_SlHn5xC5R1Um01rI9rsOr_oijSGfQFG29YDf/s1600/omnifaces+functions+124.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_4NMDXnG5zae1-Nf4MwYB7NfGlt1d9RxOlgipUjOGgKrZTGHwqkvxTroOarxHBJKJ9OMLopNA7ZCMAOfJNmbnR-6DtoJXUuvdLtZq8VC_SlHn5xC5R1Um01rI9rsOr_oijSGfQFG29YDf/s1600/omnifaces+functions+124.png" /></a></div>
<b>Usage:</b><br />
<b><br /></b>
Let's suppose that we have the following simple array defined in a bean:<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
MonthsBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> private String[] months = new String[]
{"January", "February", "March",
"April", "May", "June", "July", </span></b><br />
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> "August", "September", "October",
"November", "December"};</span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public String[] getMonths() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return
months;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> } <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now, we can display the array as it was defined and in reverse order as below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">// as it was
defined<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><ui:repeat
value="#{monthsBean.months}" var="item"
varStatus="loop"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"> #{loop.index + 1} : #{item} #{!loop.last ? ',
' : ''}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"></ui:repeat><o:p></o:p></span><br />
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><br /></span>
Output:<br />
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">1 : January , 2 : February , 3 : March , 4 : April , 5 : May , 6
: June , </span><br />
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">7 : July , 8 : August , 9 : September , 10 : October , 11 : November ,
12 : December</span></div>
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">// reversed
(xmlns:of="http://omnifaces.org/functions")<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><ui:repeat
value="<b><span style="color: #c00000;">#{of:reverseArray(monthsBean.months)}</span></b>"
var="item" varStatus="loop"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"> #{loop.index + 1} : #{item} #{!loop.last ? ',
' : ''}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"></ui:repeat></span><br />
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><br /></span>Output:<br />
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">1 : December , 2 : November , 3 : October , 4 : September , 5 :
August , 6 : July , </span><br />
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">7 : June , 8 : May , 9 : April , 10 : March , 11 : February
, 12 : January</span></div>
</div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><a href="http://omnifaces.org/docs/javadoc/2.3/">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/d43dac025cd1144c17d7e0e3d60f55335d7ef457/src/main/java/org/omnifaces/el/functions/Arrays.java">GH</a></span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-59677000145370280762016-06-28T02:09:00.001-07:002016-06-28T02:14:01.518-07:00JSF 2.3 - Import constants/enums<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Let's checkout a common practice for declaring constants in Java using the <span style="font-family: "courier new"; font-size: 9.0pt;">public static final</span> declaration:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
Circle {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final float PI = 3.14f;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final String UNIT = "
radians";<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> ...<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Also, Java interfaces and enums are very useful in applications (they
are part of the Java language <span style="font-family: "cambria math" , "serif"; mso-bidi-font-family: "Cambria Math";">ʺ</span>bricks<span style="font-family: "cambria math" , "serif"; mso-bidi-font-family: "Cambria Math";">ʺ</span>):<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public
interface Car {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public String MODEL = "Logan";<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public String COUNTRY = "RO";<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public enum
Cars {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> CITROEN, LOGAN, BMW;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now, letʹs suppose that we have these artifacts in a JSF application,
as a JSF page author we need to use them in page via EL, as below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin-bottom: 0.0001pt; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;">
</span></span><!--[endif]-->Use constants:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">#{Circle.PI}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">#{Circle.UNIT}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin-bottom: 0.0001pt; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;">
</span></span><!--[endif]-->Use interfaces and enums:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">#{Car.MODEL}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
There is no elegant way to say that the above usages will simply not
work!</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
So, now let's consider the following example. First, we define an enum:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public enum
PlayerEnum {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> FIRST, SECOND, THIRD;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public Integer getRank() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> switch (name()) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
case "FIRST":<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> case "SECOND":<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> case "THIRD":<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return 3;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> default:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Further, we use this enum in a CDI managed bean:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
PlayerBean implements Serializable {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final long serialVersionUID =
1L;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private PlayerEnum selectedPlayerEnum;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public static final String NAME_C =
"Rafael Nadal";<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public PlayerEnum getSelectedPlayerEnum() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return selectedPlayerEnum;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void setSelectedPlayerEnum(PlayerEnum
selectedPlayerEnum) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> this.selectedPlayerEnum = selectedPlayerEnum;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Well, OmniFaces comes with a tag handler named, <span style="font-family: "courier new"; font-size: 9.0pt;"><a href="http://showcase.omnifaces.org/taghandlers/importConstants"><o:importConstants/></a></span>
that is capable to map of all constant field values of the given fully qualified
name of a type in the request scope (it works with enums also). This tag
handler is detailed in the book <b><span style="font-family: "agency fb" , "sans-serif"; font-size: 14.0pt;"><a href="https://www.amazon.com/Mastering-OmniFaces-Anghel-Leonard/dp/1908689293?ie=UTF8&ref_=asap_bc">Mastering
OmniFaces</a></span></b>. PrimeFaces also comes with support for import
constants and enums. Before <a href="http://blog.primefaces.org/?p=3798">PrimeFaces
Elite 5.3.8</a> this support was available in PrimeFaces Extension as <span style="font-family: "courier new"; font-size: 9.0pt;"><pe:importConstants/></span>
and <span style="font-family: "courier new"; font-size: 9.0pt;"><pe:importEnum/></span>.
Afterwards, this support was moved from
PrimeFaces extension to <a href="http://blog.primefaces.org/?p=3798">PrimeFaces
Elite 5.3.8</a>, and is available via <span style="font-family: "courier new"; font-size: 9.0pt;"><p:importConstants/></span> and <span style="font-family: "courier new"; font-size: 9.0pt;"><p:importEnum/></span>. This tags are
detailed in book, <b><span style="font-family: "agency fb" , "sans-serif"; font-size: 14.0pt;"><a href="https://leanpub.com/PrimeFaces-OmniFaces-Powers-Combined">PrimeFaces
& OmniFaces - Powers Combined</a></span></b>.</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Starting with <a href="https://twitter.com/OmniFaces/status/747413049909350400">JSF 2.3-m07</a>,
we can import constants and enums via the
new <span style="font-family: "courier new"; font-size: 9.0pt;"><a href="https://github.com/jsf-spec/mojarra"><f:importConstants/></a></span>
tag. Check the <span style="font-family: "courier new"; font-size: 9.0pt;">NAME_C</span>
constant from above. Well, this constant (and any other constant from <span style="font-family: "courier new"; font-size: 9.0pt;">PlayerBean</span>) can be
accessed via EL once we import them as below (the <span style="font-family: "courier new"; font-size: 9.0pt;">type</span> attribute is required and its value
represents the fully qualified name of the class/interface/enum to import the
constant field values for):</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"><f:importConstants
type="javaee8.jsf23.PlayerBean" /><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The <span style="font-family: "courier new"; font-size: 9.0pt;"><f:importConstants/></span>
supports the <span style="font-family: "courier new"; font-size: 9.0pt;">var</span>
attribute also. You can use it to indicate an alias (the name of the request
attribute which exposes the mapping of the constants in the request scope), as </div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"><f:importConstants
type="javaee8.jsf23.PlayerBean" var="CONSTANTS" /><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now, we can write this:<span style="font-family: "courier new"; font-size: 9.0pt;">
<b><span style="color: #c00000;">#{CONSTANTS.NAME_C}</span></b><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The <span style="font-family: "courier new"; font-size: 9.0pt;">PlayerEnum</span>
can be imported exactly the same:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"><f:importConstants
type="javaee8.jsf23.PlayerEnum" /><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
This allows us to loop the enum, as below (returns <b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">FIRST</span></b>
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">SECOND</span></b> <b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">THIRD</span></b>):</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><ui:repeat
value="#{<b><span style="color: #c00000;">PlayerEnum.values()</span></b>}"
var="t"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> #{t}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></ui:repeat><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now, we can nominate the enum constants:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">#{PlayerEnum.FIRST}
// returns FIRST<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">#{PlayerEnum.FIRST.rank}
// returns 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
We can put all together and provide enum values as dropdown items:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:selectOneMenu
value="#{playerBean.selectedPlayerEnum}"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:selectItems
value="#{<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">PlayerEnum</span></b>}" /><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:ajax render="@form" /><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:selectOneMenu><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> #{playerBean.selectedPlayerEnum}</span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> [#{playerBean.selectedPlayerEnum ==
PlayerEnum.FIRST}]<o:p></o:p></span></b></div>
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Even more, we can provide the enum values as dropdown items via the <span style="font-family: "courier new"; font-size: 9.0pt;">var</span> attribute:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:selectOneMenu
value="#{playerBean.selectedPlayerEnum}"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:selectItems
value="#{<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">PlayerEnum.values()</span></b>}" var="t"
itemLabel="#{t.rank}" itemValue="#{t}" /><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:ajax render="@form" /><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:selectOneMenu><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </span><b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">#{playerBean.selectedPlayerEnum}</span></b><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> [#{playerBean.selectedPlayerEnum ==
PlayerEnum.FIRST}]<o:p></o:p></span></b></div>
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
We can glue all the examples above in the below view:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><html
xmlns="http://www.w3.org/1999/xhtml"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
xmlns:h="http://xmlns.jcp.org/jsf/html"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> xmlns:f="http://xmlns.jcp.org/jsf/core"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:metadata><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">
<f:importConstants type="javaee8.jsf23.PlayerEnum" /><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">
<f:importConstants type="javaee8.jsf23.PlayerBean" /><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">
<f:importConstants type="javaee8.jsf23.PlayerBean"
var="CONSTANTS" /><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </f:metadata><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:head><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <title>JSF 2.3 -
ImportConstants</title>
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:head><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:body> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h5>Display the 'NAME_C' constant
value</h5><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:outputText
value="#{PlayerBean.NAME_C}"/><br/><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:outputText value="#{CONSTANTS.NAME_C}"/><br/><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h5>Loop enum values</h5><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <ui:repeat
value="#{PlayerEnum.values()}" var="t"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> #{t}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </ui:repeat><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h5>Providing enum values as dropdown
items and test again 'FIRST'</h5><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
<h:selectOneMenu
value="#{playerBean.selectedPlayerEnum}"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
<f:selectItems value="#{PlayerEnum}" /><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
<f:ajax render="@form" /><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
</h:selectOneMenu><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> #{playerBean.selectedPlayerEnum}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> [#{playerBean.selectedPlayerEnum ==
PlayerEnum.FIRST}]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:form>
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h5>Providing enum values as dropdown
items by var and test again
'FIRST'</h5><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:selectOneMenu
value="#{playerBean.selectedPlayerEnum}"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:selectItems
value="#{PlayerEnum.values()}" var="t"
itemLabel="#{t.rank}" itemValue="#{t}" /><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <f:ajax render="@form" /><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:selectOneMenu><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> #{playerBean.selectedPlayerEnum}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
[#{playerBean.selectedPlayerEnum == PlayerEnum.FIRST}]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:form> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> </h:body><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></html></span></div>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The complete application is available <a href="https://github.com/AnghelLeonard/JSF-2.3/tree/master/JSF23ImportConstants">here</a>.</div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-51002467928222405242016-06-23T08:30:00.002-07:002016-07-03T03:48:28.624-07:00[OmniFaces utilities 2.4] Check if the given object is serializable<a href="http://www.omnifaces-fans.org/2016/06/omnifaces-utilities-24-check-if-given.html" id="utils_1_24" name="utils_1_24"></a>
<hr />
[OmniFaces utilities] The <code>isSerializable()</code> method returns <code>true</code> if the given object is serializable.<br />
<hr />
<b>Method:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlMOeAxSQenYog12g2CEyyLOObWEASyii36v2Qvys9LtJJxDbhG1GoaXr3oKwJ5l1WZzX5rlcLgGDcWD5lTgB8jT9I-PNLBtN3WRq16tqB5Uw-FXm8STnInsg2PqgHbUEwl_dXnTtdn20n/s1600/omnifaces+utils+1+24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="256" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlMOeAxSQenYog12g2CEyyLOObWEASyii36v2Qvys9LtJJxDbhG1GoaXr3oKwJ5l1WZzX5rlcLgGDcWD5lTgB8jT9I-PNLBtN3WRq16tqB5Uw-FXm8STnInsg2PqgHbUEwl_dXnTtdn20n/s400/omnifaces+utils+1+24.png" width="400" /></a></div>
<b>Usage:</b><br />
<br />
Example 1 - test <span style="font-family: "courier new"; font-size: 9pt;">Object:</span><br />
<span style="font-family: "courier new"; font-size: 9pt;"><br /></span>
<span style="font-family: "courier new"; font-size: 9pt;">import org.omnifaces.util.Utils;</span><br />
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9.0pt;">...</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">Utils.isSerializable(new
Object());</span></b> // return false</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Example 2 - test two classes</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
// Serializable class</div>
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">public class ShopingCart implements Serializable {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"> ... <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">}</span></div>
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
// NOT Serializable class</div>
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class Item {<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> ... <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}</span></div>
<div class="MsoNormal">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal">
<div class="MsoNormal">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">Utils.isSerializable(new ShopingCart()); </span><o:p></o:p></b><span style="font-family: "courier new"; font-size: 12px;">// return true</span></div>
<div class="MsoNormal">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt; line-height: 115%;">Utils.isSerializable(new
Item()); </span><o:p></o:p></b><span style="font-family: "courier new"; font-size: 12px;">// return false</span></div>
<div class="MsoNormal">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt; line-height: 115%;"><br /></span></b></div>
</div>
<div class="MsoNormal">
<span style="font-family: "courier new";"><span style="font-size: 12px; line-height: 13.8px;"><a href="http://omnifaces.org/docs/javadoc/2.3/">API</a> <a href="https://github.com/omnifaces/omnifaces/blob/14e7497ce581b4d5580e7d5ed3f2a1da5cd21718/src/main/java/org/omnifaces/util/Utils.java">GH</a></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><o:p></o:p></span></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-43539471957474415712016-06-23T06:04:00.000-07:002016-06-23T06:19:28.115-07:00JSF 2.3 Auto detect Converter based on 1st UISelectMany itemStarting with JSF 2.3, more exactly with <a href="https://maven.java.net/content/repositories/releases/org/glassfish/javax.faces/?utm_medium=referral&utm_campaign=ZEEF&utm_source=https%3A%2F%2Fjava-ee-8-progress-tracker.zeef.com%2Fanghel.leonard/">m07</a>, we can take advantage of using the auto detection of convertors based on 1st <span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 12px; line-height: 13.8px;">UISelectMany</span> item.<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">The story behind the issue solved in JSF 2.3 is easy to understand via two examples. </span><br />
<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;"><br /></span>
<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; line-height: 18.48px;"><b>Example 1:</b></span><br />
<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;"><br /></span>
<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">Let's suppose the following code:</span><br />
<span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span>
<span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><h:form> </span><br />
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:selectManyListbox value="#{playerBean.selectedRanks}" <b><span style="color: #c00000;">converter="javax.faces.Integer"</span></b>> <o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <f:selectItems value="#{playerBean.playersRanks}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> </h:selectManyListbox><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:commandButton value="Select" action="#{playerBean.selectedAction()}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></h:form> </span></div>
<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;"><br /></span>
<span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;">Let's start by supposing that the above built-in converter is not specified. Basically, the </span><span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">playersRanks</span><span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;"> is a list of integers that "populates" our list, and the </span><span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">selectedRanks</span><span style="background-color: white; color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif; font-size: 13.2px; line-height: 18.48px;"> represents the user selections:</span><br />
<span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span>
<span style="background-color: white; color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">private ArrayList<Integer> selectedRanks;</span><br />
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">private static final ArrayList<Integer> playersRanks;<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">static {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> playersRanks = new ArrayList<>();<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> playersRanks.add(1);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> playersRanks.add(2);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> playersRanks.add(3);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public ArrayList<Integer> getPlayersRanks() {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> return playersRanks;<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public ArrayList<Integer> getSelectedRanks() {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> return selectedRanks;<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public void setSelectedRanks(ArrayList<Integer> selectedRanks) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> this.selectedRanks = selectedRanks;<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div>
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
So, the user may select the ranks and submit them without issues/errors. Even if no error occurred, we can notice a "strange" behavior if we try to run the following snippet of code:</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><ui:repeat value="#{playerBean.selectedRanks}" var="i"><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> #{i}: <b><span style="color: #c00000;">#{i.getClass()}</span></b><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></ui:repeat><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
The output reveals that the selected ranks are strings, not integers as we expected to see:</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">1: class java.lang.String<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">3: class java.lang.String<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
The explanation relies on the fact that "<i>the generic type information of </i><span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">List<Integer></span><i> is lost during runtime and therefore JSF/EL who sees only </i><span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">List</span><i> is not able to identify that the generic type is Integer and assumes it to be default </i><span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">String</span><i> (as that's the default type of the underlying </i><span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">HttpServletRequest#getParameter()</span><i>call during apply request values phase) - BalusC</i>".</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
There are two approaches:</div>
<div class="MsoListParagraphCxSpFirst" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in; text-indent: -0.25in;">
<span style="font-family: "symbol";">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span>explicitly specify a <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Converter</span></div>
<div class="MsoListParagraphCxSpLast" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in; text-indent: -0.25in;">
<span style="font-family: "symbol";">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span>use <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Integer[]</span> instead</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
In this case, we can use the built-in <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">javax.faces.Integer</span> built-in converter. Now, we can perform the same test and the output will be:</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">1: class java.lang.Integer<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">3: class java.lang.Integer</span></div>
</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b style="font-size: medium; line-height: 18.48px;">Example 2:</b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b style="font-size: medium; line-height: 18.48px;"><br /></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-size: 13.2px; line-height: 18.48px;">This use case continue the story from the above use case. Cosider this code:</span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-size: 13.2px; line-height: 18.48px;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; margin-bottom: 0in;">
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><h:form> <o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:selectManyListbox value="#{playerBean.selectedPlayersList}" <b><span style="color: #c00000;">converter="playerConverter"</span></b>> <o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <f:selectItems value="#{playerBean.playersList}" var="t" itemLabel="#{t.label}" itemValue="#{t}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> </h:selectManyListbox><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:commandButton value="Select" action="#{playerBean.selectedAction()}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></h:form></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-size: 13.2px; line-height: 18.48px;">Basically, this time we use data for which we don't have a built-in converter available. The custom converter used here is needed because this time the list is "populated" with several </span><span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Player</span><span style="font-size: 13.2px; line-height: 18.48px;"> instances:</span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-size: 13.2px; line-height: 18.48px;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0in;">
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">private ArrayList<Player> selectedPlayersList;<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">private static final ArrayList<Player> playersList;<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">static {<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> playersList = new ArrayList<>();<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> playersList.add(new Player("Rafa", "Rafael Nadal"));<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> playersList.add(new Player("Roger F", "Roger Federer"));<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> playersList.add(new Player("Nole", "Novak Djokovic"));<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public ArrayList<Player> getPlayersList() {<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> return playersList;<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public ArrayList<Player> getSelectedPlayersList() {<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> return selectedPlayersList;<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public void setSelectedPlayersList(ArrayList<Player> selectedPlayersList) {<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> this.selectedPlayersList = selectedPlayersList;<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br />
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;">// Player class snippet of code </span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;">public class Player implements Serializable {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;"> private String label;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;"> private String value;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;"> public Player(String label, String value) {<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;"> this.label = label;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;"> this.value = value;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;"> }</span></div>
<div class="MsoNormal" style="font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 9pt; line-height: 13.8px;"> ...</span></div>
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Remember from the above use case that the generic type of <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">List<></span> is lost during runtime. Since the selected items are treated as strings instead of <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Player </span>instances, the below code will cause an error because <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">#{i.label}</span> cannot be evaluated:</div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><ui:repeat value="#{playerBean.selectedPlayersList}" var="i"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> #{i.label}: </span><b style="font-family: "Courier New"; font-size: 12px; line-height: 13.8px;"><span style="color: #c00000;">#{i.getClass()}</span></b></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></ui:repeat><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Since there is no built-in converter for converting strings to <span style="font-family: "courier new"; font-size: 12px; line-height: 13.8px;">Player </span>instances, we need a custom converter as below:</div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@FacesConverter("playerConverter")<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class PlayerConverter implements Converter {<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public Object getAsObject(FacesContext context, UIComponent component,String value) {<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> String[] parts = value.split("/");<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> return new Player(parts[0],parts[1]);<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public String getAsString(FacesContext context, UIComponent component,Object value) {<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> return value.toString();<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> } <o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
</div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Now, everything works as expected!</div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b style="font-size: medium; line-height: 18.48px;">JSF 2.3</b></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Well, while example 1 is pretty simple to "fix", the second example is not so easy since it requires us to write a custom converter. But, JSF 2.3 comes with a new feature that is capable to detect <span style="font-family: "courier new"; font-size: 12px; line-height: 13.8px;">Converter </span>based on 1st <span style="font-family: "courier new"; font-size: 12px; line-height: 13.8px;">UISelectMany</span><span style="color: black; font-family: "times new roman"; font-size: small; line-height: normal;"> </span>item and save us for using a built-in converter or writing a custom one for this purpose. So, in JSF 2.3 the below two codes will work as expected:</div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">1: </span><span style="font-size: 13.2px; line-height: 18.48px;">there is no need to specify a built-in conveter</span></b></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><h:form> <o:p></o:p></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:selectManyListbox value="#{playerBean.selectedRanks}" <b><span style="color: #c00000;"><strike>converter="javax.faces.Integer"</strike></span></b>> <o:p></o:p></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <f:selectItems value="#{playerBean.playersRanks}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> </h:selectManyListbox><o:p></o:p></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:commandButton value="Select" action="#{playerBean.selectedAction()}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></h:form> </span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><ui:repeat value="#{playerBean.selectedRanks}" var="i"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> #{i}: <b><span style="color: #c00000;">#{i.getClass()}</span></b><o:p></o:p></span></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></ui:repeat></span></div>
</div>
</div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
2: <b style="font-size: 13.2px; line-height: 18.48px;"><span style="font-size: 13.2px; line-height: 18.48px;">there is no need to write/specify a custom converter</span></b></div>
<div class="MsoNormal" style="margin-bottom: 0in;">
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><h:form> <o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:selectManyListbox value="#{playerBean.selectedPlayersList}" <b><span style="color: #c00000;"><strike>converter="playerConverter"</strike></span></b>> <o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <f:selectItems value="#{playerBean.playersList}" var="t" itemLabel="#{t.label}" itemValue="#{t}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> </h:selectManyListbox><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:commandButton value="Select" action="#{playerBean.selectedAction()}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></h:form></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0in;">
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><ui:repeat value="#{playerBean.selectedPlayersList}" var="i"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> #{i.label}: </span><b style="font-family: "Courier New"; font-size: 12px; line-height: 13.8px;"><span style="color: #c00000;">#{i.getClass()}</span></b></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></ui:repeat></span></div>
<div class="MsoNormal" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0in;">
<span style="color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif;"><span style="font-size: 13.2px; line-height: 18.48px;">For studying the implementation please download </span></span><a href="https://github.com/jsf-spec/mojarra" style="color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px;">Mojarra</a><span style="color: #666666; font-family: "trebuchet ms" , "trebuchet" , "verdana" , sans-serif;"><span style="font-size: 13.2px; line-height: 18.48px;"> source code and check out the </span></span><span style="color: #666666; font-family: "courier new"; font-size: 9pt; line-height: 107%;">javax.faces.component.UISelectMany, </span><span style="color: #666666; font-family: "courier new";"><span style="font-size: 12px; line-height: 12.84px;">com.sun.faces.renderkit.html_basic.MenuRenderer </span></span><span style="font-family: "calibri" , sans-serif; font-size: 11pt; line-height: 107%;">(especially, </span><span style="color: #666666; font-family: "courier new"; font-size: 12px; line-height: 12.84px;">MenuRenderer#</span><span style="color: #666666; font-family: "courier new";"><span style="font-size: 12px; line-height: 12.84px;">getConverterForSelectManyValues()</span></span><span style="font-family: "calibri" , sans-serif; font-size: 14.6667px; line-height: 15.6933px;">method) and </span><span style="font-family: "courier new"; font-size: 9pt; line-height: 107%;">com.sun.faces.util.getConverterForClass().</span></div>
</div>
</div>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-30553202778210173682016-06-21T08:59:00.003-07:002016-06-21T09:12:54.226-07:00CDI-JSF: Using CDI alternatives priority (@Priority)<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Before you read this post please read: <a href="http://www.omnifaces-fans.org/2016/06/use-cdi-alternative-as-mock.html">Use
a CDI alternative as a mock implementation for a stateless session bean</a></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
So, since you are familiar with the above post, now let's suppose that
we have multiple mock implementations. In such cases, the problem is how to
instruct the container to choose between the implementations? The answer relies
on alternatives priority! </div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Basically, we annotate each mock implementation with <b><span style="color: red; font-family: "courier new"; font-size: 9pt;">@Priority</span></b> and
provide a number as the level of priority. The higher number has the higher
priority. Here it is three mock implementations with priorities 1,2 and 3:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;">@Priority(1)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Alternative<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Stateless<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooServiceMock implements IFooService {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public List getAllFoo() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return Arrays.asList("foo 1",
"foo 2", "foo 3");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;">@Priority(2)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Alternative<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Stateless<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
BestFooServiceMock implements IFooService {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public List getAllFoo() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return Arrays.asList("best foo 1",
"best foo 2", "best foo 3");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">} <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;">@Priority(3)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Alternative<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Stateless<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
GreatFooServiceMock implements IFooService {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public List getAllFoo() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return Arrays.asList("great foo 1",
"great foo 2", "great foo 3");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The mock implementation annotated with <b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;">@Priority(3)
</span></b>has the higher priority. When we run the below code CDI will automatically
choose the <span style="font-family: "courier new"; font-size: 9.0pt;">GreatFooServiceMock:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final Logger LOG =
Logger.getLogger(FooBean.class.getName());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private IFooService fooService; <span style="color: red;">// choose GreatFooServiceMock<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void loadAllFoo() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> List allfoo = fooService.getAllFoo();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> LOG.log(Level.INFO, "allfoo:{0}",
allfoo);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now, we can also write a test using CDI-Unit as below (this will also
choose the <span style="font-family: "courier new"; font-size: 9.0pt;">GreatFooServiceMock</span>):</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RunWith(CdiRunner.class)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@ActivatedAlternatives({FooServiceMock.class,
BestFooServiceMock.class, GreatFooServiceMock.class}) <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooBeanTest {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> FooBean fooBean;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Test<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @InRequestScope<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void testStart() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> fooBean.loadAllFoo();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> } <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br />
We can use the <b><span style="color: red; font-family: "courier new"; font-size: 9pt;">@Priority</span></b> annotation to specify alternatives globally for an application that consists of multiple modules like this:<br />
<span style="font-family: "courier new"; font-size: 12px;"><br /></span>
<span style="font-family: courier new;"><span style="font-size: 12px;">@</span></span><span style="font-family: "courier new"; font-size: 12px;">Alternative</span><br />
<span style="font-family: courier new;"><span style="font-size: 12px;">@Priority(Interceptor.Priority.APPLICATION+10)</span></span><br />
<span style="font-family: "courier new"; font-size: 12px;">public class ...</span><br />
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The complete example is available <a href="https://github.com/AnghelLeonard/CDI-JSF/tree/master/PriorityAlternativeExample">here</a>.</div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-64913168773088988132016-06-18T04:32:00.001-07:002016-06-18T04:36:08.080-07:00CDI-JSF: Use a CDI alternative as a mock implementation for a stateless session bean<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
A great feature of CDI (supported by Java EE starting with version 6)
consist in <b><i>alternatives</i></b>. Basically, we want to specify an alternative for
an injected object. Let's have a simple scenario commonly followed in
JSF applications that uses stateless session beans to interact with a database.
Supposing that we write the business logic part to query the database. We can
start with an interface as:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public
interface IFooService {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> List getAllFoo();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Further, we can write a stateless session bean that implements this
interface and take usage of JPA capabilities to query the database. Well the
things didn't go too far for this part because the database is not ready and
all we can provide is a skeleton like below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Stateless<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooService implements IFooService {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @PersistenceContext(unitName =
"fooPU")<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private EntityManager em;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public List getAllFoo() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> // perform
the query<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> ...<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return new ArrayList();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Finally, we inject the stateless session bean in a CDI managed bean as
below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final Logger LOG =
Logger.getLogger(FooBean.class.getName());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private IFooService fooService;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void loadAllFoo() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> List allfoo = fooService.getAllFoo();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> LOG.log(Level.INFO, "allfoo:{0}",
allfoo);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now, let's suppose that we want to run/test this method, but we have an issue. The data that we expect from the database will not be
available, so we think to mock this service and provide a set of dummy data as
below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Stateless<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooServiceMock implements IFooService {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public List getAllFoo() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return Arrays.asList("foo 1",
"foo 2", "foo 3");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
But, if we test the code now, we will obtain an error of type <a href="http://www.omnifaces-fans.org/2016/06/cdi-jsf-avoid-common-mistakes-that.html">ambiguous
dependencies</a>. In order to fix this error, we simply annotated our mock with
<span style="font-family: "courier new"; font-size: 9.0pt;">@Alternative</span> annotation:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">@Alternative</span><span style="color: #c00000;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Stateless<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooServiceMock implements IFooService {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Override<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public List getAllFoo() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return Arrays.asList("foo 1",
"foo 2", "foo 3");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
If we run now, there will be no errors, but the application will not
use the mock. This is happening because our alternative is not activated. We must
accomplish this in the <span style="font-family: "courier new"; font-size: 9.0pt;">beans.xml</span>,
as below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><?xml
version="1.0" encoding="UTF-8"?><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> bean-discovery-mode="all"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> <alternatives><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">
<class>beans.FooServiceMock</class><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;"> </alternatives><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></beans><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Done! Now the <span style="font-family: "courier new"; font-size: 9.0pt;">FooBean
</span>will use the <span style="font-family: "courier new"; font-size: 9.0pt;">FooServiceMock</span>
instead of <span style="font-family: "courier new"; font-size: 9.0pt;">FooService. </span>When
the database will be ready/queryable we will simply deactivate the alternative.</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Via CDI-Unit we can write a quick test that also uses our mock:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RunWith(CdiRunner.class)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9.0pt;">@ActivatedAlternatives(FooServiceMock.class)
<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
FooBeanTest {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> FooBean fooBean;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Test<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @InRequestScope<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void testStart() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> fooBean.loadAllFoo();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> } <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}</span></div>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The complete example is available <a href="https://github.com/AnghelLeonard/CDI-JSF/tree/master/AlternativesExample">here</a>.</div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-79587913537954093172016-06-17T08:58:00.001-07:002016-06-17T08:58:40.956-07:00CDI-JSF: Testing CDI code using CDI-Unit<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
There are a few approaches to test CDI code such as using <span style="font-family: "courier new"; font-size: 9pt;">@Alternative</span>
and/or mocking. Another approach, presented here is to use the <a href="http://jglue.org/">JGlue</a> project - more exactly the <a href="http://jglue.org/cdi-unit/">CDI-Unit</a> which is a JUnit4 test runner
that enables unit testing Java CDI applications.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
For example, let's consider the application from post <a href="http://www.omnifaces-fans.org/2016/06/cdi-jsf-inject-java-logger-via-cdi.html">Inject
a Java logger via a CDI producer method</a> - in this application you saw exactly
what the name of the post say, how to inject a Java logger via a producer
method. Now, let's suppose that we want to write a JUnit test for the method <span style="font-family: "courier new"; font-size: 9pt;">FooBean#logBuzzAction()</span>:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">public
class FooBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> Logger fooLog;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> public void logFooAction() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> fooLog.info("Log message from FooBean
!");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
In a pretty dummy approach we can try this:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">public
class LoggerTest {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> FooBean fooBean;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Test <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> public void testStart() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> fooBean.logFooAction();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Well, obviously this will not work! The problem will be caused by
the <span style="font-family: "courier new"; font-size: 9pt;">@Inject</span>
part, so is time to find a solution. Add quickly the CDI-Unit dependency in the
POM:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"><dependency><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> <groupId>org.jglue.cdi-unit</groupId><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> <artifactId>cdi-unit</artifactId><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> <version>3.1.2</version><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> <scope>test</scope><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"></dependency><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
And specify @<span style="font-family: "courier new"; font-size: 9pt;">RunWith(CdiRunner.class)</span> on your JUnit4 test class
to enable injection directly into the test class:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new";">@RunWith(CdiRunner.class)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">public
class LoggerTest {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> FooBean fooBean;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Test <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> public void testStart() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> fooBean.logFooAction();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Ok, the <span style="font-family: "courier new"; font-size: 9pt;">FooBean</span> is injected now! But, now we have
an unsatisfied dependencies for type <span style="font-family: "courier new"; font-size: 9pt;">Logger</span>. In order to fix this issue, simply specify <span style="font-family: "courier new"; font-size: 9pt;">MyLogger</span>
class as an additional class for this test. This will tell CDI-Unit to explicitly add a class to the CDI
environment:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">@RunWith(CdiRunner.class)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new";">@AdditionalClasses(MyLogger.class) <o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">public
class LoggerTest {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> FooBean fooBean;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Test <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> public void testStart() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> fooBean.logFooAction();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Ok, the last issue that we must solve consist in the fact that
there is no active contexts for the request scope, but CDI-Unit has built in
support for request, session and conversation scopes using <span style="font-family: "courier new"; font-size: 9pt;">@InRequestScope</span>,
<span style="font-family: "courier new"; font-size: 9pt;">@InSessionScope</span>
and <span style="font-family: "courier new"; font-size: 9pt;">@InConversationScope</span>.
So, let's bring the context in:<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">@RunWith(CdiRunner.class)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">@AdditionalClasses(MyLogger.class)
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">public
class LoggerTest {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Inject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> FooBean fooBean;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> @Test<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: #c00000; font-family: "courier new";"> @InRequestScope<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> public void testStart() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> fooBean.logFooAction();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Done! Now the test is ready and you can see it <a href="https://github.com/AnghelLeonard/CDI-JSF/blob/master/InjectLoggerWithTest/src/test/java/tests/LoggerTest.java">here</a>.<o:p></o:p></div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-78613367107591465952016-06-17T04:26:00.001-07:002016-06-17T04:44:19.049-07:00CDI-JSF: Using the CDI @Observes<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
In this post we will discuss about using the CDI <span style="font-family: "courier new"; font-size: 12px; line-height: 13.8px;">@Observes </span><span style="font-size: 13.2px; line-height: 18.48px;">in a JSF application.</span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-size: 13.2px; line-height: 18.48px;"><br /></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Basically, we will exploit the fact that Java EE provides an easier implementation of the observer design pattern via the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Observes</span> annotation and <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">javax.enterprise.event.Event<T></span> interface.<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br />
<b>Note </b>In the bellow examples, we will use CDI managed beans, but you can use EJB 3 beans also.</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Basically, the observer pattern is based on a <i>subject</i> and some <i>observers</i>:<br />
<i><br /></i><i>subject</i> - an object that changes its state</div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<o:p></o:p></div>
<div class="MsoNormal" style="font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<i>observers</i> - objects notified when the <i>subject </i>has changed its state</div>
</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
This time, the <i style="font-size: 13.2px; line-height: 18.48px;">subject</i><span style="font-size: 13.2px; line-height: 18.48px;"> is a </span><span style="font-size: 13.2px; line-height: 18.48px;">CDI managed bean named, </span><span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">MainFireStationBean</span><span style="font-size: 13.2px; line-height: 18.48px;">:</span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class MainFireStationBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> @Inject<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> Event<String> evt;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void fireStarted(<b><span style="color: #c00000;">String</span></b> <b><span style="color: #c00000;">address</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> evt.fire(address);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
The container injects an <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Event</span> object of type <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">String</span> into the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">evt</span> instance variable of the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">MainFireStationBean </span>class (practically, this <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">String</span> represents the fire address). To activate an event, call the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">javax.enterprise.event.Event.fire()</span> method. This method fires an event and notifies any observer methods (<i>observers</i>). Now the observable part is completed, so it is time to create the <i>observers</i> that listens for our <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">String</span> events.<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
In Java EE the <i>observers</i> are marked with the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Observes</span> annotation. The addition of the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Observes</span> annotation to the method signature instructs the container that this method should act as an <i>observer</i> of events of the type it precedes.<br />
<br />
We have three observers, <span style="font-family: "courier new"; font-size: 12px; line-height: 13.8px;">ViningsFireStationBean </span>, <span style="font-family: "courier new"; font-size: 12px; line-height: 13.8px;">BrookhavenFireStationBean </span>and <span style="font-family: "courier new"; font-size: 12px; line-height: 13.8px;">DecaturFireStationBean</span>:<br />
<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Dependent<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class ViningsFireStationBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void update(<b><span style="color: #c00000;">@Observes String arg</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Vinings fire department will go to " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Dependent<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class BrookhavenFireStationBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void update(<b><span style="color: #c00000;">@Observes String arg</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Brookhaven fire department will go to " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Dependent<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class DecaturFireStationBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void update(<b><span style="color: #c00000;">@Observes String arg</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Decatur fire department will go to " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
So, the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Observes</span> annotation precedes the type <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">String</span> and thus listens for events of that type. The <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Observes</span> annotation followed by an object type instruct the container will all the needed information.<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
In order to test it, we just need to report some fires. We can do this in several ways, but let's do it quickly via two JSF buttons:<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:commandButton value="Report Fire at Home Park Atlanta" </span><br />
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> action="#{mainFireStationBean.fireStarted('Home Park Atlanta, GA')}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:commandButton value="Report Fire at High Museum of Art" </span><br />
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> action="#{mainFireStationBean.fireStarted('High Museum of Art 1280 Peachtree St NE Atlanta, GA 30309')}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
If we suppose that the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Report Fire at Home Park Atlanta </span>button was pressed then the output will be:<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Decatur fire department will go to Home Park Atlanta, GA<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Vinings fire department will go to Home Park Atlanta, GA<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Brookhaven fire department will go to Home Park Atlanta, GA<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
So, everything works as expected. The complete example is available <a href="https://github.com/AnghelLeonard/CDI-JSF/tree/master/FireStationObserver">here</a>.<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0.0001pt;">
One step further and we will want to differentiate between the same object types of objects and set up different observers to listen for them. For example, we may need to distinguish between small fires and big fires. Depending on this aspect, a local fire station may send to the fire address one fire truck or multiple fire trucks. We can model this case via a qualifier:<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Qualifier<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Retention(RetentionPolicy.RUNTIME)<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Target({ElementType.FIELD, ElementType.PARAMETER})<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public @interface FireType {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> Type value();<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> enum Type {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> SMALL, BIG<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
The two enum types (<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">SMALL</span> and <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">BIG</span>) will be used to act as annotation to mark the strings to be fired by the event instances. So, the <span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">MainFireStationBean</span> will be:<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class MainFireStationBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> @Inject<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> @FireType(Type.SMALL)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> Event<String> small;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> @Inject<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> @FireType(Type.BIG)<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> Event<String> big;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void fireStarted(String address, boolean t) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> if (t) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> small.fire(address);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> } else {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<b><span style="color: #c00000; font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> big.fire(address);<o:p></o:p></span></b></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Finally, add the annotations to the <i>observer</i> part:<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Dependent<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class ViningsFireStationBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void updateSmallFire(<b><span style="color: #c00000;">@Observes @FireType(FireType.Type.SMALL) String arg</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Vinings fire department will go to a small fire at " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void updateBigFire(<b><span style="color: #c00000;">@Observes @FireType(FireType.Type.BIG) String arg</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Vinings fire department will go to a big fire at " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Dependent<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class BrookhavenFireStationBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void updateSmallFire(<b><span style="color: #c00000;">@Observes @FireType(FireType.Type.SMALL) String arg</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Brookhaven fire department will go to a small fire at " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void updateBigFire(<b><span style="color: #c00000;">@Observes @FireType(FireType.Type.BIG) String arg</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Brookhaven fire department will go to a big fire at " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">@Dependent<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">public class DecaturFireStationBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void updateSmallFire(<b><span style="color: #c00000;">@Observes @FireType(FireType.Type.SMALL) String arg</span></b>) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Decatur fire department will go to a small fire at " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> public void updateBigFire(<b><span style="color: #c00000;">@Observes @FireType(FireType.Type.BIG) String a</span></b>rg) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> System.out.println("Decatur fire department will go to a big fire at " + arg);<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Now, let's report a big fire and a small fire:<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"><h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:commandButton value="Report a Small Fire at Home Park Atlanta" </span><br />
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> action="#{mainFireStationBean.fireStarted('Home Park Atlanta, GA', true)}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> <h:commandButton value="Report a Big Fire at High Museum of Art" </span><br />
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"> action="#{mainFireStationBean.fireStarted('High Museum of Art 1280 Peachtree St NE Atlanta, GA 30309', false)}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;"></h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
In case of a small fire the output will be:<o:p></o:p></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Brookhaven fire department will go to a small fire at Home Park Atlanta, GA<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Vinings fire department will go to a small fire at Home Park Atlanta, GA<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<span style="font-family: "courier new"; font-size: 9pt; line-height: 13.8px;">Decatur fire department will go to a small fire at Home Park Atlanta, GA<o:p></o:p></span></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
Note that in case of your own object types you don't need qualifiers. Since the object type is unique, you can fire/observe your own object types by using the object.</div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="background-color: white; color: #666666; font-family: "Trebuchet MS", Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 18.48px; margin-bottom: 0in;">
The complete example is available <a href="https://github.com/AnghelLeonard/CDI-JSF/tree/master/FireStationFineObserver">here</a>.</div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0tag:blogger.com,1999:blog-6358539998192401509.post-8033308958818108982016-06-16T05:35:00.001-07:002016-06-17T03:31:33.939-07:00CDI-JSF: Injecting HTTP parameters via a producer method<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Remeber from the <a href="http://www.omnifaces-fans.org/2016/06/cdi-jsf-inject-java-logger-via-cdi.html">Inject
a Java logger via a CDI producer method</a> post that the <span style="font-family: "courier new"; font-size: 9.0pt;">InjectionPoint</span> can be
used to access metadata of the class where the artifact is injected. Based on
this statement, we can write a producer method capable to inject HTTP
parameters.</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Typically, as a JSF developer you write a form like below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> Name: <h:inputText
value="#{playerBean.name}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> Surname: <h:inputText
value="#{playerBean.surname}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:commandButton value="Register"
action="#{playerBean.registerAction()}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The two fields, <span style="font-family: "courier new"; font-size: 9.0pt;">name</span>
and <span style="font-family: "courier new"; font-size: 9.0pt;">surname</span> are
declared in a bean <span style="font-family: "courier new"; font-size: 9.0pt;">private</span>
and with some getters and setters:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
PlayerBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final Logger LOG =
Logger.getLogger(PlayerBean.class.getName());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private String name;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private String surname;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public String getName() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return name;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void setName(String name) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> this.name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public String getSurname() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return
surname;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void setSurname(String surname) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> this.surname = surname;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void registerAction() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> LOG.info("Register: " + name +
" " + surname);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Well, nothing fancy here, and even a JSF novice can understand what
this code do just by looking over it in a second. But, now let's replace the <span style="font-family: "courier new"; font-size: 9.0pt;"><h:inputText/></span>
with simple HTML <span style="font-family: "courier new"; font-size: 9.0pt;"><input/></span>
as below:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"><h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;"> Name: <input type="text"
name="name" id="name"/><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;"> Surname: <input type="text"
name="surname" id="surname"/><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> <h:commandButton value="Register"
action="#{playerBean.registerAction()}"/><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"></h:form><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
This time our getters and setters become useless, but we can still
obtain these two HTTP parameters like this:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">FacesContext
facesContext = FacesContext.getCurrentInstance();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">ExternalContext
externalContext = facesContext.getExternalContext();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">ServletRequest
request = (ServletRequest) externalContext.getRequest();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">request.getParameter("name");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">request.getParameter("surname");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now, let's generalize this case
in CDI style. Instead of writing <span style="font-family: "courier new"; font-size: 9.0pt;">request.getParameter("name");
</span>we want to inject the HTTP parameter. For this, we start by defining a
qualifier and instruct the container to ignore the value:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Qualifier<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Retention(RUNTIME)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Target({METHOD,
FIELD, PARAMETER, TYPE})<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public
@interface HttpParam {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Nonbinding public String value();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Furthermore, we write the producer method as below - instead of
explicitly nominated the HTTP parameter name, we use the <span style="font-family: "courier new"; font-size: 9.0pt;">InjectionPoint</span> metadata
features:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
HttpParams {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @Produces<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> @HttpParam("")<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> String getParamValue(InjectionPoint ip) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> //
obtain the current request<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> FacesContext
facesContext = FacesContext.getCurrentInstance();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> ExternalContext
externalContext = facesContext.getExternalContext();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> ServletRequest
request = (ServletRequest) externalContext.getRequest();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> //
obtain the desired parameter<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> Annotated annotated = ip.getAnnotated();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> HttpParam httpParam =
annotated.getAnnotation(HttpParam.class);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> return
request.getParameter(httpParam.value());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Finally, we adjust the <span style="font-family: "courier new"; font-size: 9.0pt;">PlayerBean</span>
to inject the desired HTTP parameters:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@Named<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">@RequestScoped<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">public class
PlayerBean {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> private static final Logger LOG =
Logger.getLogger(PlayerBean.class.getName());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;"> @HttpParam("name") @Inject String
name;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<b><span style="color: red; font-family: "courier new"; font-size: 9.0pt;"> @HttpParam("surname") @Inject String
surname;<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> public void registerAction() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> LOG.log(Level.INFO, "Register: {0}
{1}", new Object[]{name, surname});<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 9.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Done! Following this example you can inject any other HTTP parameter.
The complete example is available <a href="https://github.com/AnghelLeonard/CDI-JSF/tree/master/InjectHTTPParams">here</a>.<span style="font-family: "courier new"; font-size: 9.0pt;"><o:p></o:p></span><br />
<br />
Now, you can use this with <span style="font-family: "courier new"; font-size: 12px;"><f:param/> </span>or in place of <span style="font-family: "courier new"; font-size: 12px;"><f:event type="preRenderView/> </span>or <span style="font-family: "courier new"; font-size: 12px;"><f:viewAction/>. </span>In addition, you may be interested in converting and validating the HTTP parameters. Check out a great implementation from OmniFaces which is materialized in the CDI annotation <span style="font-family: "courier new"; font-size: 12px;"><a href="http://showcase.omnifaces.org/cdi/Param">@Param</a></span> that allows us to inject, convert and validate a HTTP request parameter in a CDI managed bean. </div>
Anonymoushttp://www.blogger.com/profile/05850083818956862754noreply@blogger.com0