pdfspring-mvc-step-by-step_第1页
pdfspring-mvc-step-by-step_第2页
pdfspring-mvc-step-by-step_第3页
pdfspring-mvc-step-by-step_第4页
pdfspring-mvc-step-by-step_第5页
已阅读5页,还剩63页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Developing a Spring Framework MVC application step-by-stepVersion 2.5Copyright 2004-2008 Thomas Risberg, Rick Evans, Portia TungCopies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each

2、copy contains this Copyright Notice, whether distributed in print or electronically.Overviewi.ii1. Whatscoverediii2. Prerequisitesoftwareiii3. The application we arebuildingiv1. Basic Application and Environment Setup51.1. Createthe project directory structure51.2. Createindex.jsp51.3. Deploy the ap

3、plication to Tomcat61.4. Checktheapplicationworks91.5. Downloadthe Spring Framework101.6. Modify web.xml in the WEB-INF directory101.7. CopylibrariestoWEB-INF/lib121.8. CreatetheController121.9. Write a test for the Controller131.10. CreatetheView141.11. Compile anddeploy the application141.12. Tryo

4、uttheapplication151.13. Summary162. Developing and Configuring the Views and the Controller182.1. Configure JSTL and add JSP header file182.2. Improvethecontroller192.3. Decouple the view from the controller212.4. Summary233. Developingthe Business Logic253.1. Review the business case of the Invento

5、ry Management System253.2. Add some classes for business logic253.3. Summary314. Developingthe Web Interface344.1. Add reference to business logic in the controller344.2. Modify the view to display business data and add support for message bundle354.3. Add some test data to automatically populate so

6、me business objects354.4. Add the message bundle and a clean target to build.xml364.5. Addingaform374.6. Addingaformcontroller404.7. Summary435. ImplementingDatabasePersistence455.1. Createdatabasestartupscript455.2. Create table and test data scripts455.3. Add Ant tasks to run scripts and load test

7、 data465.4. Create a Data Access Object (DAO) implementation for JDBC475.5. Implement tests for JDBC DAO implementation505.6. Summary536. Integrating the Web Application with the Persistence Layer556.1. Modifyservicelayer556.2. Fixthefailingtests566.3. Create new application context for service laye

8、r configuration586.4. Add transaction and connection pool configuration to application context606.5. Final test of the complete application616.6. Summary62A.BuildScripts64Spring Framework (2.5)iiOverviewThis document is a step-by-step guide on how to develop a web application from scratch using the

9、Spring Framework.Only a cursory knowledge of Spring itself is assumed, and as such this tutorial is ideal if you are learning or investigating Spring. Hopefully by the time you have worked your way through the tutorial material you will see how the constituent parts of the Spring Framework, namely I

10、nversion of Control (IoC), Aspect-Oriented Programming (AOP), and the various Spring service libraries (such as the JDBC library) all fit together in the context of a Spring MVC web application.Spring provides several options for configuring your application. The most popular one is using XML files.

11、 This is also the traditional way that has been supported from the first release of Spring. With the introduction of Annotations in Java 5, we now have an alternate way of configuring our Spring applications. The new Spring2.5 release introduces extensive support for using Annotations to configure a

12、 web application.This document uses the traditional XML style for configuration. We are working on an Annotation Edition of this document and hope to publish it in the near future.Please note that we are not going to cover any background information or theory in this tutorial; there are plenty of bo

13、oks available that cover the areas in depth; whenever a new class or feature is used in the tutorial, forward pointers to the relevant section(s) of the Spring reference documentation are provided where the class or feature is covered in depth.1. Whats coveredThe following list details all of the va

14、rious parts of the Spring Framework that are covered over the course of the tutorial.Inversion of Control (IoC)The Spring Web MVC frameworkData access with JDBCUnit and integration testingTransaction management2. Prerequisite softwareThe following prerequisite software and environment setup is assum

15、ed. You should also be reasonably comfortable using the following technologies.Java SDK 1.5Ant 1.7Apache Tomcat 6.0.14Spring Framework (2.5)iiiOverview Eclipse 3.3 (Recommended, but not necessary)Eclipse 3.3 Europa (/europa) with the Web Tools Platform (WTP) Project (http:/www.e

16、/webtools) and the Spring IDE Project () provides an excellent envirnment for web development.You may of course use pretty much any variation or version of the above software. If you want to use NetBeans or IntelliJ instead of Eclipse or Jetty instead of Tomcat, then

17、 many of the tutorial steps will not translate directly to your environment but you should be able to follow along anyway.3. The application we are buildingThe application we will be building from scratch over the course of this tutorial is a very basic inventory management system. This inventory ma

18、nagement system is severely constrained in terms of scope; find below a use case diagram illustrating the simple use cases that we will be implementing. The reason why the application is so constrained is so that you can concentrate on the specifics of Spring Web MVC and Spring, and not the finer de

