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

第一步是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:

SNAGHTML2c995f6

 

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(便于部署),建立了就是这样的:

image

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

 1: <?xml version="1.0"?>
 2:  
 3: <project name="springapp" basedir="." default="usage">
 4: <property file="build.properties"/>
 5:  
 6: <property name="src.dir" value="src"/>
 7: <property name="web.dir" value="war"/>
 8: <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
 9: <property name="name" value="springapp"/>
 10:  
 11: <path id="master-classpath">
 12: <fileset dir="${web.dir}/WEB-INF/lib">
 13: <include name="*.jar"/>
 14: </fileset>
 15: <!-- We need the servlet API classes: -->
 16: <!-- * for Tomcat 5/6 use servlet-api.jar -->
 17: <!-- * for other app servers - check the docs -->
 18: <fileset dir="${appserver.lib}">
 19: <include name="servlet*.jar"/>
 20: </fileset>
 21: <pathelement path="${build.dir}"/>
 22: </path>
 23:  
 24: <target name="usage">
 25: <echo message=""/>
 26: <echo message="${name} build file"/>
 27: <echo message="-----------------------------------"/>
 28: <echo message=""/>
 29: <echo message="Available targets are:"/>
 30: <echo message=""/>
 31: <echo message="build --> Build the application"/>
 32: <echo message="deploy --> Deploy application as directory"/>
 33: <echo message="deploywar --> Deploy application as a WAR file"/>
 34: <echo message="install --> Install application in Tomcat"/>
 35: <echo message="reload --> Reload application in Tomcat"/>
 36: <echo message="start --> Start Tomcat application"/>
 37: <echo message="stop --> Stop Tomcat application"/>
 38: <echo message="list --> List Tomcat applications"/>
 39: <echo message=""/>
 40: </target>
 41:  
 42: <!-- Create folder in tomcat
 43:  <target name="init"> 
 44:  <mkdir dir="${deploy.path}/springapp"/>
 45:  </target> -->
 46: 
 47: <target name="build" description="Compile main source tree java files">
 48: <mkdir dir="${build.dir}"/>
 49: <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
 50: deprecation="false" optimize="false" failonerror="true">
 51: <src path="${src.dir}"/>
 52: <classpath refid="master-classpath"/>
 53: </javac>
 54: </target>
 55:  
 56: <target name="deploy" depends="build" description="Deploy application">
 57: <copy todir="${deploy.path}/${name}" preservelastmodified="true">
 58: <fileset dir="${web.dir}">
 59: <include name="**/*.*"/>
 60: </fileset>
 61: </copy>
 62: </target>
 63:  
 64: <target name="deploywar" depends="build" description="Deploy application as a WAR file">
 65: <war destfile="${name}.war"
 66: webxml="${web.dir}/WEB-INF/web.xml">
 67: <fileset dir="${web.dir}">
 68: <include name="**/*.*"/>
 69: </fileset>
 70: </war>
 71: <copy todir="${deploy.path}" preservelastmodified="true">
 72: <fileset dir=".">
 73: <include name="*.war"/>
 74: </fileset>
 75: </copy>
 76: </target>
 77: 
 78: <!-- ============================================================== -->
 79: <!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
 80: <!-- ============================================================== -->
 81:  
 82: <path id="catalina-ant-classpath">
 83: <!-- We need the Catalina jars for Tomcat -->
 84: <!-- * for other app servers - check the docs -->
 85: <fileset dir="${appserver.lib}">
 86: <include name="catalina-ant.jar"/>
 87: </fileset>
 88: </path>
 89:  
 90: <taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
 91: <classpath refid="catalina-ant-classpath"/>
 92: </taskdef>
 93: <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
 94: <classpath refid="catalina-ant-classpath"/>
 95: </taskdef>
 96: <taskdef name="list" classname="org.apache.catalina.ant.ListTask">
 97: <classpath refid="catalina-ant-classpath"/>
 98: </taskdef>
 99: <taskdef name="start" classname="org.apache.catalina.ant.StartTask">
 100: <classpath refid="catalina-ant-classpath"/>
 101: </taskdef>
 102: <taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
 103: <classpath refid="catalina-ant-classpath"/>
 104: </taskdef>
 105:  
 106: <target name="install" description="Install application in Tomcat">
 107: <install url="${tomcat.manager.url}"
 108: username="${tomcat.manager.username}"
 109: password="${tomcat.manager.password}"
 110: path="/${name}"
 111: war="${name}"/>
 112: </target>
 113:  
 114: <target name="reload" description="Reload application in Tomcat">
 115: <reload url="${tomcat.manager.url}"
 116: username="${tomcat.manager.username}"
 117: password="${tomcat.manager.password}"
 118: path="/${name}"/>
 119: </target>
 120:  
 121: <target name="start" description="Start Tomcat application">
 122: <start url="${tomcat.manager.url}"
 123: username="${tomcat.manager.username}"
 124: password="${tomcat.manager.password}"
 125: path="/${name}"/>
 126: </target>
 127:  
 128: <target name="stop" description="Stop Tomcat application">
 129: <stop url="${tomcat.manager.url}"
 130: username="${tomcat.manager.username}"
 131: password="${tomcat.manager.password}"
 132: path="/${name}"/>
 133: </target>
 134:  
 135: <target name="list" description="List Tomcat applications">
 136: <list url="${tomcat.manager.url}"
 137: username="${tomcat.manager.username}"
 138: password="${tomcat.manager.password}"/>
 139: </target>
 140:  
 141: <!-- End Tomcat tasks -->
 142:  
 143: </project>

在整个项目下添加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的人懂的。

 1: package net.spring.controller;
 2:  
 3: import org.springframework.stereotype.Controller;
 4: import org.springframework.web.bind.annotation.RequestMapping;
 5: import org.springframework.web.servlet.ModelAndView;
 6:  
 7: @Controller
 8: public class HelloWorldController {
 9:  
 10: @RequestMapping("/hello")
 11: public ModelAndView helloWorld() {
 12:  
 13: String message = "Hello World, Spring 3.1.1 Release!";
 14: System.out.println(message);
 15: return new ModelAndView("hello", "message", message);
 16: }
 17:  
 18: }

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

 1: <html>
 2: <head><title>Example :: Spring Application</title></head>
 3: <body>
 4: <h1>Example - Spring Application</h1>
 5: <p>This is my test.</p>
 6: <p><a href="hello.html">Say Hello</a></p>
 7: </body>
 8: </html>

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

 1: <?xml version="1.0" encoding="UTF-8"?>
 2:  
 3: <web-app version="2.4"
 4: xmlns="http://java.sun.com/xml/ns/j2ee"
 5: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6: xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 7: http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
 8:  
 9: <display-name>Spring3MVC</display-name>
 10: <servlet>
 11: <servlet-name>springapp</servlet-name>
 12: <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 13: <load-on-startup>1</load-on-startup>
 14: </servlet>
 15:  
 16: <servlet-mapping>
 17: <servlet-name>springapp</servlet-name>
 18: <url-pattern>*.html</url-pattern>
 19: </servlet-mapping>
 20: 
 21: <welcome-file-list>
 22: <welcome-file>
 23: index.jsp
 24: </welcome-file>
 25: </welcome-file-list>
 26:  
 27: </web-app>

加一个文件srpingapp-servlet.xml

 1: <?xml version="1.0" encoding="UTF-8"?>
 2: <beans xmlns="http://www.springframework.org/schema/beans"
 3: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4: xmlns:p="http://www.springframework.org/schema/p"
 5: xmlns:context="http://www.springframework.org/schema/context"
 6: xsi:schemaLocation="http://www.springframework.org/schema/beans
 7: http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8: http://www.springframework.org/schema/context
 9: http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 10: 
 11: <context:component-scan base-package="net.spring.controller" />
 12: 
 13: <!-- the application context definition for the springapp DispatcherServlet -->
 14: <!-- <bean name="/hello.html" class="net.spring.controller.HelloWorldController"/> -->
 15: 
 16: <bean id="viewResolver"
 17: class="org.springframework.web.servlet.view.UrlBasedViewResolver">
 18: <property name="viewClass"
 19: value="org.springframework.web.servlet.view.JstlView" />
 20: <property name="prefix" value="/WEB-INF/jsp/" />
 21: <property name="suffix" value=".jsp" />
 22: </bean>
 23: </beans>

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

image

然后右键选择项目属性,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:

 1: <%@ page session="false"%>
 2: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 3: <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

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

 1: <%@ include file="/WEB-INF/jsp/include.jsp" %>
 2:  
 3: <html>
 4: <head><title>Hello :: Spring Application</title></head>
 5: <body>
 6: <h1>Hello - Spring Application</h1>
 7: <p>Greetings.</p>
 8: <p>Message: <c:out value="${message}"/></p>
 9: </body>
 10: </html>

 

Ant编译和自动部署到Tomcat

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

SNAGHTML2e22b43

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

 1: Buildfile: C:\Users\GatesBill\workspace\springapp\build.xml
 2:  
 3: usage:
 4: [echo] springapp build file
 5: [echo] -----------------------------------
 6:  [echo] Available targets are:
 7: [echo] build --> Build the application
 8: [echo] deploy --> Deploy application as directory
 9: [echo] deploywar --> Deploy application as a WAR file
 10: [echo] install --> Install application in Tomcat
 11: [echo] reload --> Reload application in Tomcat
 12: [echo] start --> Start Tomcat application
 13: [echo] stop --> Stop Tomcat application
 14: [echo] list --> List Tomcat applications
 15: BUILD SUCCESSFUL
 16: Total time: 989 milliseconds

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

image

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

 1: Buildfile: C:\Users\GatesBill\workspace\springapp\build.xml
 2:  
 3: build:
 4: [javac] Compiling 1 source file to C:\Users\GatesBill\workspace\springapp\war\WEB-INF\classes
 5: [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.5
 6: [javac] 1 warning
 7:  
 8: deploy:
 9: [copy] Copying 11 files to C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\springapp
 10: BUILD SUCCESSFUL
 11: Total time: 4 seconds

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

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

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

SNAGHTML3b360e

 

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

SNAGHTML2ea4317

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

SNAGHTML2eb6ed7

上面这个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还是非常爽的,过程很流畅。喜欢这种感觉。

转载于:https://www.cnblogs.com/Mainz/archive/2012/04/14/2447786.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/379812.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL的环境变量配置详细步骤

1.下载MySQL 例如下载&#xff1a;mysql-installer-community-5.5.60.1 我以这个版本为例 2.右击->我的电脑->属性 3. 4.找到在 系统变量 中找到 Path 5.找MySql的bin目录&#xff08;每个人的电脑上这个文件的位置应该是差不多的&#xff0c;找到之后复制下来&#x…

转帖:强命名程序集(Strong Name Assembly)的概念

强命名程序集&#xff08;Strong Name Assembly&#xff09;的概念因为不同的公司可能会开发出有相同名字的程序集来&#xff0c;如果这些程序集都被复制到同一 个相同的目录下&#xff0c;最后一个安装的程序集将会代替前面的程序集。这就是著名的Windows “DLL Hell”出现的原…

串口发送通信---UART发送---STM32F4实现

串口发送程序配置过程&#xff08;HAL库&#xff09; 初始化串口相关参数&#xff0c;使能串口 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)该函数的参数是串口的基址&#xff0c;在stm32f429xx.h文件中&#xff0c;定义了8个UART_HandleTypeDef的值&#x…

开源软件 许可证密钥_自由和开源软件-1中的重要许可证

开源软件 许可证密钥Its important to take note that there are no good or bad licenses and that no license is superior to another. Anybody can make an open-source license that suits their extravagant, which is the explanation that there are such huge numbers…

串口接收中断配置过程---STM32F4--HAL

串口接收中断程序配置过程&#xff08;HAL&#xff09; 初始化相关参数&#xff0c;使能串口&#xff1a; HAL_UART_Init();该函数的参数是串口的基址&#xff0c;在stm32f429xx.h文件中&#xff0c;定义了8个UART_HandleTypeDef的值&#xff0c;分别是USART1、USART2、USART…

electron 切换至esm

前言 好消息&#xff0c;经过不知道多少年的讨论。 electron28.0.0开始&#xff08;23.08.31&#xff09;&#xff0c;默认支持esm了。 see https://github.com/electron/electron/issues/21457 使用方法 升级至electron^28.0.0简单地在package.json中添加"type":…

javascript运算符_JavaScript中!=或!==运算符之间的区别

javascript运算符We can perceive the differences between these two operators as the same difference that occurs between double equalsTo () and triple equalsTo () operators. We already know that the (!) not-operator used along with () operator is used to che…

实训09.09:简单的彩票系统(机选多注)

package wsq; import java.util.Random; import java.util.Scanner;//机选多注 public class MoreCaiPiao {public static void main(String[] args) {// 定义二维数组 存储多注彩票int[][] numArray new int[5][7];/** 二维数组中 未赋值之前的元素值都为0 { {0,0,0,0,0,0,0}…

项目组的激励策略

我们经常会采取一奖励措施&#xff0c;来激发大家工作的积极性&#xff0c;从而达到提高工作效率的目的。那么我们应该对项目组中的那些类型的人实施激励呢&#xff1f;项目的实践过程中&#xff0c;笔者认为有两类人需要给予正面的奖励。一&#xff0c;能够主动思考&#xff0…

zk中的Datebox中得到Timestamp

String formatDate sdf.format(datebox.getValue()); para.setCreate_time(Timestamp.valueOf(formatDate)); 转载于:https://www.cnblogs.com/avenxia/archive/2012/04/15/2450052.html

定时器--STM32f4--HAL

基本概念 STM32中有三种定时器&#xff0c;高级定时器&#xff0c;通用定时器&#xff0c;基本定时器&#xff0c;具体如下图&#xff1a; 发生如下事件将产生中断/DMA 更新&#xff1a;计数器向上溢出/向下溢出&#xff0c;计数器初始化触发事件&#xff1a;计数器启动、停…

实训09.09:简单的彩票系统(自选多注)

package wsq; import java.util.Scanner; public class CustomCaipiao {public static void main(String[] args) {/** 用户自选彩票数字: 1.使用scanner来输入彩票号码! 2.直接写成自选多注,注数由用户输入决定;* 3.红球值1-33,不重复;蓝球值1-16;*/// 定义二维数组 存储所有的…

c ++查找字符串_C ++类和对象| 查找输出程序| 套装4

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;class Sample {int X;int* const PTR &X;public:void set(int x);void print();};void Sample::set(int x){*PTR x;}void Sample::print(){cout << *PTR - EOF << …

ASP.NET 泛型类型 Dictionary操作

protected void Page_Load(object sender, EventArgs e){//泛型Dictionary Dictionary<string, string> dit new Dictionary<string, string>();dit.Add("13", "张三");dit.Add("22", "李四");Response.Write("总数…

独立看门狗---STM32----HAL

基本概念 看门狗解决的问题是什么&#xff1f; 在系统跑飞&#xff08;程序异常执行&#xff09;的情况&#xff0c;是系统复位&#xff0c;程序重新执行。 独立看门狗适应用于需要看门狗作为一个在主程序之外能够完全独立工作&#xff0c;并且对时间精度要求低的场合。 工…

实训09.09:简单的彩票系统(注册信息)

package wsq; import java.util.Scanner;//本文件负责注册用户信息 /*用户注册信息:1.要求设置账号和密码,使用字符串数组2.账号名不能重复3.密码需要输入两次,两次密码输入一致4.满足账号名不重复.且两次密码一致,即为注册成功!!将信息添加到字符串数组中String[][] users ne…

【转】JAVA生成缩略图

方法1&#xff1a;[第一种方法比后一种生成的缩略图要清晰] import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.WritableRaster;import java.awt.*;import java.awt.geom.AffineTransform;import jav…

javascript写入_如何在JavaScript中写入HTML元素?

javascript写入写入HTML元素 (Writing into an HTML element) To write string/text into an HTML element, we use the following things: 要将字符串/文本写入HTML元素&#xff0c;我们使用以下内容&#xff1a; There must be an HTML element like paragraph, span, div e…

大话设计模式之设计模式遵循的七大原则

最近几年来&#xff0c;人们踊跃的提倡和使用设计模式&#xff0c;其根本原因就是为了实现代码的复用性&#xff0c;增加代码的可维护性。设计模式的实现遵循了一些原则&#xff0c;从而达到代码的复用性及增加可维护性的目的&#xff0c;设计模式对理解面向对象的三大特征有很…

IIC通信---EEPROM24C02---STMF4

IIC通信协议 IIC是同步半双工通信&#xff0c;一个数据线SDA和一个时钟SCL线&#xff0c;可以接受和发送数据。在CPU与被控IC之间、IC与IC之间进行双向传送。 空闲状态 IIC总线的SDA和SCL两条信号线同时处于高电平时&#xff0c;规定为总线的空闲状态。 起始信号 当SCL为高…