Adam Bien's Weblog

Thursday Aug 13, 2009

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:form>
                    <h:outputLabel value="Message:"/><h:inputText value="#{
messageview.message.message}"/>
                    <h:commandButton action="#{messageview.save}" value="Save"/>
                </h:form>
            </ui:define>

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

@ManagedBean(name="messageview")
@RequestScoped
public class MessageView {
    @EJB
    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(){
        this.messageService.save(message);
        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:

@Entity
@NamedQuery(name=HelloMessage.findAll,query="SELECT hm from HelloMessage hm")
public class HelloMessage{
    public final static String findAll = "com.abien.leancomp.business.message.entity.HelloMessage.findAll";

    @Id
    @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:

@Stateless
public class MessageService {
    @PersistenceContext
    EntityManager em;

    public void save(HelloMessage hm){
        this.em.persist(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: http://kenai.com/projects/javaee-patterns/. 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]


Special Event: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014

A book about rethinking Java EE Patterns

Comments:

Adam, who else is supporting ejb3.1 presently?

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

Rb,

JBoss (https://jira.jboss.org/jira/browse/EJBTHREE-1579), openEJB: http://openejb.apache.org/openejb-31.html

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

thanks,

adam

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!

:-D

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

Adam,

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.
https://www14.software.ibm.com/iwm/web/cc/earlyprograms/websphere/wsasoa/

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
realworldpatterns.com
...the last 150 posts
...the last 10 comments
Links
License