posts - 241,  comments - 116,  trackbacks - 0

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

          1、配置ant

          新建一個環境變量ANT_HOME,值為ant所在目錄。

          在path中添加%ANT_HOME%\bin;

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

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

          3、創建工程

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

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

          3.1創建index.jsp

          JAVA內存問題:Java heap space

          在目錄springapp/WebContent中創建。

          <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來查看工程是否已經部署成功。

          最后,可以在瀏覽器中輸入網址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創建控制器

          在springapp/src中創建包springapp.web,在包中創建類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,包中創建類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創建視圖

          使用Entity Framework創建Model

          在springapp/WebContent中創建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 墻頭草 閱讀(676) 評論(0)  編輯  收藏

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


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 朝阳县| 澜沧| 榕江县| 西城区| 沁水县| 琼结县| 永嘉县| 马尔康县| 东辽县| 天柱县| 崇州市| 同仁县| 温宿县| 平罗县| 浦北县| 巴南区| 津市市| 安庆市| 元氏县| 凯里市| 罗平县| 衡东县| 和顺县| 绥德县| 射洪县| 土默特左旗| 廉江市| 滦平县| 英德市| 屏东县| 达日县| 杨浦区| 乐业县| 体育| 西乌珠穆沁旗| 定兴县| 东阳市| 钟祥市| 乐业县| 封丘县| 巴南区|