MyEclipse Logo

MyEclipse for Spring: Spring MVC Scaffolding

Scaffolding a CRUD application in minutes

Niel Eyde

Abstract

This simple tutorial details the steps required to create a fully functional SpringSource certified Java web application using MyEclipse for Spring.


Table of Contents

1. Introduction
2. Goal
3. Prerequisites
4. Create Web Project
5. Scaffold from Database Tables
6. Deploy the App

1. Introduction

Scaffolding consists of generating full applications by applying standard application patterns from a minimal set of inputs provided by the developer. In some cases the scaffolded applications are used as-is, but in other cases the scaffolded artifacts are used as a starting point for additional customization by a developer. In either case scaffolding, originally popularized by the RAILS and GRAILS frameworks, is very effective at jump-starting application development.

Spring MVC is a web framework from the creators of the Spring. While it isn't the only Spring-based web framework, it is one of the mostly commonly used web frameworks. This scaffolding tutorial is going to focus on generating a ready-to-run application based on the Spring MVC web framework. Since Spring MVC is just a web framework and the goal is to generate a ready-to-run application, the other layers of the application will also be scaffolded, including the service layer, data access layer, and in some cases even the domain layer.

Application Architecture:

  • Web Layer - Spring MVC (@Controller)

  • Service Layer - Spring (@Service)

  • Domain Layer - JPA (@Entity)

  • Data Access Layer - Spring (@Repository)

MyEclipse for Spring uses the Create-Read-Update-Delete (CRUD) application pattern for generating applications that allows the end-user to manage application data. While CRUD isn't the only application pattern, it's a fairly typical application pattern. While not all web applications are satisfied solely by the CRUD application pattern, developers find that the resulting generated application artifacts lend themselves to being easily re-used, customized and extended. CRUD applications are tied to an application domain model that is used as the input into the scaffolding engine. The domain model can exist in many forms, and the MyEclipse for Spring scaffolding functionality supports the use of Java beans, JPA entities, or database tables as inputs.

2. Goal

This tutorial is going to walk you through producing a ready-to-run Spring MVC application that implements the CRUD application pattern for a domain model.

  • Domain model: CUSTOMERS table from the MyEclipse Derby database.

  • Target Container: MyEclipse Tomcat

  • Target Database: MyEclipse Derby

MyEclipse for Spring will be used generate the entire application within a matter of minutes that includes:

  • A JPA entity corresponding to domain model (CUSTOMERS)

  • A DAO for managing the JPA entity,

  • Finder methods (JPA named queries) in the DAO based on domain model fields,

  • A Service with fully implemented CRUD operations for managing domain model,

  • A Controller with fully implemented request handlers for supporting web application,

  • All the necessary Spring annotations and config files for a Spring MVC app,

  • CRUD JSP pages using Spring Form tag library and JSTL

  • Layout managed user interface using Sitemesh,

  • Client-side validation implemented Spring JS with DOJO,

  • CSS for UI styling

  • JUnits for every Service and Controller,

  • SpringSource certified code and configuration files,

  • Generated code that follows Spring Recipes,

3. Prerequisites

The prerequisites needed to complete this tutorial are:

  • MyEclipse 8.0 Professional Edition

  • MyEclipse for Spring plugins

4. Create Web Project

  1. Create a MyEclipse Web Project (or Eclipse Dynamic Web Project) called CustomersApp. On the New Web Project Wizard, select Java EE 5.0 for the J2EE specification level.

5. Scaffold from Database Tables

It's now time to automatically generate all the necessary Java/Spring code and configuration files to implement the CRUD application.

  1. Right-click on the CustomersApp project, and choose Spring Code Generation > Scaffold Spring MVC CRUD application from...

    Figure 1. Scaffolding Wizard

    Scaffolding Wizard

  2. The first step is to select the type of artifact you want to scaffold from. As mentioned in the tutorial introduction there are a variety of possible inputs into scaffolding. For this tutorial we're going to scaffold from a pre-existing database table that comes with MyEclipse Derby database. Choose the Database Schema option on the Select Artifact Type(s) panel. Click the Next button.

    Figure 2. Select Artifact Type(s)

    Select Artifact Type(s)

  3. The next step is to select the DB connection for accessing the MyEclipse Derby database. This panel will show you all configured DB connections in the MyEclipse workspace, and you must select the MyEclipse Derby connection, which is a preconfigured DB connection in MyEclipse. Click the Next button.

    Figure 3. Specify Persistence Connection Properties

    Specify Persistence Connection Properties

  4. The next step is to select the desired schema and database table(s) that should be used for scaffolding. When the CLASSICCARS schema is selected the tables list will be populated with a list of all the available tables. The CUSTOMER table should be added to the scaffolding list. Click the Next button.

    Figure 4. Select Database Tables

    Select Database Tables

  5. The next panel will prompt you to select parent objects, and this panel also lets you override the derived name of the Java Object that will be created from the database table. Since we're only scaffolding from a single database table, the Customer java object must be the parent. For this tutorial there's nothing that needs to be changed on this panel. Just click the Next button.

    Figure 5. Database Scaffolding Options

    Database Scaffolding Options

    Note: Java Object names are automatically derived from table names, but the name can be overriden by double-clicking on the name and typing a new name.

  6. The next step is to specify which layers of the application should be scaffolded and which package names should be used for each layer. All the layers are enabled by default. Enter org.customerapp as the base package. The package names for the different layers will be automatically derived from the base package. A sub-package (i.e. web, service, dao, and domain) will be added to the end of the base package.

    Figure 6. Application Layers and Packages

    Application Layers and Packages

  7. The final configuration step is to specify where the application (source code, configuration files, JSP, etc...) should be generated to. For this panel the defaults are fine. Click the Next button.

    Figure 7. Select Target Folders

    Select Target Folders

  8. The final panel will give you a summary of everything that will be generated for you. (In the milestone release this panel is a bit sparse, but we'll have it all there for GA.) Click the Finish button to scaffold the application from the information you provided in the wizard.

    Figure 8. Summary

    Summary

That's it. Once the wizard is complete you have a ready-to-run Spring MVC application that implements the CRUD application pattern for the domain model (CUSTOMERS db table).

6. Deploy the App

  1. To deploy the application, right-click on the CustomersApp project and select Run As --> MyEclipse Server Application.

    Figure 9. Run As --> MyEclipse Server Application

    Run As --> MyEclipse Server Application

  2. MyEclipse Tomcat will start up. This deployed application can be used to (a) list all customers, (b) view a customer, (c) edit a customer, (d) delete customers and (e) add new customers. The following screenshots show some of the view.

    Figure 10. CustomerApps - List

    CustomerApps - List

    Figure 11. CustomerApps - Edit Details

    CustomerApps - Edit Details