Thomas has posted 2 posts at DZone. View Full User Profile

Spring Web Flow CRUD Tutorial

  • submit to reddit

I'm currently studying Spring Web Flow (SWF). I want to share my experience in creating a simple CRUD Web application.

Technologies used:
  • JPA 1.0
  • Spring Framework 2.5
  • JSF 1.2
  • Facelets 1.1
  • Spring Web Flow 2.0
  • Maven 2 for build tool
  • Any IDE you want (I use Eclipse and IntelliJ for this tutorial)

I know it's overwhelming for a simple project, but don't worry, everything should be easy :-)

You can download the project here. The following are some information about the project.

1. Domain model

The tutorial has a Person class, this is the only model we're dealing with. Since we'll be using JPA, we need to add suitable annotations. Don't forget to implement Serializable, it's required for SWF. Here's the final Person class (package name, imports, getters and setters are removed for brevity):

@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"first"})})
public class Person implements Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private Integer version;

private PersonName name = new PersonName();

private String email;

private int workExperience;

private Boolean female;

private MaritalStatus status;

As you can see from the code above, the tutorial demonstrates the use of embedded class (PersonName) and enumeration (MaritalStatus).

2. Data access and service
public class PersonServiceImpl implements PersonService {
private EntityManager em;

public void setEntityManager(EntityManager em) {
this.em = em;

@Transactional(readOnly = true)
public List<Person> getList() {
return em.createQuery("select o from Person o").getResultList();

@Transactional(readOnly = true)
public Person get(Long id) {
return em.find(Person.class, id);

public void save(Person person) {

public void remove(Person person) {

public Person prepare(Person person) {
if (person != null) {
return person;
return new Person();

You can see that we're using Spring annotation, and we're combining the DAO (@Repository) and Service (@Service) in one class for the sake of simplicity.

3. web.xml

Here's what you need to add:

<!-- Use JSF view templates saved as *.xhtml, for use with Facelets -->

<!-- Just here so the JSF implementation can initialize, *not* used at runtime -->

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</ servlet-class>
4. faces-config.xml

We won't use the JSF lifecycle, but we still need to enable Facelets in faces-config.xml:

<!-- Enables Facelets -->

You can download the file, extract it and browse the contents to get "the feel" on how the application works. Pay attention to the files in /WEB-INF/flows since it's where the "flows" are defined. Take a look inside config.xml to see the Spring wirings.

Assuming you already installed Maven 2, to make war-file, type "mvn". To run the application directly from the project directory using maven-jetty-plugin, type "mvn jetty:run". It might take a while for Maven to download all dependencies.

Feel free to ask questions in the comment box below.

Published at DZone with permission of its author, Thomas Wiradikusuma.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Alexander Shvets replied on Wed, 2008/07/02 - 9:03am

Nice example!


small corrections:


1. you forgot this repository:



2. upgrade version for the dependency:



Christophe Chuvan replied on Thu, 2008/07/03 - 11:46am

Can you explain why the save method on your DAO isn't annotated as @Transactional, when all the others are?

I've noticed that you get an error when trying to remove a record after having edited it so I've been trying to debug the issue by playing with Hibernate settings (and with the save method's transactional configuration) but I'm a bit unclear as to how the JPA transaction manager interacts with Hibernate sessions.

Have you noticed the same issue, and if yes, have you worked out how to solve it?




Luqman Samad replied on Mon, 2009/07/20 - 9:45am

New correction:


Masum Sam replied on Sat, 2010/06/26 - 5:45am

pls give me source code for this example . How to Intregrate Spring and JSF . Give a idea or link . 


Nadine Abdel Hamid replied on Sat, 2010/07/03 - 2:26am

Great example, but I need the source code for it The link is not working

Kalita Gupta replied on Wed, 2011/06/08 - 9:39am

I need the source code for it The link is not working

High Information replied on Fri, 2011/07/22 - 1:45am in response to: Kalita Gupta

The same for me, can you reupload it again?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.