[webbeans-commits] Webbeans SVN: r598 - ri/trunk/examples/numberguess/WebContent and 1 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Fri Dec 19 15:13:49 EST 2008


Author: pete.muir at jboss.org
Date: 2008-12-19 15:13:49 -0500 (Fri, 19 Dec 2008)
New Revision: 598

Removed:
   ri/trunk/examples/translator/webbeans-translator-war/WebContent/WEB-INF/web-beans.xml
Modified:
   doc/trunk/reference/en/modules/ri.xml
   ri/trunk/examples/numberguess/WebContent/home.xhtml
Log:
Doc the translator example, tidy up

Modified: doc/trunk/reference/en/modules/ri.xml
===================================================================
--- doc/trunk/reference/en/modules/ri.xml	2008-12-19 19:33:21 UTC (rev 597)
+++ doc/trunk/reference/en/modules/ri.xml	2008-12-19 20:13:49 UTC (rev 598)
@@ -432,8 +432,7 @@
       
       <programlisting role="JAVA"><![CDATA[@Named
 @SessionScoped
-public class Game
-{
+public class Game {
    private int number;
    
    private int guess;
@@ -441,13 +440,10 @@
    private int biggest;
    private int remainingGuesses;
    
-   public Game()
-   {
-   }
+   public Game() {}
    
    @Initializer
-   Game(@Random int number, @MaxNumber int maxNumber)
-   {
+   Game(@Random int number, @MaxNumber int maxNumber) {
       this.number = number;
       this.smallest = 1;
       this.biggest = maxNumber;
@@ -456,18 +452,14 @@
 
    // Getters and setters for fields
    
-   public String check()
-   {
-      if (guess>number)
-      {
+   public String check() {
+      if (guess>number) {
          biggest = guess - 1;
       }
-      if (guess<number)
-      {
+      if (guess<number) {
          smallest = guess + 1;
       }
-      if (guess == number)
-      {
+      if (guess == number) {
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Correct!"));
       }
       remainingGuesses--;
@@ -484,6 +476,241 @@
          The translator example will take any sentences you enter, and translate
          them to Latin.
       </para>
+      
+      <para>
+         The translator example is built as an ear, and contains EJBs and 
+         enterprise beans. As a result, it's structure is more complex than
+         the numberguess example.
+      </para>
+      
+      <note>
+         <para>
+            EJB 3.1 and Jave EE 6 allow you to package EJBs in a war, which will
+            make this structure much simpler!
+         </para>
+      </note>
+      
+      <para>
+         First, let's take a look at the ear aggregator, which is located in 
+         <literal>webbeans-translator-ear</literal> module. Maven automatically
+         generates the <literal>application.xml</literal> and 
+         <literal>jboss-app.xml</literal> for us:
+      </para>
+      
+      <programlisting role="XML"><![CDATA[<plugin>
+   <groupId>org.apache.maven.plugins</groupId>
+   <artifactId>maven-ear-plugin</artifactId>
+   <configuration>
+      <modules>
+         <webModule>
+            <groupId>org.jboss.webbeans.examples.translator</groupId>
+            <artifactId>webbeans-translator-war</artifactId>
+            <contextRoot>/webbeans-translator</contextRoot>
+         </webModule>
+      </modules>
+      <jboss>
+         <loader-repository>webbeans.jboss.org:loader=webbeans-translator</loader-repository>
+      </jboss>
+   </configuration>
+</plugin>]]></programlisting>
+
+      <para>
+         We're doing a couple of things here - firstly we set the context path,
+         which gives us a nice url 
+         (<ulink url="http://localhost:8080/webbeans-translator">http://localhost:8080/webbeans-translator</ulink>)
+         and we also enable class loader isolation for JBoss AS.
+      </para>
+      
+      <tip>
+         <para>
+            If you aren't using Maven to generate these files, you would need
+            <literal>META-INF/jboss-app.xml</literal>:
+         </para>
+
+         <programlisting role="XML"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE jboss-app
+    PUBLIC "-//JBoss//DTD J2EE Application 4.2//EN"
+    "http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd">
+<jboss-app>
+  <loader-repository>webbeans.jboss.org:loader=webbeans-translator</loader-repository>
+</jboss-app>]]></programlisting>
+
+         <para>
+            and <literal>META-INF/application.xml</literal>:
+         </para>
+         
+         <programlisting role="XML"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
+<application xmlns="http://java.sun.com/xml/ns/javaee" 
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
+             version="5">
+  <display-name>webbeans-translator-ear</display-name>
+  <description>Ear Example for the reference implementation of JSR 299: Web Beans</description>
+  
+  <module>
+    <web>
+      <web-uri>webbeans-translator.war</web-uri>
+      <context-root>/webbeans-translator</context-root>
+    </web>
+  </module>
+  <module>
+    <ejb>webbeans-translator.jar</ejb>
+  </module>
+</application>]]></programlisting>
+      </tip>
+      
+      <para>
+         Next, lets look at the war. Just as in the numberguess example, we have
+         a <literal>faces-config.xml</literal> (to enabled Facelets) and a 
+         <literal>web.xml</literal> (to enable JSF and attach Web Beans services
+         to the servlet container) in <literal>WebContent/WEB-INF</literal>.
+      </para>
+      
+      <para>
+         More intersting is the facelet used to translate text. Just as in
+         the numberguess example we have a template, which surrounds the form
+         (ommitted here for brevity):
+      </para>
+      
+      <programlisting role="XML"><![CDATA[<h:form id="NumberGuessMain">
+            
+   <table>
+      <tr align="center" style="font-weight: bold" >
+         <td>
+            Your text
+         </td>
+         <td>
+            Translation
+         </td>
+      </tr>
+      <tr>
+         <td>
+            <h:inputTextarea id="text" value="#{translator.text}" required="true" rows="5" cols="80" />
+         </td>
+         <td>
+            <h:outputText value="#{translator.translatedText}" />
+         </td>
+      </tr>
+   </table>
+   <div>
+      <h:commandButton id="button" value="Translate" action="#{translator.translate}"/>
+   </div>
+   
+</h:form>]]></programlisting>
+
+      <para>
+         The user can enter some text in the lefthand textarea, and hit the 
+         translate button to see the result to the right.
+      </para>
+      
+      <para>
+         Finally, let's look at the ejb module, 
+         <literal>webbeans-translator-ejb</literal>.
+         There are two configuration files in 
+         <literal>src/main/resources/META-INF</literal>, an empty
+         <literal>web-beans.xml</literal>, used to mark the archive as 
+         containing Web Beans, and <literal>ejb-jar.xml</literal>. Web Beans
+         provides injection and initializtion services for all EJBs, and uses 
+         <literal>ejb-jar.xml</literal> to enable this, you'll need this in any
+         EJB project which uses Web Beans:
+      </para>
+      
+      <programlisting role="XML"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
+<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" 
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
+         version="3.0">
+         
+   <interceptors>
+     <interceptor>
+       <interceptor-class>org.jboss.webbeans.ejb.SessionBeanInterceptor</interceptor-class>
+     </interceptor>
+   </interceptors>
+   
+   <assembly-descriptor>
+      <interceptor-binding>
+         <ejb-name>*</ejb-name>
+         <interceptor-class>org.jboss.webbeans.ejb.SessionBeanInterceptor</interceptor-class>
+      </interceptor-binding>
+   </assembly-descriptor>
+   
+</ejb-jar>]]></programlisting>
+
+      <para>
+         We've saved the most interesting bit to last, the code! The project has
+         two simple beans, <literal>SentanceParser</literal> and
+         <literal>TextTranslator</literal> and two enterprise beans,
+         <literal>TanslatorControllerBean</literal> and 
+         <literal>SentenceTranslator</literal>. You should be getting quite
+         familiar with what a Web Bean looks like by now, so we'll just 
+         highlight the most interesting bits here.
+      </para>
+      
+      <para>
+         Both <literal>SentanceParser</literal> and 
+         <literal>TextTranslator</literal> are dependent beans, and 
+         <literal>TextTranslator</literal> uses constructor initialization:
+      </para>
+      
+      <programlisting role="JAVA"><![CDATA[public class TextTranslator { 
+   private SentenceParser sentenceParser; 
+   private Translator sentenceTranslator; 
+   
+   @Initializer
+   TextTranslator(SentenceParser sentenceParser, Translator sentenceTranslator) 
+   { 
+      this.sentenceParser = sentenceParser; 
+      this.sentenceTranslator = sentenceTranslator;]]></programlisting>
+      
+      <para>
+         <literal>TextTranslator</literal> is a stateless bean (with a local 
+         business interface), where the magic happens - of course, we couldn't 
+         develop a full translator, but we gave it a good go!
+      </para>
+      
+      <para>
+         Finally, there is UI orientated controller, that collects the text from
+         the user, and dispatches it to the translator. This is a request 
+         scoped, named, stateful session bean, which injects the translator.
+      </para>
+      
+      <programlisting role="JAVA"><![CDATA[@Stateful
+ at RequestScoped
+ at Named("translator")
+public class TranslatorControllerBean implements TranslatorController
+{
+   
+   @Current TextTranslator translator;]]></programlisting>
+   
+      <para>
+         The bean also has getters and setters for all the fields on the page.
+      </para>
+      
+      <para>
+         As this is a stateful session bean, we have to have a remove method:
+      </para>
+      
+      <programlisting role="JAVA"><![CDATA[   @Remove
+   public void remove()
+   {
+      
+   }]]></programlisting>
+   
+      <para>
+         The Web Beans manager will call the remove method for you when the bean
+         is destroyed; in this case at the end of the request.
+      </para>
    </section>
+   
+   <para>
+      That concludes our short tour of the Web Beans RI examples. For more on 
+      the RI, or to help out, please visit 
+      <ulink url="http://www.seamframework.org/WebBeans/Development">http://www.seamframework.org/WebBeans/Development</ulink>.
+   </para>
+   
+   <para>
+      We need help in all areas - bug fixing, writing new features, writing
+      examples and translating this reference guide.
+   </para>
 
 </chapter>
\ No newline at end of file

Modified: ri/trunk/examples/numberguess/WebContent/home.xhtml
===================================================================
--- ri/trunk/examples/numberguess/WebContent/home.xhtml	2008-12-19 19:33:21 UTC (rev 597)
+++ ri/trunk/examples/numberguess/WebContent/home.xhtml	2008-12-19 20:13:49 UTC (rev 598)
@@ -10,7 +10,7 @@
        <h1>Guess a number...</h1>
        <h:form id="NumberGuessMain">
           <div style="color: red">
-             <h:messages id="messages" globalOnly="true"/>
+             <h:messages id="messages" globalOnly="false"/>
              <h:outputText id="Higher" value="Higher!" rendered="#{game.number gt game.guess}"/>
              <h:outputText id="Lower" value="Lower!" rendered="#{game.number lt game.guess}"/>
           </div>
@@ -26,10 +26,6 @@
              </h:inputText>
             <h:commandButton id="GuessButton" value="Guess" action="#{game.check}"/>
           </div>
-   
-          <div>
-             <h:message id="message" for="inputGuess" style="color: red"/>
-          </div>
        </h:form>
     </ui:define>
   </ui:composition>

Deleted: ri/trunk/examples/translator/webbeans-translator-war/WebContent/WEB-INF/web-beans.xml
===================================================================




More information about the weld-commits mailing list