Return-Path: X-Original-To: apmail-openwebbeans-commits-archive@www.apache.org Delivered-To: apmail-openwebbeans-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EE38910B32 for ; Fri, 4 Oct 2013 10:11:34 +0000 (UTC) Received: (qmail 87881 invoked by uid 500); 4 Oct 2013 10:11:34 -0000 Delivered-To: apmail-openwebbeans-commits-archive@openwebbeans.apache.org Received: (qmail 87861 invoked by uid 500); 4 Oct 2013 10:11:32 -0000 Mailing-List: contact commits-help@openwebbeans.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openwebbeans.apache.org Delivered-To: mailing list commits@openwebbeans.apache.org Received: (qmail 87851 invoked by uid 99); 4 Oct 2013 10:11:30 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Oct 2013 10:11:30 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Oct 2013 10:11:28 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id AD46A2388A5B; Fri, 4 Oct 2013 10:11:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r1529115 - /openwebbeans/cms-site/trunk/content/cdi_explained.mdtext Date: Fri, 04 Oct 2013 10:11:08 -0000 To: commits@openwebbeans.apache.org From: struberg@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131004101108.AD46A2388A5B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: struberg Date: Fri Oct 4 10:11:08 2013 New Revision: 1529115 URL: http://svn.apache.org/r1529115 Log: add sample source code Modified: openwebbeans/cms-site/trunk/content/cdi_explained.mdtext Modified: openwebbeans/cms-site/trunk/content/cdi_explained.mdtext URL: http://svn.apache.org/viewvc/openwebbeans/cms-site/trunk/content/cdi_explained.mdtext?rev=1529115&r1=1529114&r2=1529115&view=diff ============================================================================== --- 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 + ... + + +
+ +
+ +
+ + +
+ ... + +This page uses the backing bean from above via it's EL name *mailForm*. +In the same way it uses the ``@SessionScoped`` *user*. +