September 16, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Introducing JBoss(r) Seam

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

Listing 4.Gadget Entry JSF Page

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<html>
<head>
   <title>Add a Gadget</title>
</head>

<body>
   <f:view>
      <h:form>
         <table border="0">
            <tr>
               <td>Description: </td>
               <td>
                  <h: inputText value="#{gadget.description}"
                                required="true" />
               </td>
            </tr>
            <tr>
               <td>Type: </td>
               <td>
                  <h:selectOneMenu value="#{gadget.type}"
                                   required="true">
                     <f:selectItems
                        value="#{gadgetAdmin.gadgetTypes}" />
                  </h: selectOneMenu>
               </td>
            </tr>
         </table>
         <h:commandButton type="submit" value="Create"
                          action="#{gadgetAdmin .newGadget}" />
      </h: form>
   </f: view>
</body>
</html>

This page generates a simple entry form that prompts the user for a type and description for a new gadget for the catalog. The description field is a simple text entry field, while the type is a drop-down menu populated with allowed values from the GadgetTypes enumeration. Both fields are bound to properties on a new managed bean named "gadget". At the end of the form is a submit button that invokes the newGadget () operation on the gadgetAdmin managed bean.

At this point, as with any JSF application, we need to wire the JSF managed beans to classes in our model. We could try to associate the gadgetAdmin bean with an instance of our GadgetAdminBean session EJB and the gadget bean to our GadgetBean entity EJB, using entries in our faces-config.xml like this:

<faces-config>
   <managed-bean>
      <managed-bean-name>gadget</managed-bean-name>
      <managed-bean-class>GadgetBean</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
   </managed-bean>

   <managed-bean>
      <managed-bean-name>gadgetAdmin</managed-bean-name>
      <managed-bean-class>GadgetAdminBean</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
   </managed-bean>
</faces-config>

But what you'll find is that this won't work, at least not the way you'd expect. In JSF, managed beans are expected to be simple JavaBeans, and they'll be managed that way at runtime by the JSF container. When the gadget or gadgetAdmin beans are created and used at runtime, the JSF container won't follow the rules for EJB components when handling them. It won't, for example, use the EJB container to obtain an instance of the GadgetAdminBean, as you should for any session bean. Instead, it will try to construct instances of GadgetAdminBean directly, outside of the EJB container and all of its services. This obviously isn't what we want—we defined GadgetBean and GadgetAdminBean components because we wanted them to use the persistence and transaction manage­ment services of the EJB container.

In order to integrate our EJB components into our JSF pages, we need to create JavaBean wrappers that the JSF components can use directly. These JavaBean wrappers will then interact with the EJB components to get the gadget persistence done.

First, we'll have to write a simple JavaBean version of our GadgetBean entity EJB that can be used as the gadget managed bean. This JavaBean class, Gadget, is shown in Listing 5.

Listing 5. Simple JavaBean for Gadget Values

public class Gadget {
   private String mDescription;
   private GadgetType mType;

   public String getDescription() { return mDescription; }
   public void setDescription(String desc) { mDescription = desc; }

   public String getType() { return (mType != null ?
                                     mType.name() : null);
   public void setType(String t) { mType = GadgetType.valueOf(t); }
}

This JavaBean just carries the value of the gadget's properties between the JSF in the UI and the GadgetBean in the EJB container. It has the same two properties as our GadgetBean entity EJB, naturally.

Now, we need a JavaBean façade for our GadgetAdminBean session EJB. We'll be using this bean to implement JSF actions, so we call it "GadgetAdminAction". The code for this wrapper is shown in Listing 6.

Listing 6. Action Wrapper for GadgetAdminBean

public class GadgetAdminAction {
   @EJB
   private IGadgetAdminBean mGadgetAdmin;

   // Managed property for our JSF action, populated with the
   // Gadget being operated in the current request
   // (added/deleted/edited)
   private Gadget mGadget;
   public Gadget getGadget() { return mGadget; }
   public void setGadget(Gadget g) { mGadget = g; }

   /** Facade to the newGadget operation on the GadgetAdminBean */
   public String newGadget() {
      // Convert the Gadget into a GadgetBean and persist it
      GadgetBean bean = gadgetToBean(mGadget);
      mGadgetAdmin . newGadget(bean);
      return "success";
   }

   /** Facade to the getAllGadgets operation on the GadgetAdminBean */
   public List<Gadget> getAllGadgets() {
      List<Gadget> gadgets = new ArrayList<Gadget>();
      List<GadgetBean> beans = mGadgetAdmin .getAllGadgets ();
      Iterator i = beans.iterator();
      while (i.hasNext()) {
         Gadget g = beanToGadget((GadgetBean)i.next());
         gadgets.add(g);
      }

      return gadgets;
   }

   public Map<String,String> getGadgetTypes() {
      Map<String,String> types = new HashMap<String,String>();
      for (GadgetType value : GadgetType.values()) {
         types.put(value.label(), value.name());
      }
      return types;
   }

   /** Convert a Gadget JavaBean to a GadgetBean EJB */
   private GadgetBean gadgetToBean(Gadget g) {
      GadgetBean bean = new GadgetBean();
      bean.setDescription (g.getDescription ());
      bean.setType(g.getType());
      return bean;
   }

   /** Convert a GadgetBean EJB to a Gadget JavaBean */
   private Gadget beanToGadget(GadgetBean g) {
      Gadget ig = new Gadget();
      ig.setDescription(g.getDescription());
      ig.setType(g.getType());
      return ig;
   }
}




Page 4 of 6



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel