openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r881120 - in /websites/staging/openwebbeans/trunk/content: ./ cdi_explained.html
Date Fri, 04 Oct 2013 10:11:13 GMT
Author: buildbot
Date: Fri Oct  4 10:11:12 2013
New Revision: 881120

Log:
Staging update by buildbot for openwebbeans

Modified:
    websites/staging/openwebbeans/trunk/content/   (props changed)
    websites/staging/openwebbeans/trunk/content/cdi_explained.html

Propchange: websites/staging/openwebbeans/trunk/content/
------------------------------------------------------------------------------
--- cms:source-revision (original)
+++ cms:source-revision Fri Oct  4 10:11:12 2013
@@ -1 +1 @@
-1529111
+1529115

Modified: websites/staging/openwebbeans/trunk/content/cdi_explained.html
==============================================================================
--- websites/staging/openwebbeans/trunk/content/cdi_explained.html (original)
+++ websites/staging/openwebbeans/trunk/content/cdi_explained.html Fri Oct  4 10:11:12 2013
@@ -94,8 +94,8 @@ including Java SE.</p>
 <p>this does not mean that CDI applications can only run in JavaEE those days. 
 At least OpenWebBeans provides CDI functionality which also runs in pure Java SE apps like
Swing, JavaFX
 and even Eclipse RCP apps as well.</p>
-<h3 id="relation-to-jsr-330">Relation to JSR-330</h3>
-<p>A half year before the CDI specification became final, other communities 
+<h3 id="relationship-to-jsr-330">Relationship to JSR-330</h3>
+<p>Half a year before the CDI specification became final, other communities 
 (Spring and guice) had begun an effort to specify the basics of injection as </p>
 <blockquote>
 <p>“JSR-330: Dependency Injection for Java” (nicknamed “AtInject”).
</p>
@@ -149,7 +149,8 @@ CDI provides out-of-the-box support for 
 </li>
 </ul>
 <h2 id="a-small-cdi-example">A small CDI Example</h2>
-<p>The following small sample application allows you to send eMails via a web form.
+<p>The following small JSF and CDI sample application allows 
+you to send eMails via a web form.
 We will only show code fragments, please checkout our samples form our 
 <a href="source.html">Source Code</a> for more information.</p>
 <h4 id="the-backend">The 'Backend'</h4>
@@ -172,6 +173,71 @@ The <code>@Inject</code> annotation tell
     <span class="o">}</span>
 <span class="o">}</span>
 </pre></div>
+
+
+<h4 id="the-user-handling">The User handling</h4>
+<p>Our application also knows the currently loggedin user which we like to 
+store in the Servlet Session (the login itself is not part of this sample, 
+just consider  this is done via your container or a 
+login page + Servlet Filter upfront).</p>
+<p>CDI provides the session scope, which ensures that you will get 
+the same instance of an object per HTTP Session (in a web application).</p>
+<div class="codehilite"><pre><span class="nd">@SessionScoped</span>
+<span class="nd">@Named</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">User</span> <span class="o">{</span>
+    <span class="kd">public</span> <span class="n">String</span>
<span class="nf">getName</span><span class="o">()</span> <span
class="o">{..}</span>
+    <span class="kd">public</span> <span class="n">String</span>
<span class="nf">getEmail</span><span class="o">()</span> <span
class="o">{..}</span>
+    <span class="o">..</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>By default, CDI beans are not available for use in JSF via the 
+Unified Expression Language. In order to expose it for use by JSF and EL, 
+we simply added the <code>@Named</code> annotation.</p>
+<h4 id="the-backing-bean">The Backing Bean</h4>
+<p>Our web page is implemented with JSF-2. Thus we need a backing bean.
+We use a <code>@RequestScoped</code> backing bean which means that every request
+will get a new instance of it. Otoh, during the request you will always get
+the same instance.</p>
+<div class="codehilite"><pre><span class="nd">@RequestScoped</span>
+<span class="nd">@Named</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">MailForm</span> <span class="o">{</span>
+    <span class="kd">private</span> <span class="nd">@Inject</span>
<span class="n">MailService</span> <span class="n">mailService</span><span
class="o">;</span>
+    <span class="kd">private</span> <span class="nd">@Inject</span>
<span class="n">User</span> <span class="n">user</span><span class="o">;</span>
+
+    <span class="kd">private</span> <span class="n">String</span>
<span class="n">text</span><span class="o">;</span> <span class="c1">//
+ getter and setter</span>
+    <span class="kd">private</span> <span class="n">String</span>
<span class="n">recipient</span><span class="o">;</span> <span
class="c1">// + getter and setter</span>
+
+    <span class="kd">public</span> <span class="n">String</span>
<span class="nf">sendMail</span><span class="o">()</span> <span
class="o">{</span>
+        <span class="n">mailService</span><span class="o">.</span><span
class="na">send</span><span class="o">(</span><span class="n">user</span><span
class="o">.</span><span class="na">getName</span><span class="o">(),</span>
<span class="n">recipient</span><span class="o">,</span> <span
class="n">text</span><span class="o">);</span>
+        <span class="k">return</span> <span class="s">&quot;messageSent&quot;</span><span
class="o">;</span> <span class="c1">// forward to &#39;message sent&#39;
JSF2 page</span>
+    <span class="o">}</span>
+
+<span class="o">}</span>
+</pre></div>
+
+
+<h4 id="the-jsf-2-page">The JSF-2 page</h4>
+<p>We now only miss the JSF page itself to make our example work.
+The page is implemented using <em>facelets</em>.</p>
+<div class="codehilite"><pre>...
+<span class="nt">&lt;h:form&gt;</span>
+    <span class="nt">&lt;h:outputLabel</span> <span class="na">value=</span><span
class="s">&quot;Username&quot;</span> <span class="na">for=</span><span
class="s">&quot;username&quot;</span><span class="nt">/&gt;</span>
+    <span class="nt">&lt;h:outputText</span> <span class="na">id=</span><span
class="s">&quot;username&quot;</span> <span class="na">value=</span><span
class="s">&quot;#{user.name}&quot;</span><span class="nt">/&gt;&lt;br/&gt;</span>
+    <span class="nt">&lt;h:outputLabel</span> <span class="na">value=</span><span
class="s">&quot;Recipient&quot;</span> <span class="na">for=</span><span
class="s">&quot;recipient&quot;</span><span class="nt">/&gt;</span>
+    <span class="nt">&lt;h:inputText</span> <span class="na">id=</span><span
class="s">&quot;recipient&quot;</span> <span class="na">value=</span><span
class="s">&quot;#{mailForm.recipient}&quot;</span><span class="nt">/&gt;&lt;br/&gt;</span>
+    <span class="nt">&lt;h:outputLabel</span> <span class="na">value=</span><span
class="s">&quot;Body&quot;</span> <span class="na">for=</span><span
class="s">&quot;body&quot;</span><span class="nt">/&gt;</span>
+    <span class="nt">&lt;h:inputText</span> <span class="na">id=</span><span
class="s">&quot;body&quot;</span> <span class="na">value=</span><span
class="s">&quot;#{mailForm.body}&quot;</span><span class="nt">/&gt;&lt;br/&gt;</span>
+
+    <span class="nt">&lt;h:commandButton</span> <span class="na">value=</span><span
class="s">&quot;Send&quot;</span> <span class="na">action=</span><span
class="s">&quot;#{mailForm.send}&quot;</span><span class="nt">/&gt;</span>
+<span class="nt">&lt;/h:form&gt;</span>
+...
+</pre></div>
+
+
+<p>This page uses the backing bean from above via it's EL name <em>mailForm</em>.
+In the same way it uses the <code>@SessionScoped</code> <em>user</em>.</p>
         </div>
 
         <hr>



Mime
View raw message