openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject svn commit: r1529115 - /openwebbeans/cms-site/trunk/content/cdi_explained.mdtext
Date Fri, 04 Oct 2013 10:11:08 GMT
Author: struberg
Date: Fri Oct  4 10:11:08 2013
New Revision: 1529115

add sample source code


Modified: openwebbeans/cms-site/trunk/content/cdi_explained.mdtext
--- openwebbeans/cms-site/trunk/content/cdi_explained.mdtext (original)
+++ openwebbeans/cms-site/trunk/content/cdi_explained.mdtext Fri Oct  4 10:11:08 2013
@@ -43,9 +43,9 @@ this does not mean that CDI applications
 At least OpenWebBeans provides CDI functionality which also runs in pure Java SE apps like
Swing, JavaFX
 and even Eclipse RCP apps as well.
-### Relation to JSR-330
+### Relationship to JSR-330
-A half year before the CDI specification became final, other communities 
+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 
 > “JSR-330: Dependency Injection for Java” (nicknamed “AtInject”).

@@ -98,7 +98,8 @@ CDI provides out-of-the-box support for 
 ## A small CDI Example
-The following small sample application allows you to send eMails via a web form.
+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 
 [Source Code](source.html) for more information.
@@ -127,4 +128,75 @@ The ``@Inject`` annotation tells CDI to 
+#### The User handling
+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).
+CDI provides the session scope, which ensures that you will get 
+the same instance of an object per HTTP Session (in a web application).
+    :::java
+    @SessionScoped
+    @Named
+    public class User {
+        public String getName() {..}
+        public String getEmail() {..}
+        ..
+    }
+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 ``@Named`` annotation.
+#### The Backing Bean
+Our web page is implemented with JSF-2. Thus we need a backing bean.
+We use a ``@RequestScoped`` 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.
+    :::java
+    @RequestScoped
+    @Named
+    public class MailForm {
+        private @Inject MailService mailService;
+        private @Inject User user;
+        private String text; // + getter and setter
+        private String recipient; // + getter and setter 
+        public String sendMail() {
+            mailService.send(user.getName(), recipient, text);
+            return "messageSent"; // forward to 'message sent' JSF2 page
+        }
+    }
+#### The JSF-2 page
+We now only miss the JSF page itself to make our example work.
+The page is implemented using *facelets*.
+    :::xml
+    ...
+    <h:form>
+        <h:outputLabel value="Username" for="username"/>
+        <h:outputText id="username" value="#{}"/><br/>
+        <h:outputLabel value="Recipient" for="recipient"/>
+        <h:inputText id="recipient" value="#{mailForm.recipient}"/><br/>
+        <h:outputLabel value="Body" for="body"/>
+        <h:inputText id="body" value="#{mailForm.body}"/><br/>
+        <h:commandButton value="Send" action="#{mailForm.send}"/>
+    </h:form>
+    ...
+This page uses the backing bean from above via it's EL name *mailForm*.
+In the same way it uses the ``@SessionScoped`` *user*.

View raw message