[jboss-user] [JBoss Seam] - Re: HowTo: When going to a page to enter data, pull from db

CptnKirk do-not-reply at jboss.com
Sat Apr 21 04:13:23 EDT 2007


It's late, but I'll give this a go.  Christian or Pete feel free to jump in.  It sounds like your mixing your retrieval and persistence mechanisms.

@Factory - Method called when a JSF page tries to reference a contextual variable that doesn't exist, but has a named factory method.  So if #{activity} isn't found and there is a @Factory method, the method will be called and the contextual variable populated.  The variable will have the scope defined by @Factory or if absent, the scope of the enclosing component.  @Factory is only called if the contextual variable is null (needs to be created).

@Unwrap - Method called on a component that allows the component to manage the creation of a contextual variable.  So Component ActivityManager might have an @Unwrap method that puts an Activity into scope referenceable by the EL #{activity}.  This method will be called every time the #{activity} variable is referenced or injected into a bean.  Because the @Unwrap annotation is on a method within a Seam Component, the component can take advantage of the entire Seam life cycle, including providing tear down logic.

EntityHome - These are classes the provide generic DAO capabilities to objects.  By using an EntityHome you can Create, Update, Delete a single entity via its primary key.  In order to Update or Delete with an EntityHome you must pass the ID of the object to the EntityHome prior to calling update or remove.

EJB3 Entity Beans - Seam components that are EJB3 Entity Beans are automatically registered as event scoped components.  They have an implicit auto-create when used with JSF pages.  So you could @Name("activity") your Activity EJB3 entity bean.  And reference it in your JSF page as #{activity}.  Seam will call the default constructor and you'll be able to use EL to set values and can inject @In("activity") into the controller that deals with the form action.  Beans can have multiple names, and multiple scopes.  Read more about the @Role/@Roles annotation for more information. 

The Seam documentation covers these concepts in greater detail.

If all you want to do is supply simple CRUD operations, then you can use an EntityHome.  By using an EntityHome it becomes your controller (action method), and thus you don't have to write one.  EntityHome can be configured with FacesMessages to notify your users that CRUD operations have completed.  Remember that in order to use EntityHome for update and remove you need to pass in an id.  The developer guide shows how.

Based on the description you can decide whether @Factory or @Unwrap are best suited for your needs.

Also, by default JSF will populate an unfilled form with empty values, not null.  So if you're trying to populate your #{activity} entity, have a blank form and hit submit.  Your controller will be injected with a live object with the form values set to their empty state (empty Strings, 0 ints, etc).  If you want these values to be null instead you'll need to write a custom converter.  I've written a simple one below.  Use the converter method or the <f:converter> tag.  The version of MyFaces that ships with JBoss 4.0.5 doesn't seem to want to let you override the default String converter, so you'll need to specify the converter on every input component you'd like it on.  This might change on the 1.2 RI, I've heard different things about the RI in the 4.2 CVS tree.

@Name("emptytonull")
  | @org.jboss.seam.annotations.jsf.Converter(forClass=String.class)
  | public class EmptyStringToNullConverter implements Converter, Serializable
  | {
  |     public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException
  |     {
  |         return arg2 == null || arg2.isEmpty() ? null : arg2;
  |     }
  | 
  |     public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException
  |     {
  |         return arg2 == null ? "" : arg2.toString();
  |     }
  | }

Hope this helps.  I'm going to crash now.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4039504#4039504

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4039504



More information about the jboss-user mailing list