19、tails of inventory management.Use case diagram of the inventory management systemWe will start by setting up the basic project directory structure for our application, downloading the required libraries, setting up our Ant build scripts, etc. The first step gives us a solid foundation on which to de

20、velop the application proper in parts 2, 3, and 4.Once the basic setup is out of the way, Spring itself will be introduced, starting with the Spring Web MVC framework. We will use Spring Web MVC to display the inventoried stock, which will involve writing some simple Java classes and some JSPs. We w

21、ill then move onto introducing persistent data access into our application, using Springs Simple JDBC support.By the time we have finished all of the steps in the tutorial, we will have an application that does basic inventory management, including listing stock and permitting the price increase of

22、such stock.Spring Framework (2.5)ivChapter 1. Basic Application and EnvironmentSetup1.1. Create the project directory structureWe are going to need a place to keep all the source and other files we will be creating, so lets create a directory named springapp. The decision as to where you create this

23、 directory is totally up to you; we created ours in a Projects directory that we already had in our home directory so the complete path to our project directory is now $HOME/Projects/springapp. Inside this directory we create a sub-directory named src to hold all the Java source files that we are go

24、ing to create. Then we create another sub-directory that we name war. This directory will hold everything that should go into the WAR file that we will use to package and deploy our application. All source files other than Java source, like JSPs and configuration files, belong in the war directory.F

25、ind below a screen shot of what your project directory structure must look like after following the above instructions. (The screen shot shows the project directory structure inside the Eclipse IDE: you do not need to use the Eclipse IDE to complete this tutorial successfully, but using Eclipse will

26、 make it much easier to follow along.)The project directory structure1.2. Create index.jspSince we are creating a web application, lets start by creating a very simple JSP page named index.jsp inSpring Framework (2.5)5Basic Application and Environment Setupthe war directory. The index.jsp is the ent

27、ry point for our application.springapp/war/index.jsp:Just to have a complete web application, lets create a WEB-INFa web.xml file in this new directory.directory inside the wardirectory and placespringapp/war/WEB-INF/web.xml:1.3. Deploy the application to TomcatLets now write the Ant build script th

28、at we are going to use throughout the tutorial. This Ant build script will contain targets for compiling, building and deploying the application. A separate build script will be used for application server specific targets, such as targets for controlling the application under Tomcat.springapp/build

29、.xml:Spring Framework (2.5)6index.jspExample : Spring ApplicationExample - Spring ApplicationThis is my test.Basic Application and Environment SetupSpring Framework (2.5)7 Build the application/ Deploy application as directory/ Deploy application as a WAR file/ Install application in Tomcat/ Reload

30、application in Tomcat/ Start Tomcat application/ Stop Tomcat application/ List Tomcat applications/Basic Application and Environment Setupeverybody full rights to this directory.To create Tomcat user named tomcat with s3cret as their password, go to the Tomcat users fileappserver.home/conf/tomcat-us

31、ers.xml and add the user entry.Now we run Ant to make sure that everything is working okay. You must have your current directory set to thespringapp directory.Open up a command shell (or prompt) and execute ant .The last thing we need to do here is to build and deploy the application. Just run Ant a

32、nd specify deploy deploywar as the target.or1.4. Check the application worksLets just quickly start Tomcat by running $appserver.home/bin/startup.bat. To make sure that we can access the application, run the list task from our build file to see if Tomcat has picked up the new application.Spring Fram

33、ework (2.5)9$ ant list Buildfile: build.xmllist:$ ant deploy Buildfile: build.xmlbuild:mkdir Created dir: /Users/trisberg/Projects/springapp/war/WEB-INF/classesdeploy:copy Copying 2 files to /Users/trisberg/apache-tomcat-5.5.17/webapps/springappBUILD SUCCESSFULTotal time: 4 seconds$ antBuildfile: bu

34、ild.xmlusage:echoecho springapp build fileecho echoecho Available targets are:echoecho build- Build the applicationecho deploy- Deploy application as directory echo deploywar - Deploy application as a WAR file echo install- Install application in Tomcat echo reload- Reload application in Tomcat echo

35、 start- Start Tomcat applicationecho stop- Stop Tomcat application echo list- List Tomcat applications echoBUILD SUCCESSFULTotal time: 2 secondsBasic Application and Environment SetupYou can now open up a browser and navigate to the starting page of our application at the following URL: http:/localh

36、ost:8080/springapp/index.jsp.The applications starting page1.5. Download the Spring FrameworkIf you have not already downloaded the Spring Framework, now is the time to do so. We are currently using the Spring Framework 2.5 release that can be downloaded from /download.

37、Unzip this file somewhere as we are going to use several files from this download later on.This completes the setup of the environment that is necessary, and now we can start actually developing our Spring Framework MVC application.1.6. Modify web.xml in the WEB-INF directorySpring Framework (2.5)10list OK - Listed applications for virtual host localhost list /springapp:running:0:springapplist /manager:running:0:manager list /:running:0:ROOTlist /docs:running:0:docslist /examples:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论