MyEclipse for Spring 8.6: Spring MVC Scaffolding
2011-02-16 20:06
585 查看
Scaffolding a Spring MVC CRUD application in minutes
Niel Eyde
<neyde@skywaysoftware.com>
Copyright © 2010 Skyway Software
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.
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)
Spring MVC - Annotation-based Programming Model
Spring MVC supports multiple approaches for implementing the web layer, and Spring 2.5 added support for an annotation-based programming model. The annotation-based approach has quickly become the predominant way of using Spring MVC, and it's generally regarded as the best practice in Spring MVC development. The scaffolding functions of MyEclipse for Spring will generate the web layer using the annotation-based programming model.
What about other web technologies?
This tutorial is focused on Spring MVC, but the scaffolding wizard can also generate the web layer implementation for Spring Web Flow, Adobe Flex, GWT and iPhone.
Figure 2. MyEclipse for Spring CRUD Scaffolding
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:Download MyEclipse for Spring 8.6
4. Create Web Project
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.Right-click on the CustomersApp project, and choose MyEclipse > Scaffold CRUD application from...
Figure 3. Scaffolding Wizard
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.
Figure 4. Select Artifact Type(s)
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 5. Specify Persistence Connection Properties
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 6. Select Database Tables
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 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.
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 8. Application Layers and Packages
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.
Figure 9. Select Web Client
The next step is in an optional step to customize the UI. For this tutorial we'll go with the defaults.
Figure 10. Customize UI
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 11. Select Target Folders
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 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).
Scaffold Spring DSL
If you also want to scaffold Spring DSL artifacts for further maintenance and enhancement using Spring DSL Editors, then you should run the Add Spring Code Generation Capabilities wizard first, which will enable the Spring DSL for your project and add the Spring DSL view to your project. The scaffolding wizard will then add corresponding Spring DSL artifacts (model packages, controllers, services, etc...) for all scaffolded application components. You will see them all in the Spring DSL view of your project, and you can maintain and enhance the scaffolded artifacts using the Spring DSL and Spring Editors.
6. Deploy the App
To deploy the application, right-click on the CustomersApp project and select Run As --> MyEclipse Server Application.Figure 13. Run As --> MyEclipse Server Application
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.
Figure 14. Dashboard
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.
Figure 15. CustomerApps - List
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
Reference - Eclipse Help (MyEclipse for Spring 8.6 --> MyEclipse for Spring Reference) or Education Materials (online)
Tutorials - Eclipse Help (MyEclipse for Spring 8.6 --> Tutorials) or Education Materials (online)
Screencasts - MyEclipse for Spring YouTube Channel (online)
Support Forums - MyEclipse for Spring Forums (online)
Blog - MyEclipse for Spring Posts (online)
相关文章推荐
- myeclipse for spring 8.6注册码
- MyEclipse for Spring 8.6注册码生成代码
- 修复升级myeclipseforspring-8.6插件后不能启动的问题
- myeclipse for spring 8.6 序列号
- myeclipse8.6 for spring环境配置
- MyEclipse最新版下载--MyEclipse for Spring 8.6
- myeclipse for spring 8.6 注册码生成
- MyEclipse for Spring 10.0: GWT 2.1 and Spring Scaffolding
- MyEclipse for Spring 8.6注册码代码
- MyEclipse for Spring 10.0: GWT 2.1 and Spring Scaffolding
- Myeclipse8.6_for_Spring性能优化…
- myeclipse for spring-8.6.0-win32 下载
- MYECLIPSE FOR SPRING2014添加tomcat8服务器(win8.1,64位)
- myeclipse for spring优化
- Myeclipse For Blue/Spring/Professional 9.1 破解方法及注册机 JAVA开发相关软件
- myeclipse for spring 2014安装与相应插件的相关问题
- myEclipse for spring 生成注册码
- MyEclipse中spring_servlet.xml报异常错误:Referenced file contains error(...)For&n
- MyEclipse for Spring 10.6设置编码方式
- MyEclipse for Spring 9.0 在线安装SVN