On the topic of general JSF design patterns (particularly related to
managed beans), has anyone noticed that the managed beans mechanism
implements a simple Dependency Injection framework, using Setter
Injection to inject the dependencies?
Consider the following code from the Shale Use Cases example app
(commented out in the current code because I'm illustrating the
transparent use of Spring's bean factory, but it works fine with pure
JSF):
<managed-bean>
<description>
Business logic used to manipulate registered user
profile information, and authenticate logons.
</description>
<managed-bean-name>logon$logic</managed-bean-name>
<managed-bean-class>
org.apache.shale.usecases.logic.LogonLogic
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>dao</property-name>
<property-class>org.apache.shale.usecases.model.UsersDAO</property-class>
<value>#{logon$users}</value>
</managed-property>
</managed-bean>
<managed-bean>
<description>
Data Access Object (DAO) used to manipulate registered
user profile information.
</description>
<managed-bean-name>logon$users</managed-bean-name>
<managed-bean-class>
org.apache.shale.usecases.model.minimal.MinimalUsersDAO
</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
Now, the event hander for the login button can acquire a reference to
a business logic object, preconfigured with the appropriate DAO, like
this:
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding vb =
context.getApplication.createValueBinding("#{logon$logic});
LogonLogic logic = (LogonLogic) vb.getValue(context);
User user = logic.authenticate(username, password);
(Shale provides a convenience helper method that combines the first
three lines into one, but this is the code that is actually executed
under the covers.)
As configured, the DAO object is an appication singeton (because it is
stored in application scope), and a new logic instance is created for
each request. If your logic classes are stateless as well (this one
is, but that won't always be the case), you can make them application
singetons as well simply by changing the scope entry for
"logon$logic".
If you want to get a closer look at the code using this, follow the
download links on:
http://wiki.apache.org/struts/StrutsShale
Craig McClanahan
On Apr 8, 2005 2:47 AM, Slawek <[EMAIL PROTECTED]> wrote:
> excellent thraed gentleman!
> give us more...
>
> though faces technology implements many design patterns (j2ee and common),
> there is still "semantic hole" between language and real world domain...
>
> i think that sommetimes many of us feel that we are doing our job in
> "dirty" way;)
>
> havin somme tutoral called "faces desing patterns" (or sth.) would be very
> helpfull (and writing such a book would be more profit than coding boring
> business apps:P)
>
> Slawek
>
> > Thank you all for your input. I appreciate your
> > ideas.
> >
> > Ray
> >
> > --- Enrique Medina <[EMAIL PROTECTED]> wrote:
> >> Shall MyFaces offer this functionality in the
> >> future? At the moment,
> >> I'm watching WebFlow from Spring to see whether it
> >> provides JSF
> >> support...
> >>
> >> On Apr 7, 2005 6:07 PM, Heath Borders
> >> <[EMAIL PROTECTED]> wrote:
> >> > I'm talking about the wizards that my team is
> >> developing.
> >> >
> >> >
> >> >
> >> > On Apr 7, 2005 11:03 AM, Jonathan Eric Miller
> >> <[EMAIL PROTECTED]> wrote:
> >> > > You mean the wizard that you developed on your
> >> own, or, is this
> >> > > functionality that MyFaces offers? I think
> >> someone said that Shale offers
> >> > > this functionality. I still haven't had a chance
> >> to look at Shale yet
> >> > > although I've been meaning to. I wonder if this
> >> functionality will every
> >> > be
> >> > > integrated into JSF?
> >> > >
> >> > > Jon
> >> > >
> >> > > ----- Original Message -----
> >> > > From: "Heath Borders" <[EMAIL PROTECTED]>
> >> > > To: "MyFaces Discussion"
> >> > <[email protected]>
> >> > > Sent: Thursday, April 07, 2005 10:55 AM
> >> > > Subject: Re: Managed Bean OO Design question
> >> > >
> >> > > With our wizard pages, the user is not allowed
> >> to skip a page if that page
> >> > > has required input. This is controlled by JSF.
> >> If there is an optional
> >> > page,
> >> > > the user can skip it. If there are many optional
> >> pages our wizards have a
> >> > > summary page that shows the changes before they
> >> make them. This way, the
> >> > > user has the opportunity to go back and make
> >> changes.
> >> > >
> >> > >
> >> >
> >> >
> >> >
> >> > --
> >> > -Heath Borders-Wing
> >> > [EMAIL PROTECTED]
> >>
> >
> >
> >
> > __________________________________
> > Yahoo! Messenger
> > Show us what our next emoticon should look like. Join the fun.
> > http://www.advision.webevents.yahoo.com/emoticontest
> >
>
>