username="${tomcat.manager.username}"
password="${tomcat.manager.password}"/>
--EndTomcattasks-->
这个脚本现在包含了所有我们需要的目标,以便使我们开发更加容易。
这里我不会详细解释这个脚本,因为大部分内容都是比较标准Ant和Tomcat的东西。
你可以直接复制上面的构建文件并且把它放在你的开发目录的根目录中。
我们还需要一个build.properties文件,你需要自定这个文件来配合你的服务器安装。
这个文件和build.xml文件在同一个目录中。
springapp/build.properties
#Antpropertiesforbuildingthespringapp
appserver.home=${user.home}/jakarta-tomcat-5.0.28
deploy.path=${appserver.home}/webapps
tomcat.manager.url=http:
//localhost:
8080/manager
tomcat.manager.username=admin
tomcat.manager.password=tomcat
如果你是在一个你不是Tomcat安装的所有者的系统中,那么Tomcat所有者必须给你访问webapps目录的全部权限,或者他可以在webapps目录下面新建一个“springapp”目录,并且给你全部权限来把程序部署到这个新建的目录中。
在Linux上我运行chmoda+rwxspringapp来给与所有人对目录的访问权利。
如果你使用一个不用的Web应用服务器,那么你要删除在构建脚本底部的那些特定于Tomcat的任务。
你还要依赖你服务器的热部署特定,否则你就需要手工重新启动你的应用服务器。
现在我运行Ant来确保所有的东西都工作正常。
你应该把你当前的目录设置到“springapp”目录下。
[trisberg@localhostspringapp]$ant
Buildfile:
build.xml
usage:
[echo]springappbuildfile
[echo]-----------------------------------
[echo]Availabletargetsare:
[echo]build-->Buildtheapplication
[echo]deploy-->Deployapplicationasdirectory
[echo]deploywar-->DeployapplicationasaWARfile
[echo]install-->InstallapplicationinTomcat
[echo]reload-->ReloadapplicationinTomcat
[echo]start-->StartTomcatapplication
[echo]stop-->StopTomcatapplication
[echo]list-->ListTomcatapplications
BUILDSUCCESSFUL
Totaltime:
2seconds
这里最后的动作是进行实际的部署。
只要运行Ant并且指明“deploy”或者“deploywar”作为目标。
[trisberg@localhostspringapp]$antdeploy
Buildfile:
build.xml
build:
[mkdir]Createddir:
/Users/trisberg/projects/springapp/war/WEB-INF/classes
deploy:
[copy]Copying2filesto/Users/trisberg/jakarta-tomcat-5.0.28/webapps/springapp
BUILDSUCCESSFUL
Totaltime:
2seconds
第4步–测试应用
让我们立刻启动Tomcat并且确保我们可以访问这个应用程序。
使用我们的构建脚本中的“list”任务来查看Tomcat是否已经载入了新的应用程序。
[trisberg@localhostspringapp]$antlist
Buildfile:
build.xml
list:
[list]OK-Listedapplicationsforvirtualhostlocalhost
[list]/admin:
running:
0:
/Users/trisberg/jakarta-tomcat-5.0.28/server/webapps/admin
[list]/webdav:
running:
0:
/Users/trisberg/jakarta-tomcat-5.0.28/webapps/webdav
[list]/servlets-examples:
running:
0:
/Users/trisberg/jakarta-tomcat-5.0.28/webapps/servlets-examples
[list]/springapp:
running:
0:
/Users/trisberg/jakarta-tomcat-5.0.28/webapps/springapp
[list]/jsp-examples:
running:
0:
/Users/trisberg/jakarta-tomcat-5.0.28/webapps/jsp-examples
[list]/balancer:
running:
0:
balancer
[list]/tomcat-docs:
running:
0:
/Users/trisberg/jakarta-tomcat-5.0.28/webapps/tomcat-docs
[list]/:
running:
0:
/Users/trisberg/jakarta-tomcat-5.0.28/webapps/ROOT
[list]/manager:
running:
0:
/Users/trisberg/jakarta-tomcat-5.0.28/server/webapps/manager
BUILDSUCCESSFUL
Totaltime:
1second
如果他没有被列出,使用“install”任务来把应用程序安装到Tomcat中。
[trisberg@localhostspringapp]$antinstall
Buildfile:
build.xml
install:
[install]OK-Installedapplicationatcontextpath/springapp
BUILDSUCCESSFUL
Totaltime:
2seconds
现在打开一个浏览器并浏览http:
//localhost:
8080/springapp/index.jsp.
第5步–下载Spring分发包
如果你还没有下载SpringFramework的发布文件,那现在就行动吧。
我目前使用的是“spring-framework-1.2-with-dependencies.zip”,可以从www.springframework.org/download.html下载到。
我把文件解压缩到我的主目录中。
我们后面将要用到里面的一些文件。
到此为止必要的环境安装已经完成了,现在我们要开始实际开发我们的SpringFrameworkMVC应用了。
第6步–修改WEB-INF目录中的web.xml
进入“springapp/war/WEB-INF”目录。
修改我们前面创建的最小“web.xml”文件。
现在我们要修改它来满足我们需求。
我们定义一个将来控制我们所有请求转向的Dispat