<- Part1. Our goal is develop an application called the "Music Event Platform". On this platform you can submit music events by their name and get an overview of all music events submitted. The Platform is reachable through an Web Interface and a REST API.
The project structure has already been constructed by Netbeans, so we don't need to do much there. Also worth mentioning is that Netbeans deploys the application with each save. First I will describe the Frontend, which consists of "welcomePrimefaces.xhtml", "showEvents.xhtml" and "WelcomeController.java". The platforms entry point is the sample page "welcomePrimefaces.xhtml", which needs to be modified. We delete the body and write a form to enter the name of an event an submit it. The name is transmitted to the Java file which backs the JSF and then is sent to the backend by clicking the button. The Frontends code looks as follows:
As you can see
save() from
WelcomeController.java is called by clicking the button, because the buttons
action attribute points to the method. All tags in the namespace / beginning with
p: are Primefaces components,
<p:panelGrid> is defining the layout, the others should be self-explaining. For using them the namespace should be included over the html tag. The
WelcomeController.java class is annotated with
@Named, which allows us the refer to it with
welcomeController from the pages without any further configuration. The class also includes a Enterprise Java Bean
@EJB which is a standard component within JEE and is managed through the container (e.g. you don't need to initialize it). EJBs are never connected directy to the frontend, they are placed on the server and contain Business Logic. The most significant characteristic is, if they are stateful or stateless (read more: http://www.oracle.com/technetwork/java/ejb-141389.html).
Over a "Show Events"-link the page "showEvents.xhtml" is shown, it contains all the events submitted:
JSF Components can directly compute several Java types, e.g. Lists, which is done here by a
<p:data
List>. It lists all results retrieved from
public List<MusicEvent> getMusicEvents() in the
WelcomeController.java. All the calls concerning data are forwarded to the backend, which is explained in the following.
It consists of
MusicEventsBackend.java (the EJB)
, MusicEventsREST.java and
MusicEvent.java. The EJB manages the interaction and calls the
EntityManager to add or get
MusicEvent.java , which is an EntityBean, i.e.
serializable and normally directly mapped to a database relation (read more: http://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.html).
MusicEventsREST.java implements the REST API and is build upon the backend
. Netbeans automatically detects the implementation of a RESTful Webservice and guides you step by step through the configuration needed.
The sources look as follows:
The important part of the Entity MusicEvent (the rest will be generated) and the RESTful Webservice MusicEventsREST:
The Web Frontend then looks like:
Now we can add music events over the form and they are added to the database. Comments and Questions are welcome. Also watch out for a Technology comparison on this blog providing pros and cons of JEE with Primefaces.