第一步是Java的Web环境搭建,下载Eclipse(或者更好的但收费的IDE-IntelliJ Idea,和Resharper一家公司出的),下载Tomcat,下载JDK,下载Spring,注意安装Tomcat的时候配置一下管理员账号和密码(如Tomcat/s3cret),安装好了Tomcat以后应该可以在浏览器访问这个地址:http://localhost:8080/(或者其它端口如9090你可以自己制定),点击里面的manager链接可以进入Tomcat管理manager页面 http://localhost:8080/manager/html

Spring MVC+Ant+Tomcat+Eclipse最简单的demo

 

Eclilpse相关设置

首先是环境变量设置,然后要把tools.jar添加到Eclipse的Ant运行时里面去:window->preferences->ant-> runtime, Global entries, add: external jars: jdk7的安装路径/lib/tools.jar。

 

建立一个Spring MVC的程序+Ant+Tomcat

在Eclipse的java环境下(非JavaEE下)建立一个空的java项目(无需选择dynamic web project),名字叫springapp,然后加个目录叫war(便于部署),建立了就是这样的:

Spring MVC+Ant+Tomcat+Eclipse最简单的demo

然后在整个项目下添加build.xml(自动用Ant编译和部署用的,类似makefile,这玩意爽),build.xml内容如下:

>
 2:  
>
/>
 5:  
/>
/>
/>
/>
 10:  
>
>
/>
>
<!-- We need the servlet API classes: -->
<!-- * for Tomcat 5/6 use servlet-api.jar -->
<!-- * for other app servers - check the docs -->
>
/>
>
/>
>
 23:  
>
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
>
 41:  
<!-- Create folder in tomcat
 </target> -->
 46: 
>
/>
>
/>
/>
>
>
 55:  
>
>
>
/>
>
>
>
 63:  
>
>
>
/>
>
>
>
>
/>
>
>
>
 77: 
<!-- ============================================================== -->
<!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
<!-- ============================================================== -->
 81:  
>
<!-- We need the Catalina jars for Tomcat -->
<!-- * for other app servers - check the docs -->
>
/>
>
>
 89:  
>
/>
>
>
/>
>
>
/>
>
>
/>
>
>
/>
>
 105:  
>
/>
>
 113:  
>
/>
>
 120:  
>
/>
>
 127:  
>
/>
>
 134:  
>
/>
>
 140:  
<!-- End Tomcat tasks -->
 142:  
>

在整个项目下添加build.properties(这个是给build.xml配置环境变量的。直接拿过来运行的朋友,这里面的内容记得需要修改为你本地的路径哦!!)

 1: # Ant properties for building the springapp
 2:  
 3: appserver.home=C:/Program Files/Apache Software Foundation/Tomcat 6.0
 4: # for Tomcat 5 use $appserver.home}/server/lib
 5: # for Tomcat 6 use $appserver.home}/lib
 6: appserver.lib=${appserver.home}/lib
 7:  
 8: deploy.path=${appserver.home}/webapps
 9:  
 10: tomcat.manager.url=http://localhost:8080/manager/html
 11: tomcat.manager.username=tomcat
 12: tomcat.manager.password=s3cret

然后添加一个controller,在src下添加一个java文件,输入package为:net.spring.controller。这个controller的意思我想懂得mvc的人懂的。

package net.spring.controller;
 2:  
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 6:  
 7: @Controller
class HelloWorldController {
 9:  
)
public ModelAndView helloWorld() {
 12:  
;
 14: System.out.println(message);
, message);
 16: }
 17:  
 18: }

接着工作在war目录下。首先加个index.jsp

>
>
>
>
>
>
>
>

然后加个目录WEB-INF。里面加一个文件web.xml(这个文件很重要,是web项目最重要的配置文件)(有关Servlet,这个是java web的核心概念。)

>
 2:  
>
 8:  
>
>
>
>
>
>
 15:  
>
>
>
>
 20: 
>
>
 23: index.jsp
>
>
 26:  
>

加一个文件srpingapp-servlet.xml

>
 7: http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8: http://www.springframework.org/schema/context
>
 10: 
/>
 12: 
<!-- the application context definition for the springapp DispatcherServlet -->
 15: 
>
/>
/>
/>
>
>

在WEB-INF加两个目录:jsp和lib。首先复制引用的jar包,例如Spring的jar,然后在lib目录上粘贴,要引用这些jar:

Spring MVC+Ant+Tomcat+Eclipse最简单的demo

然后右键选择项目属性,Build path… Configure build path. Libraries – > add jars…把这些lib下面的jar加入引用。

说一下目录结构:通常,src存放Java源文件,classes存放编译后的class文件,lib存放编译和运行用到的所有jar文件,web存放JSP等web文件,dist存放打包后的jar文件,doc存放API文档。

在jsp目录下添加include.jsp:

添加hello.jsp,注意里面用了Model里面的:message

 2:  
>
>
>
>
>
>
>
>

 

Ant编译和自动部署到Tomcat

为了让Eclipse用我们的Ant编译和build.xml文件,需要设置一下Eclipse:项目属性,Builders,把java builder去掉勾,然后New…一个,选择Ant builder….,然后选择build.xml,如图:

Spring MVC+Ant+Tomcat+Eclipse最简单的demo

确定了以后,点击菜单 project –> Build all … 自动Ant编译:

 1: Buildfile: C:\Users\GatesBill\workspace\springapp\build.xml
 2:  
usage:
file
--
 [echo] Available targets are:
 7: [echo] build --> Build the application
as directory
file
in Tomcat
in Tomcat
Start Tomcat application
 13: [echo] stop --> Stop Tomcat application
 14: [echo] list --> List Tomcat applications
 15: BUILD SUCCESSFUL
time: 989 milliseconds

看了一下源码,果然已经编译好了:

Spring MVC+Ant+Tomcat+Eclipse最简单的demo

但是没有自动部署到Tomcat的webapps里面,我们需要运行Ant deploy:在项目属性,Builders,选择刚才我们新建的那个Ant编译,选择edit,然后里面Argument的地方输入deploy,然后Apply,OK。再次编译,就自动部署Tomcat了:

 1: Buildfile: C:\Users\GatesBill\workspace\springapp\build.xml
 2:  
 3: build:
to C:\Users\GatesBill\workspace\springapp\war\WEB-INF\classes
with -source 1.5
 6: [javac] 1 warning
 7:  
 8: deploy:
to C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\springapp
 10: BUILD SUCCESSFUL
time: 4 seconds

也可以用命令行的方式执行Ant编译(这样我们可以另外写一个deploy的bat脚本,非常方便),不过要首先到computer – properties – advanced - 环境变量,添加下列环境变量:

ANT_HOME=<Ant解压目录,通常在Eclipse的plugin目录下>,Path=…;%ANT_HOME%\bin

然后打开command(如果在win7下,可能需要提升administration 权限),转到springapp目录为当前目录,然后执行ant deploy 即可,如下图:

Spring MVC+Ant+Tomcat+Eclipse最简单的demo

 

到Tomcat的目录下webapps一看,果然有了springapp,然后在浏览器打开Tomcat的manager:http://localhost:8080/manager/html,点击我们的网站springapp,有了:

Spring MVC+Ant+Tomcat+Eclipse最简单的demo

点击say hello,链接到:http://localhost:8080/springapp/hello.html

Spring MVC+Ant+Tomcat+Eclipse最简单的demo

上面这个message是从controller传给model的。

 

Build Error: taskdef class org.apache.catalina.ant.InstallTask cannot be found

如果得到这个错误,一般是因为安装的Tomcat 7而不是Tomcat 6.0,因为在Tomcat 7.0下面要修改下build.xml:

1 <taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
2         <classpath refid="catalina-ant-classpath"/>
3 </taskdef>

要改成:

1 <taskdef name="install" classname="org.apache.catalina.ant.DeployTask">
2         <classpath refid="catalina-ant-classpath"/>
3 </taskdef>

 

总结

用Ant编译和部署到Tomcat还是非常爽的,过程很流畅。喜欢这种感觉。

相关文章: