Adam Bien's Weblog

Simplest Possible JSF 2 / EJB 3.1 / JPA Component - With WAR Deployment

The welcome.xhtml page uses facelets templating mechanism. It mainly consists of a textbox and a button:

         <ui:define name="body">
                    <h:outputLabel value="Message:"/><h:inputText value="#{
                    <h:commandButton action="#{}" value="Save"/>

The InputText and the Button are value-bound to a @ManagedBean with the name MessageView:

public class MessageView {
    MessageService messageService;

    private HelloMessage message;

    public MessageView() {
        this.message = new HelloMessage();

    public HelloMessage getMessage() {
        return message;

    public int getNumberOfMessages(){
        return messageService.getMessages().size();

    public String save(){;
        return "theend";

The method save() returns "theend" String. This is the name of the next view. No page flow definitions in faces-config.xml are required. The MessageView managed bean instantiates and directly exposes the HelloMessage entity:

@NamedQuery(name=HelloMessage.findAll,query="SELECT hm from HelloMessage hm")
public class HelloMessage{
    public final static String findAll = "";

    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String message;

    public HelloMessage() {

    public String getMessage() {
        return message;

    public void setMessage(String message) {
        this.message = message;

The actual business logic is implemented in a @Stateless no-interface view EJB 3.1:

public class MessageService {
    EntityManager em;

    public void save(HelloMessage hm){

    public List<HelloMessage> getMessages(){
        return this.em.createNamedQuery(HelloMessage.findAll).getResultList();

The MessageService manages the EntityManager and cares about transactions. Interestingly: the overall amount of code can be reduced with the introduction of a single EJB 3.1. You can use JSF 2 without EJBs, but then you will have to manage the persistence and transactions manually - what will result in significantly more code. You could expose the same EJB 3.1 as a RESTful service.

The whole example (LeanJSF2EJB31Component) was checked-in into: It was developed with Netbeans 6.8m1 and deployed to Glassfishv3b57.

Btw. the slowest "deployment" (with creation of the table) was: INFO: Deployment of LeanJSF2EJB31Component done is 895 ms

[The whole book "Real World Java EE Patterns - Rethinking Best Practices" describes lean Java EE architectures and patterns. See ServiceFacade, Service, PDO patterns and the chapter 6 "Pragmatic Java EE Architectures", Page 253]

NEW workshop: HTML 5 and JavaScript Essentials, Three days in April 2017: from Java EE 7 Architectures over Microservices to Performance, Troubleshooting and Monitoring
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.


A book about rethinking Java EE Patterns


Adam, who else is supporting ejb3.1 presently?

Posted by rb on August 28, 2009 at 01:22 PM CEST #


JBoss (, openEJB:

Not sure about the others. You could probably expect of EJB 3.1 in Websphere around late 2020 :-),



Posted by Adam Bien on August 28, 2009 at 02:19 PM CEST #

There's a plugin for WAS but I couldn't for the life of me get it to work, so after about 2 weeks of headaches I went back to Glassfish and 'accepted' it's bugs in order to get the most out of EJB3.1.

Just built my first JSF2.0.2 EJB3.1 app and it was as easy as, so little code needed to get the basic framework up and running!

For me JSF2.0 has just gave Struts & SpringMVC a big slap!


Posted by kps on February 02, 2010 at 02:58 PM CET #


I found your WebSphere/2020 comment while doing some other searching.

Say what you will about IBM (and I have in the past as well), they are actually becoming quite agile with the most recent versions. Java EE 6 has clearly quickened the pace of change in Java EE but I don't think WebSphere will be left in the dust.

An early version of WebSphere 8.0 is actually already available.

Granted this is not a supported version but neither is JBoss 6 right now.

It will be interesting next year to start working with WebSphere shops that are going from WebSphere 6.1 (J2EE 1.4) to WebSphere 8.0 (Java EE 6). They will feel like a kid in a candy store!!

WebSphere 7.0 actually beat JBoss 5 to the Java EE 5 certification list. If you compare WebSphere 7.0 to the SUPPORTED JBoss 5.0 EAP there was a difference of more than a year in the availability of WebSphere 7.0 before JBoss 5.0 EAP.

The big question is going to be will WebSphere 8.0 beat the SUPPORTED version of JBoss 6.0 EAP and make it 2 in a row.

If it does, perhaps the "corporate world" will finally realize that Glassfish is the proper king of the open source market.

Of course that will happen just in time for Oracle to ruin it by trying to morph Glassfish into WebLogic...

Exciting times,
Stuart Smith
Java and Administration Lead
Web Age Solutions

PS. I do realize the WebSphere/2020 comment was probably a joke and I do love your book on Java EE 6 patterns (still one of the only resources on the topic) so no worries here :)

Posted by Stuart Smith on June 09, 2010 at 10:43 AM CEST #

What is EJB 3.1 specific in this example? Wouldn't JavaEE 5 (EJB 3 and JSF 1.2) sufficient? Thanks for clarification.

Posted by MP on August 06, 2011 at 04:39 PM CEST #

In a JEE application with JSF as the web implementation running in Websphere, can you tell me if by placing the domain logic in POJOs instead of using EJBs is an ideal design choice? Also using the web tier to access the service layer is correct?

Posted by Kuldip bajwa on February 05, 2012 at 07:09 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments