转载请注明出处:http://blog.csdn.net/pearyangyang/article/details/42467729
Spring MVC是Spring大家族框架之一,它通过控制器和模型对象的分离,并利用spring技术整合,使得系统的架构清晰明了。
先前准备:
1.JDK安裝和配置
path: ;C:\Program Files\Java\jdk1.6.0_17\bin
在环境变量中新增一个系统变量JAVA_HOME并配置:C:\Program Files\Java\jdk1.6.0_17
新增系统变量classpath:.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar (注意 . 点号和;分隔符)
验证(在cmd中):java -version
2.Tomcat安装和配置
新增一个CATALINA_HOME:E:\tools\apache-tomcat-7.0.57
在path中新增: ;%CATALINA_HOME%\bin
验证(在cmd中):startup.bat ,顺利启动就可以了。
3.ant的安装和配置
新增一个catalina-ant-classpath: E:\tools\apache-ant-1.9.4
在path中新增: ;%catalina-ant-classpath%\bin
验证(在cmd中): ant -version
所有包请看下面链接:
http://download.csdn.net/detail/yangliding2011/8335613
新建一个dynamic web application,名为springapp,为了和ant结合,我们把WebContent改为了war文件,程序结构如下图:
创建一个index.jsp文件,位置在springmvc1.0/war/index.jsp,内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring MVC Demo</title> </head> <body> <h1>Spring MVC Application</h1> <p>This is my test</p> </body> </html>
然后我们配置web.xml,位置在springmvc1.0/war/WEB-INF/web.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.2" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee; http://java.sun.com/xml/ns/javaee/web-app_3_2.xsd" > <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <project name="springmvc1.0" basedir="." default="usage"> <property file="build.properties"/> <property name="src.dir" value="src"/> <property name="web.dir" value="war"/> <property name="build.dir" value="${web.dir}/WEB-INF/classes"/> <property name="name" value="springmvc1.0"/> <path id="master-classpath"> <fileset dir="${web.dir}/WEB-INF/lib"> <include name="*.jar"/> </fileset> <!-- We need the servlet API classes: --> <!-- * for Tomcat 5/6 use servlet-api.jar --> <!-- * for other app servers - check the docs --> <fileset dir="${appserver.lib}"> <include name="servlet*.jar"/> </fileset> <pathelement path="${build.dir}"/> </path> <target name="usage"> <echo message=""/> <echo message="${name} build file"/> <echo message="-----------------------------------"/> <echo message=""/> <echo message="Available targets are:"/> <echo message=""/> <echo message="build --> Build the application"/> <echo message="deploy --> Deploy application as directory"/> <echo message="deploywar --> Deploy application as a WAR file"/> <echo message="install --> Install application in Tomcat"/> <echo message="reload --> Reload application in Tomcat"/> <echo message="start --> Start Tomcat application"/> <echo message="stop --> Stop Tomcat application"/> <echo message="list --> List Tomcat applications"/> <echo message=""/> </target> <target name="build" description="Compile main source tree java files"> <mkdir dir="${build.dir}"/> <javac destdir="${build.dir}" source="1.6" target="1.6" debug="true" deprecation="false" optimize="false" failonerror="true"> <src path="${src.dir}"/> <classpath refid="master-classpath"/> </javac> </target> <target name="deploy" depends="build" description="Deploy application"> <copy todir="${deploy.path}/${name}" preservelastmodified="true"> <fileset dir="${web.dir}"> <include name="**/*.*"/> </fileset> </copy> </target> <target name="deploywar" depends="build" description="Deploy application as a WAR file"> <war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml"> <fileset dir="${web.dir}"> <include name="**/*.*"/> </fileset> </war> <copy todir="${deploy.path}" preservelastmodified="true"> <fileset dir="."> <include name="*.war"/> </fileset> </copy> </target> <!--如果你服務器不是tomcat,則可以配置其他的服務器--> <path id="catalina-ant-classpath"> <!-- We need the Catalina jars for Tomcat --> <!-- * for other app servers - check the docs --> <fileset dir="${appserver.lib}"> <include name="catalina-ant.jar"/> <include name="tomcat-coyote.jar"/> <include name="tomcat-util.jar"/> </fileset> <fileset dir="${appserver.home}/bin"> <include name="tomcat-juli.jar"/> </fileset> </path> <taskdef name="install" classname="org.apache.catalina.ant.DeployTask"> <classpath refid="catalina-ant-classpath"/> </taskdef> <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask"> <classpath refid="catalina-ant-classpath"/> </taskdef> <taskdef name="list" classname="org.apache.catalina.ant.ListTask"> <classpath refid="catalina-ant-classpath"/> </taskdef> <taskdef name="start" classname="org.apache.catalina.ant.StartTask"> <classpath refid="catalina-ant-classpath"/> </taskdef> <taskdef name="stop" classname="org.apache.catalina.ant.StopTask"> <classpath refid="catalina-ant-classpath"/> </taskdef> <target name="install" description="Install application in Tomcat"> <install url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}" war="${name}"/> </target> <target name="reload" description="Reload application in Tomcat"> <reload url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}"/> </target> <target name="start" description="Start Tomcat application"> <start url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}"/> </target> <target name="stop" description="Stop Tomcat application"> <stop url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}"/> </target> <target name="list" description="List Tomcat applications"> <list url="${tomcat.manager.url}/text" username="${tomcat.manager.username}" password="${tomcat.manager.password}"/> </target> <!-- END tomcat配置 --> </project>
# Ant properties for building the springapp appserver.home=E:/tools/apache-tomcat-7.0.57 # for Tomcat 5 use $appserver.home}/server/lib # for Tomcat 6 use $appserver.home}/lib appserver.lib=${appserver.home}/lib deploy.path=${appserver.home}/webapps tomcat.manager.url=http://localhost:8080/manager tomcat.manager.username=tomcat tomcat.manager.password=s3cret
<?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="manager"/> <user username="tomcat" password="s3cret" roles="manager"/> </tomcat-users>打开DOS命令,路径指定到项目路径下,运用ant指令查看项目是否有误。
ant deploy命令,把项目部署到tomcat上面,使用ant deploy或者ant deploywar命令都可以:
最后可以在相应的tomcat目录下的webapps里面查看部署的项目,打开网页,输入http://localhost:8080/springmvc1.0/index.jsp,如下显示:
我们也可以通过tomcat用户权限登录tomcat管理界面http://localhost:8080/manager,查看服务器相关信息:
好了,前期的准备工作我们已经OK了,最后我们看看文档的目录结构,与我们平常的差别就是WebContent的文件包名字变成了war包:
注意tips:
1.如果是tomcat5或6,则在build.xml配置的时候,install的配置应该引用InstallTask类, 7或更高版本则是org.apache.catalina.ant.DeployTask
2.解决warning:‘includeantruntime‘ was not set的问题,在build里面加上includeantruntime="on" 就可以了
代码下载:http://download.csdn.net/detail/yangliding2011/8355879
翻译来自:http://docs.spring.io/docs/Spring-MVC-step-by-step/part1.html
原文地址:http://blog.csdn.net/pearyangyang/article/details/42467729