combining CDI producer methods with EL

CDI’s Producer methods are nice to expose any sort of class (e.g. legacy or 3rd party (JDK)). Combining the @Produces with the @Named you can reuse the result in your XHTML JSF page.

Some Java code:

...
@Produces @Named public MyBean getMyBeanImpl()
{
....
return someBean;
}

Now the XHTML can have something like:

...
<h:outputText value="#{myBeanImpl.someOfItsProperties}" />
...

If you want, you can use the @Named with a named value (e.g. @Named(“fooBar”)). Now the fooBar also is required by the EL.

Pretty nice!

Howdy!

Posted in ajax, apache, CDI, facelets, guice, java, jsf, myfaces, seam, WebBeans
One comment on “combining CDI producer methods with EL
  1. Yeah, it’s cool. And useful too. I’m using it in my MyFaces 2.0 demo app, like this:
    public class Utils {
    @Produces @Named public String getBasePath() {
    return FacesContext
    .getCurrentInstance()
    .getExternalContext()
    .getRequestContextPath();
    }
    }

    And then in my Facelet, I can do:

    But on the other hand, this can become dangerous stuff, because you don’t know where the objects come from…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: