MyEclipse Logo

MyEclipse for Spring 10.0: Spring Flex Scaffolding Tutorial and Tips

Scaffolding a Spring-Flex CRUD application in minutes

Niel Eyde

Skyway Software, Inc.

Table of Contents

1. Introduction
2. Goal
3. Prerequisites
4. Create Web Project
5. Scaffold from Database Tables
6. Flex BlazeDS Lazy Loading
7. Deploy the App
8. What's generated?
9. Steps to update the Flex User Interface
10. Conclusion
11. Additional Developer Resources

1. Introduction

One of the goals of MyEclipse for Spring (ME4S) is to help developers write less code from scratch and use code generation more broadly, and ME4S offers the richest set of code generation options available anywhere. ME4S can generate software components for Spring MVC, Spring Web Flow, Spring Security, REST, JAX-WS, Adobe Flex, Google Web Toolkit (GWT), JPA, DAO and JSF 2.0/Primefaces. As an alternative to writing software components from scratch, ME4S helps developers generate software components that are specific to their project. In many cases the software components may be used as-is, or they may be used as a starting point for further development. Furthermore the developer can edit the ME4S templates to further customize how the software components are generated.

Scaffolding refers to the superset of ME4S's broad code generation functions, from generating a specific set of requested software components to generating fully implemented ready-to-run applications. Besides the obvious benefits of reducing application development effort and sparing developers from some of the chores of development, scaffolding is also frequently used for rapidly developing functional prototypes, learning new technologies, and application modernization projects. The ability to quickly generate a full application is very beneficial to prototyping because you can iterate on a domain model and regenerate a functional prototype in minutes. The prototype can be used with business analysts as a tool for elaborating requirements and as the starting point for development. ME4S helps many developers learn new technologies by generating contextual reference projects, which are personalized projects that are based on their own data model. This is a considerably better approach than hunting for sample projects that are often incomplete, don't follow modern best practices, and are based on trivial data models. ME4S can also be used to jumpstart an application modernization effort by leveraging application artifacts from the current system for generating a significant portion of the application components for the new system.

The objective of this tutorial is to walk you through the generation of software components for Adobe Flex and Spring-Flex. Adobe Flex is a free open source framework for building expressive web applications that deploy consistently on all major browsers, desktops, and operating systems by leveraging the Adobe® Flash® Player and Adobe AIR® runtimes. Spring-Flex BlazeDS Integration is a top-level Spring project, with the purpose of making it easier to integrate Adobe Flex applications with Spring-based sofware components. In addition to generating the Adobe Flex and Spring-Flex software components, this tutorial will also generate the software components for the service, domain, and data access layers. This will result in a fully implemented ready-to-run Adobe Flex application that can be immediately used for validating and testing the generated Adobe Flex components, and it will also serve a reference implementation for how the generated Adobe Flex components integrate with the other components of an application using Spring-Flex.

Figure 1. Application Layers - Adobe Flex


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

Figure 3. Spring Flex runtime app created in minutes


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

  • Domain model: EMPLOYEE and OFFICE tables 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 models (EMPLOYEE and OFFICE)

  • DAOs to managing the JPA entity,

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

  • Services with fully implemented CRUD operations for managing the domain model,

  • All the necessary Spring annotations and configuration files for a Spring Flex app,

  • JUnits for every Service and Controller,

  • SpringSource certified code and configuration files,

  • Spring Framework 3.0 (Spring 2.5 optional)

  • Generated Flex 4.0 mxml (Flex 3.5 optional)

  • Spring BlazeDS Integration 1.0.3

  • Out of the box Adobe Flex 4.0 open source SDK and ant script using the mxmlc compiler that generates the swf file into the webroot,

  • An mxml Default Application with separate mxml Forms and Grids for CRUD operations and relationships,

  • A flex_src/assets folder with CSS and images. Uses Embed statements and common Flex styles like backgroundGradientAlphas, up-skin, over-skin, down-skin

  • Navigation to the Context Root,

  • Use of the DataGrid with dataProvider to render results from the Server,

  • Implementation of a TabNavigator to show the CRUD operations and relationships,

  • A RemoteObject in separate mxml applications implemented by creating Operations,

  • An Alert Form validation using StringValidator, NumberValidator, the Validator.validateAll method and ValidationResultEvent adhering to the General guidelines for validation,

  • Asterisks on required fields with the required attribute on the FormItem,

  • Use of the Accordion control to show child relationships,

  • PopUpManager implementation when deleting objects or reporting errors,

  • Editable DataGrid control for the child in One-to-Many or Many-toMany relationships,

  • DateFormatter controls used to format Date and Time fields in forms and inside editable data grids,

  • Use of the Dissolve alternative to fading text,

  • Creates Bindable RemoteClass domain objects with relationships, and

  • Code which follows spring-flex-testdrive conventions including, an Event driven action script architecture for created, updated, and deleted objects. Plus, a compilation of best practices found within the Flex community and recommended to us by Flex experts.

3. Prerequisites

The prerequisites needed to complete this tutorial are:

  • Download MyEclipse for Spring 10.0

  • OPTIONAL: Flash Builder plugins 4. As noted above, MyEclipse for Spring 8.6 comes packaged with the open source Flex 3 SDK, which is used to compile the Flex development assets into a flash file.

4. Create Web Project

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

    Figure 4. New Web Project Wizard


    NOTE: If you are using Flash Builder and want to use a Flex Project, please take a look at the following forum post which helps set up the project for scaffolding: http://www.myeclipseide.com/PNphpBB2-viewtopic-t-25828.html.

5. Scaffold from Database Tables

It's now time to generate all the software components and configuration files required to implement the CRUD application.

  1. Right-click on the EmployeesApp project, and choose MyEclipse > Scaffold Spring CRUD Application from...

    Figure 5. Scaffolding Menu Option


  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 an 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 6. 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 7. 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. Add the EMPLOYEE and OFFICE table to the scaffolding list. Click the Next button.

    Figure 8. 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. Select the Customer Java object for parent. Click the Next button.

    Figure 9. Database Scaffolding Options


  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. Click the Next button.

    Figure 10. 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, JSF 2.0, and iPhone.

    This tutorial is focused on Spring Flex, so click on the Generate checkbox for Adobe Flex. Click the Next button.

    Figure 11. Select Adobe Flex Web Client


  8. The next step is to configure some additional Flex Options. By default, the Flex SDK Home is populated with the packaged Open Source Flex 4.0 SDK located in the user directory. This path is configurable through Windows > Preferences > MyEclipse > Spring Code Generation > Flex.

    If the Flex Builder plug-ins are installed into MyEclipse for Spring and the project contains the Flex nature, this Flex SDK Home text box will not show.

    The next option is to specify the Flex MXML option, Flex 4.0 being the default. Either choice will be compiled against the Flex 4.0 SDK, unless you want to use a different Flex SDK or the Flex builder plugins.

    For this tutorial keep the defaults, and click Next.

    Figure 12. Flex Options


  9. The next step is in an optional step to customize the UI. For this tutorial keep the defaults, and click Next.

    Figure 13. Customize UI


  10. The next step is to specify where the application (source code, configuration files, JSP, etc...) should be generated to in the current project and a few additional scaffolding options. For this panel the defaults are fine. Click the Next button.

    Figure 14. Select Target Folders


  11. The final configuration step is to specify the libraries that should be added to the current project, how they should be added (classpath containers or copied into project), and the version of Spring to use. For this panel the defaults are fine. Click the Next button.

    Figure 15. Runtime Dependencies


    NOTE: if using Spring 2.5, you'll need to consider a classpath dependency conflict described in this forum thread: http://www.myeclipseide.com/PNphpBB2-viewtopic-t-25763.html.

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

    Figure 16. Summary


6. Flex BlazeDS Lazy Loading

The current BlazeDS implementation has limited Lazy Loading support, so some considerations need to be mentioned when dealing with relationships. For more information and discussion about this topic and alternate options, see the myeclipseide.com Spring General forum post on Flex Scaffolding Hibernate Lazy Loading.

  • One-to-One or Many-to-One relationships: To get the Employee > Office (Many-to-One) child form to populate, the FetchType annotation needs to be updated to EAGER in the EmployeesApp\generated\org\employeesapp\domain\Employee.java file. Open that file, and find the @ManyToOne annotation. Change the FetchType enumeration as follows:

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumns(@JoinColumn(name = "OFFICECODE", referencedColumnName = "OFFICECODE"))
    @XmlTransient
    Office office;
    
  • One-to-Many or Many-to-Many relationships: Interestingly enough, no changes need to be made to these types of relationships to get the relationship results to populate. However, it needs to be noted that all the relationship data for to-Many type relationships will be pre-loaded automatically when the application first loads. If this becomes an issue with performance, take a look at the alternate options in the forum linked above.

7. Deploy the App

The next step is to see the application in operation by deploying it to Tomcat. The running application can be used for validating the generated Adobe Flex components and observing the integration with the other components of the application

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

    Figure 17. 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 parent object for Flex, the only option under Adobe Flex is View Employees. Click the button to see generated Spring Flex application in operation.

    Figure 18. Dashboard


  3. The Spring Flex application can be used to perform create, read, update, and delete functions on Employees and Offices. The following screen shots show some of the views.

    Figure 19. EmployeesApp - List & View


8. What's generated?

The scaffolded application is compromised of many integrated software components, and the following diagrams provide an overview of which software components were generated and where they can be found in the project. Understanding what gets generated (and where) will help you find the individual or set of software components that you want use in your own developent projects.

Figure 20. What's generated with Flex Scaffolding (part 1)


Figure 21. What's generated with Flex Scaffolding (part 2)


The Adobe Flex scaffolding blueprint provides a snapshot of what gets generated during scaffolding. The Adobe Flex blueprint and the blueprints for the other ME4S code generations capabilities are covered in more detail in the MyEclipse for Spring reference guide. (see Additional Developer Resources).

Figure 22. Adobe Flex Scaffolding Blueprint


9. Steps to update the Flex User Interface

The following steps provide a general guideline on what to do if the Flex UI needs to be updated:

  1. Make modifications to the *.mxml or *.as files.

  2. Right-click the /build/Employeebuild.xml file and select Run As... Ant Build.

  3. Check the console for a successful build or any errors.

NOTE: If any changes are made to the flex configuration files in WEB-INF, like the services-config.xml, the Flex Ant script needs to be run. Those configuration files are compiled in to the Flash file.

10. Conclusion

Now that you have a running Spring Flex application, you may want re-run the tutorial and scaffold different web clients, including Spring MVC, Web Flow, 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.

11. 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: