Simple CRUD Example – Part 2

In the last article I showed you how to create a Debry database and set up a persistence unit. In this article I’ll create a very basic CRUD application. Using JSF 2, JPA 2 and EJB 3.1.

At this point you should have a standard Maven web application with the only modification being the persistence.xml file we created in the previous article. The next step is to create some additional settings files that pretty much all web applications need.

Creating the Settings Files

With the advent of Java EE 6 most of the settings files have become optional but in my experience they are always necessary even if you actually put very little in them. It’s disappointing to see that Maven doesn’t create them for you any more.

The web.xml File

The first one to create is the web.xml file which lives under the WEB-INF directory. This file contains settings for the web application such as context parameters and listeners for server state etc. It also controls security, filters and a whole host of other things that we won’t be usign here. The default Maven web application doesn’t (for some unknown reason) configure your application to use JSF from the start so right click on the application node and select ​Properties​. Under the frameworks section click Add and select JavaServer Faces version 2.0 (the version used by the application server). The ​web.xml​ file should be created automatically along with a file called index.xhtml.

​The glassfish-web.xml File

You should also create the glassfish-web.xml ​file while you are here. This is easily done by selecting the ​WEB-INF​ directory. Right clicking and selecting New then Other. Scroll down the left list until you see GlassFish and then create a new GlassFish Descriptor.

The beans.xml File

Finally create the ​beans.xml​ file which tells the system that you will be using CDI. This file can be completely empty but it will trigger a warning. It is better to populate it as shown here.

You now have a fully set up web application.

The Front End

At this point I’m going to break with tradition with this type of tutorial and tell you to install PrimeFaces 3 (or later). The reason for doing this is simple, you won’t be using the basic JSF components in your applications – they are very limited – you’ll be using some other widget library and PrimeFaces is probably about the best out there at the moment. It’s also no more complex to use PrimeFaces than it is to use the basic widgets so there is no good reason to limit your options. Right click on the Dependencies node of your application and add PrimeFaces 3, you may need to set up your Maven installation so that it can locate the PrimeFaces repository as currently they aren’t available in the usual locations.

At a minimum we need to create four more files to make this a crud application. Three of the files are .java and one is .xhtml which contains JSF markup. We’ll start with the JSF page that will display the entities we create and let us modify them. I’m not planning on straying too far from the well beaten path of making an application that modifies People objects – sorry.

Open the index.xhtml file and enter the following contents:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
	  xmlns:f="http://java.sun.com/jsf/core"
	  xmlns:ui="http://java.sun.com/jsf/facelets"
	  xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>CRUD Example</title>
    </h:head>
    <h:body>
		<h:form>
			<p:dataTable var="p" value="#{personView.people}">
				<p:column>
					<f:facet name="header">Name</f:facet>
					#{p.name}
				</p:column>
			</p:dataTable>
		</h:form>
    </h:body>
</html>

The Back End

Now create the following class files.

Person.java

package wobblycogs.crudexample;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Person implements Serializable {

	@Id
	private Long id;
	private String name;

	public Person() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}

PersonView.java

package wobblycogs.crudexample;

import java.util.List;
import javax.enterprise.context.RequestScoped;

@RequestScoped
public class PersonView {
	
	private List<Person> people;
	private Person selected;
	
	public PersonView() {
	}

	public List<Person> getPeople() {
		return people;
	}

	public void setPeople(List<Person> people) {
		this.people = people;
	}

	public Person getSelected() {
		return selected;
	}

	public void setSelected(Person selected) {
		this.selected = selected;
	}
}

PersonService.java

package wobblycogs.crudexample;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PersonService {
	
	@PersistenceContext
	private EntityManager em;

	public PersonService() {
	}
}

The Person class is the entity that the system will be manipulating. The PersonView is a CDI Bean that is used to handle the front end interaction and the PersonService is a stateless EJB used to control the business logic.

At this point if you fire the application up you’ll see that it automatically creates tables in the database and if you visit the index.xhtml page you’ll see an empty table. In the next article I’ll get the application doing something interesting.