Yanger(y-ge@263.net)
开发环境:
Java SDK (I am currently using version 1.4.1)
Ant (using version 1.5.3)
Apache Tomcat (using version 4.1.24)
您可以用功能相同的任何容器或者java运行环境
第1步:开发路径 为了保留我们的开发中每一步的流程,我们将保存所有的源码和文件。 首先,我们创建一个springapp的目录(你可以将这个目录建在任何地方),然后,在springapp目录下创建子目录src,用来保存java资源文件。然后我们建立另一个子目录war,这个目录将保存生成的war文件。 建立WEB-INF子目录,和它的classes,lib子目录。 OK,你的目录应该这个样子: 代码:
-spirngapp -WEB-INF -classes -lib -src -war
第2步:创建jsp 我们创建一个jsp文件-index.jsp在springapp目录下,这将是我们应用程序的入口.
代码: springapp/index.jsp
<html> <head><title>Example :: Spring Application</title></head> <body> <h1>Example - Spring Application</h1> <p>This is my test.</p> </body> </html>
现在,我们来完成这个web应用,在WEB-INF下建立web.xml文件 代码:
springapp/WEB-INF/web.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list>
</web-app>
第3步:部署这个应用到Tomcat 现在我们来写Ant脚本文件,用于建立,部署,控制这个应用. 代码:
springapp/build.xml <?xml version="1.0" encoding="gb2312"?>
<project name="springapp" 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="springapp"/>
<path id="master-classpath"> <fileset dir="${web.dir}/WEB-INF/lib"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.home}/common/lib"> <include name="servlet.jar"/> </fileset> </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 --> 建立应用"/> <echo message="deploy --> 部署应用"/> <echo message="deploywar --> 部署war文件"/> <echo message="install --> 安装应用"/> <echo message="reload --> 重载应用"/> <echo message="start --> 启动Tomcat application"/> <echo message="stop --> 停止 Tomcat application"/> <echo message="list --> 列表 Tomcat applications"/> <echo message=""/> </target>
<!-- =========================================================== --> <!-- spring tasks by yanger 2003-11 --> <!-- =========================================================== --> <target name="build" description="Compile main source tree java files"> <mkdir dir="${build.dir}"/> <javac destdir="${build.dir}" target="1.3" 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="${tomcat.home}/webapps/${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 tasks - remove these if you don't have Tomcat installed --> <!-- ============================================================ -->
<taskdef name="install" classname="org.apache.catalina.ant.InstallTask"> <classpath> <path location="${tomcat.home}/server/lib/catalina-ant.jar"/> </classpath> </taskdef> <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask"> <classpath> <path location="${tomcat.home}/server/lib/catalina-ant.jar"/> </classpath> </taskdef> <taskdef name="list" classname="org.apache.catalina.ant.ListTask"> <classpath> <path location="${tomcat.home}/server/lib/catalina-ant.jar"/> </classpath> </taskdef> <taskdef name="start" classname="org.apache.catalina.ant.StartTask"> <classpath> <path location="${tomcat.home}/server/lib/catalina-ant.jar"/> </classpath> </taskdef> <taskdef name="stop" classname="org.apache.catalina.ant.StopTask"> <classpath> <path location="${tomcat.home}/server/lib/catalina-ant.jar"/> </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}" username="${tomcat.manager.username}" password="${tomcat.manager.password}"/> </target>
<!-- End Tomcat tasks -->
</project>
这个Ant脚本包含了建立应用主要的工具命令,但是,这个脚本要配合build.properties使用. 代码:
springapp/build.properties # Ant properties for building the springapp
deploy.path=/home/trisberg/jakarta-tomcat-4.1.24/webapps #deploy.path=c:/Tomcat 4.1/webapps #deploy.path=c:/bea/user_projects/domains/mydomain/applications
tomcat.home=/home/trisberg/jakarta-tomcat-4.1.24 #tomcat.home= c:/Tomcat 4.1 tomcat.manager.url=http://localhost:8080/manager tomcat.manager.username=admin tomcat.manager.password=tomcat
deploy.path 是tomcat的应用目录 tomcat.path 是tomcat的主目录 tomcat.manager.url是管理访问路径,注意你的端口是否正确 tomcat.manager.username,tomcat.manager.password 我就不用说了吧
如果你执行install命令,将在tomcat webapps目录下建立springapp目录。 如果你用其他的web应用服务器,那么你可以删除tomcat指定的命令集,你可以用server提供的热部署进行启动和停止你的应用.
现在我们来看一下ant提供的每个命令,在你的springapp目录下执行ant
代码: C:\projects\springapp>ant Buildfile: build.xml
usage:
[echo] springapp build file [echo] -----------------------------------
[echo] Available targets are:
[echo] build --> 建立应用 [echo] deploy --> 部署应用 [echo] deploywar --> 部署war文件 [echo] install --> 安装应用 [echo] reload --> 重载应用 [echo] start --> 启动 springapp [echo] stop --> 停止 springapp [echo] list --> 列表 Tomcat applications
BUILD SUCCESSFUL Total time: 5 seconds
现在,我们来部署应用,执行ant deploy
代码: Ant deploy e:\projects\springapp>ant deploy Buildfile: build.xml deploy: [copy] Copying 1 file to C:\Tomcat 4.1\webapps\springapp BUILD SUCCESSFUL Total time: 1 seconds
第4步:测试应用 先看一下我们是否把应用部署成功
代码: Ant list E:\projects\springapp>ant list Buildfile: build.xml
list: [list] OK - Listed applications for virtual host localhost
[list] /admin:running:0:../server/webapps/admin
[list] /webdav:running:0:C:\Tomcat 4.1\webapps\webdav
[list] /springapp:running:1:C:\Tomcat 4.1\webapps\springapp
[list] /examples:running:0:examples
[list] /ofproject:running:0:C:\Tomcat 4.1\webapps\ofproject
[list] /tomcat-docs:running:0:C:\Tomcat 4.1\webapps\tomcat-docs
[list] /:running:0:C:\Tomcat 4.1\webapps\ROOT
[list] /manager:running:0:../server/webapps/manager
BUILD SUCCESSFUL Total time: 5 seconds
如果你还没有安装,请执行ant install
代码: Ant install E:\projects\springapp>ant install Buildfile: build.xml install: [install] OK - Installed application at context path /springapp BUILD SUCCESSFUL Total time: 2 seconds
现在,让我们打开浏览器看一下结果http://localhost:8080/springapp/index.jsp
第5步:下载spring framework package 如果你还没有下在spring,请到www.springframework.org/download.html. 寻找最新版本.
下面我们将完成利用spring framework 进行开发MVC 应用程序.
第6步:修改web.xml
进入WEB-INF目录编辑web.xml
代码: WEB-INF/web.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet> <servlet-name>springapp</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>springapp</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping>
<welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list>
</web-app>
在WEB-INF目录下创建一个springapp-servlet.xml文件,它的命名规则是web.xml中的servlet-name 加上'-servlet'后缀,这是在springframework中的标准命名法则. 现在,我们声明一个bean :springappController 他对应的类是SpringappController.class 这个定义用于我们控制业务逻辑。我们还需要定义一个url mapping作为http调用的路径.
代码: springapp/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - Application context definition for "springapp" DispatcherServlet. -->
<beans> <bean id="springappController" class="SpringappController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hello.htm">springappController</prop> </props> </property> </bean> </beans>
第7步:添加支持包
从spring的下载文件包中找到,spring.jar (spring-framework-1.0-m1/dist/spring.jar) 拷贝到WEB-INF/lib下面。 拷贝spring-framework-1.0-m1/lib/log4j/log4j-1.2.8.jar 和 spring-framework-1.0-m1/lib/jakarta-commons/commons-logging.jar到WEB-INF/lib下面。
第8步:创建springappController
代码:
springapp/src/SpringappController.java import org.springframework.web.servlet.mvc.Controller; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SpringappController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { return new ModelAndView(""); } }
这只是一个基础控制框架,稍候我们完成它.
第9步:建立应用
代码: Ant build E:\projects\springapp>ant build Buildfile: build.xml
build: [javac] Compiling 1 source file to E:\projects\springapp\WEB-INF\classes
BUILD SUCCESSFUL Total time: 2 seconds
第10步:建立日志系统 spring利用log4j来管理日志,在classes目录下建立log4j.properties
代码: springapp/war/WEB-INF/classes/log4j.properties log4j.rootCategory=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=e:/projects/springapp/springapp.log log4j.appender.logfile.MaxFileSize=512KB # Keep three backup files log4j.appender.logfile.MaxBackupIndex=3 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout #Pattern to output : date priority [category] - <message>line_separator log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
第11步:部署应用 运行ant deploy
第12步:创建视图
代码: springapp/war/hello.jsp <html> <head><title>Example :: Spring Application</title></head> <body> <h1>Hello - Spring Application</h1> <p>Greetings.</p> </body> </html>
下面我们修改SpringappController.java
代码:
springapp/src/SpringappController.java import org.springframework.web.servlet.mvc.Controller; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("SpringappController - returning hello view");
return new ModelAndView("hello.jsp"); } }
现在我们运行build, deploy, stop, start 命令. 这样我们就成功地建立了一个mvc应用,访问http://loaclhost:8080/springapp/hello.htm
小结 以上,我们快速地利用spring framework建立了MVC应用程序, 1、建立index.jsp,测试管理工具和开发环境 2、编写springapp-servlet.xml配置文件。 3、编写控制器代码,SpringappController.java结合springapp-servlet中的定义进行工作. 4、编写、展示hello.jsp,完成MVC的应用程序.
|
|