MyEclipse for Spring 8.6: Spring MVC Scaffolding

Scaffolding a Spring MVC CRUD application in minutes

Abstract

This tutorial details the steps required to create a fully functional SpringSource certified Spring MVC 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 7. Conclusion 8. Additional Developer Resources

1. Introduction

Scaffolding consists of single-pass generation of full or major portions of an application 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 Layers - Spring MVC

Figure 1. Application Layers - Spring MVC


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 to 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 configuration 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:

 

4. Create Web Project

 

  1. Create a MyEclipse Web Project (or Eclipse Dynamic Web Project) called CustomersApp.

 

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 MyEclipse > Scaffold CRUD application from...

    Scaffolding Wizard

    Figure 3. Scaffolding Wizard


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

    Select Artifact Type(s)

    Figure 4. 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.

    Specify Persistence Connection Properties

    Figure 5. 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.

    Select Database Tables

    Figure 6. 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.

    Database Scaffolding Options

    Figure 7. Database Scaffolding Options


    Note: Java Object names are automatically derived from table names, but the name can be overridden 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.

    Application Layers and Packages

    Figure 8. Application Layers and Packages


     

  7. The next step is to specify which web clients should be generated for the web layer. As you can see, there are a variety of different web clients available, including Spring MVC, Spring Web Flow, Adobe Flex, GWT, and iPhone. This tutorial is focused on Spring MVC, so click on the Generate checkbox for Spring MVC.

    Select Web Client

    Figure 9. Select Web Client


     

  8. The next step is in an optional step to customize the UI. For this tutorial we'll go with the defaults.

    Customize UI

    Figure 10. Customize UI


     

  9. 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.

    Select Target Folders

    Figure 11. Select Target Folders


     

  10. The final panel will give you a summary of everything that will be generated for you. Click the Finish button to scaffold the application from the information you provided in the wizard.

    Summary

    Figure 12. 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.

    Run As --> MyEclipse Server Application

    Figure 13. Run As --> MyEclipse Server Application


  2. MyEclipse Tomcat will start up. The first page will be a dashboard for accessing all scaffolded web clients. Since this tutorial only scaffolded a single database table for Spring MVC, the only option under Spring MVC is View Customers. Click on it to see generated Spring MVC application in operation.

    Dashboard

    Figure 14. Dashboard


     

  3. The Spring MVC 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 screen shots show some of the views.

    CustomerApps - List

    Figure 15. CustomerApps - List


    CustomerApps - Edit Details

    Figure 16. CustomerApps - Edit Details


     

 

7. Conclusion

Now that you have a running Spring MVC application, you may want re-run the tutorial and scaffold different web clients, including Spring Web Flow, Adobe Flex, GWT, and iPhone.

You may also want to try the Spring Annotator and JAX-WS Annotator tutorials which are available in the Eclipse help system and online (see Additional Developer Resources).

What was your experience with this tutorial? Share your experience with us by completing a very brief survey.

8. Additional Developer Resources

Thank you for you interest in MyEclipse for Spring. If you are interested in learning more, the following developer resources are available:

 

Developer Resources

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值