posts - 241,  comments - 116,  trackbacks - 0

          使用eclipse Hilos,Apache Tomcat 6.0.23,ant 1.7.1(eclipse自帶),srping framework 2.5

          1、配置ant

          新建一個環(huán)境變量ANT_HOME,值為ant所在目錄。

          在path中添加%ANT_HOME%\bin;

          這樣,就可以在cmd中使用startup和shutdown輕松的打開和關閉服務器了。

          2、新建一個Server,選擇Apache Tomcat 6.0。

          3、創(chuàng)建工程

          3.1新建一個Dynamic Web Project,名稱為springapp。

          src文件夾用來保存源碼,WebContent用來保存將會部署到服務器的資源。

          3.1創(chuàng)建index.jsp

          JAVA內(nèi)存問題:Java heap space

          在目錄springapp/WebContent中創(chuàng)建。

          <html>
          <head><title>Example :: Spring Application</title></head>
          <body>
          <h1>Example - Spring Application</h1>
          <p>This is my test.</p>
          </body>
          </html>
          修改springapp/war/WEB-INF/web.xml。
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.4"
          xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
          <welcome-file-list>
          <welcome-file>
          index.jsp
          </welcome-file>
          </welcome-file-list>
          </web-app>

          3.3部署到tomcat

          在springapp目錄下新建build.xml。

          <?xml version="1.0"?>
          <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>
          <!-- 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.5" target="1.5" 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 tasks - remove these if you don't have Tomcat installed -->
          <!-- ============================================================== -->
          <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"/>
          </fileset>
          </path>
          <taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
          <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}"
          username="${tomcat.manager.username}"
          password="${tomcat.manager.password}"/>
          </target>
          <!-- End Tomcat tasks -->
          </project>
          在springapp下新建build.properties。
          # Ant properties for building the springapp
          appserver.home=D:/Program Files/apache-tomcat-6.0.32
          # 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
          修改appserver.home/conf/tomcat-users.xml,添加一個管理員。
          <?xml version='1.0' encoding='utf-8'?>
          <tomcat-users>
          <role rolename="manager"/>
          <user username="tomcat" password="s3cret" roles="manager"/>
          </tomcat-users>

          在cmd中切換到工程目錄下,使用startup啟動服務器,使用ant在當前目錄尋找build.xml文件,如果找到了,就將該文件作為build 配置文件。

          使用ant deploy命令來將工程部署到tomcat上,即把WebContent復制到tomcat目錄的webapps下。

          3.4使用ant list來查看工程是否已經(jīng)部署成功。

          最后,可以在瀏覽器中輸入網(wǎng)址http://localhost:8080/springapp/index.jsp來查看我們的工程了。

          3.5下載spring framework。

          http://www.springsource.org/download

          3.6修改springapp/war/WEB-INF/web.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.4"
          xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
          <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>
          在目錄springapp/war/WEB-INF中新建springapp-servlet.xml。
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          <!-- the application context definition for the springapp DispatcherServlet -->
          <bean name="/hello.htm" class="springapp.web.HelloController"/>
          </beans>

          3.7復制所需庫到WebContent/WEB-INF/lib中。

          分別是spring-framework-2.5/dist中的spring.jar,spring-framework-2.5/dist /modules中的spring-webmvc.jar,spring-framework-2.5/lib/jakarta-commons中的 commons-logging.jar和apache-tomcat-6.0.32\lib中的servlet-api.jar。

          在IDE中右擊工程屬性,添加這幾個包。

          3.8創(chuàng)建控制器

          在springapp/src中創(chuàng)建包springapp.web,在包中創(chuàng)建類HelloController.java。

          package springapp.web;
          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 org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          import java.io.IOException;
          public class HelloController implements Controller {
          protected final Log logger = LogFactory.getLog(getClass());
          public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
          logger.info("Returning hello view");
          return new ModelAndView("hello.jsp");
          }
          }

          3.9進行單元測試

          新建一個Source File,然后在這個File中新建包srpingapp,包中創(chuàng)建類HelloControllerTests,并將spring- framework-2.5/lib/junit中的junit-3.8.2.jar復制到WebContent/WEB-INF/lib中。接著在工程 中導入這個包。

          package springapp.web;
          import org.springframework.web.servlet.ModelAndView;
          import springapp.web.HelloController;
          import junit.framework.TestCase;
          public class HelloControllerTests extends TestCase {
          public void testHandleRequestView() throws Exception{
          HelloController controller = new HelloController();
          ModelAndView modelAndView = controller.handleRequest(null, null);
          assertEquals("hello.jsp", modelAndView.getViewName());
          }
          }
          運行這個test,需要在springapp/build.xml中加入下列代碼,使用ant tests檢測。
          <property name="test.dir" value="test"/>
          <target name="buildtests" description="Compile test tree java files">
          <mkdir dir="${build.dir}"/>
          <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
          deprecation="false" optimize="false" failonerror="true">
          <src path="${test.dir}"/>
          <classpath refid="master-classpath"/>
          </javac>
          </target>
          <target name="tests" depends="build, buildtests" description="Run tests">
          <junit printsummary="on"
          fork="false"
          haltonfailure="false"
          failureproperty="tests.failed"
          showoutput="true">
          <classpath refid="master-classpath"/>
          <formatter type="brief" usefile="false"/>
          <batchtest>
          <fileset dir="${build.dir}">
          <include name="**/*Tests.*"/>
          </fileset>
          </batchtest>
          </junit>
          <fail if="tests.failed">
          tests.failed=${tests.failed}
          ***********************************************************
          ***********************************************************
          **** One or more tests failed! Check the output ... ****
          ***********************************************************
          ***********************************************************
          </fail>
          </target>

          3.10創(chuàng)建視圖

          使用Entity Framework創(chuàng)建Model

          在springapp/WebContent中創(chuàng)建hello.jsp

          <html>
          <head><title>Hello :: Spring Application</title></head>
          <body>
          <h1>Hello - Spring Application</h1>
          <p>Greetings.</p>
          </body>
          </html>
          posted on 2011-07-13 09:25 墻頭草 閱讀(672) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運專家
          主站蜘蛛池模板: 淮南市| 安吉县| 霍城县| 清苑县| 秦安县| 行唐县| 三都| 石家庄市| 西青区| 克山县| 翁牛特旗| 景宁| 陇川县| 峨边| 双江| 雷州市| 铁力市| 开远市| 吉水县| 家居| 通道| 崇礼县| 蒲江县| 盐津县| 河西区| 将乐县| 沈丘县| 夹江县| 黔西县| 洛浦县| 个旧市| 天等县| 新营市| 昌黎县| 阿瓦提县| 浪卡子县| 柞水县| 修武县| 青阳县| 固安县| 江城|