DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Web Flow CRUD Tutorial

Spring Web Flow CRUD Tutorial

Thomas Wiradikusuma user avatar by
Thomas Wiradikusuma
·
Jul. 02, 08 · Interview
Like (0)
Save
Tweet
Share
74.64K Views

Join the DZone community and get the full member experience.

Join For Free

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):

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

@Version
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
@Repository
@Service("service")
public class PersonServiceImpl implements PersonService {
private EntityManager em;

@PersistenceContext
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) {
em.merge(person);
}

@Transactional
public void remove(Person person) {
em.remove(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 -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>

<!-- Just here so the JSF implementation can initialize, *not* used at runtime -->
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</ servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/s/*</url-pattern>
</servlet-mapping>
4. faces-config.xml

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

<!-- Enables Facelets -->
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>

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.

Spring Framework Web Service Flow (web browser)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Understanding and Solving the AWS Lambda Cold Start Problem
  • 3 Main Pillars in ReactJS
  • Shift-Left: A Developer's Pipe(line) Dream?
  • Testing Repository Adapters With Hexagonal Architecture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: