October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Introducing JBoss(r) Seam

  • December 7, 2007
  • By Jim Farley
  • Send Email »
  • More Articles »

The GadgetAdminAction wrapper bean does two things: it converts GadgetBean entity EJBs into Gadget JavaBeans for the JSF components with the beanToGadget () utility method and also converts Gadget beans from the JSF beans back into GadgetBean EJBs to persist them with the gadgetToBean() utility method. It also has a method for each operation on the GadgetAdminBean that we want to invoke as actions. The getAllGadgets () method invokes the GadgetAdminBean.getAllGadgets () method and converts the list of GadgetBean references into Gadget instances. The newGadget() method takes the managed Gadget property and converts it into a new GadgetBean, and then passes it to the GadgetAdminBean.newGadget () method to be persisted.

Finally, we can wire these JavaBean wrappers into the JSF UI as managed beans, using the faces-config.xml file shown in Listing 7.

Listing 7. JSF faces-config.xml for Java EE Gadget Catalog

<faces-config>
   <managed-bean>
      <managed-bean-name>gadget</managed-bean-name>
      <managed-bean-class>Gadget</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
   </managed-bean>
   <managed-bean>
      <managed-bean-name>gadgetAdmin</managed-bean-name>
      <managed-bean-class>
         GadgetAdminAction
      </managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      <managed-property>
         <property-name>gadget</property- name>
         <value>#{gadget}</value>
      </managed-property>
   </managed-bean>

   <navigation-rule>
      <navigation-case>
         <from-outcome>success</from-outcome>
         <to-view-id>/listGadgets.jsf</to-view-id>
      </navigation-case>
      <navigation-case>
         <from-action>addGadget</from-action>
         <to-view-id>/addGadget.jsf</to-view-id>
      </navigation-case>
   </navigation-rule>
</faces-config>

Notice that, in addition to using the Gadget JavaBean as the gadget managed bean and GadgetAdminAction as the gadgetAdmin managed bean, we've also defined the pageflow in a navigation-rule section. Any "success" outcome should take the user back to the gadget list page, and the addGadget action we referenced in the link on the listGadget.jsp page should take the user to the addGadget.jsp JSF page.

The Gadget Catalog with JBoss Seam

The last few steps that we were forced to carry out in creating the plain Java EE version of the Gadget Catalog provide some of the motivation for the simplifications introduced in JBoss Seam. If you look back at the previous section, you'll notice that we had all the required persistence and UI functionality for the Gadget Catalog defined as of Listing 4. Everything that follows in the section is just overhead that's needed in order to bridge from the JSF components to the EJB components, and to configure everything. This "overhead" includes the following:

  • The Gadget and GadgetAdminAction JavaBean wrapper classes
  • The managed bean declarations in the faces-config.xml

The JBoss Seam project team saw these steps as wasted effort to be eliminated. Creating the JavaBean wrappers to integrate the JSF and EJB component models doesn't add anything to the functionality of the Gadget Catalog; it's just an implementation detail required because Java EE doesn't provide its own bridge between these two component models. And their philosophy on the managed-bean entries in the faces-config.xml file is that these represent missing code annotations in the JSF model. In EJB, virtually all of the details in the ejb-jar deployment descriptors can (optionally) be replaced with code annotations in EJB 3. Why not give JSF programmers the same option with all those faces-config.xml options?

JBoss Seam eliminates both of these issues, making our Gadget Catalog simpler to implement when we use a Java EE server enhanced with Seam. First off, we can use our GadgetBean and GadgetAdminBean EJB components directly as managed beans within JSF pages. We no longer need the JavaBean wrapper classes, because Seam provides a bridge between the JSF and EJB component models.

Second, Seam provides a @Name annotation that can be inserted into our EJBs to specify their JSF managed bean names. Our updated GadgetBean EJB is shown in Listing 8.

Listing 8. Seam-Enhanced GadgetBean Entity EJB

@Entity
@Table (name= "GADGET")
@Name( "gadget")
public class GadgetBean implements Serializable {
   private String mDescription = "";
   private String mType = "";

   public GadgetBean() { }

   @Id
   @Column (name="DESCR")
   public String getDescription() { return mDescription; }
   public void setDescription(String desc) {mDescription = desc; }

   @Id
   @Column (name="TYPE")
   public String getType() { return mType; }
   public void setType(String t) { mType = t;}
}

The only difference in the Seam-enabled version is the @Name ("gadget") annotation at the start of the class. This annotation is equivalent to the "gadget" managed-bean entry in the faces-config.xml file shown in Listing 7. We can eliminate the "gadgetAdmin" entry as well if we put a corresponding @Name annotation on the GadgetAdminBean EJB class.

In summary, Seam has helped us to significantly simplify the implementation of our little application. There are fewer objects in our model, and the relationship between the UI views and the objects in the model makes more sense.





Page 5 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel