Virgo Tomcat Server 指南-Hello World


Eclipse發布了最新的Virgo Tomccat Server。VTS是一個應用服務器與OSGi緊密結合並且可以開發bundles形式的Spring web apps應用,他們同樣擁有OSGi和Spring的特性。真是太好了,所以我想嘗試下。但是很多人和我一樣沒有接觸過VTS的開發工作。VTS的入門有些困難, 網上有些新手文檔但是大部分需要有Spring DM Server的相關開發經驗,所以我決定記錄我的入門經驗幫助從未用過Spring和VTS的新手。如果你了解OSGi和知道怎么開發OSGI的Bundles並且了解相關的開發知識,那么你可以直接看后面的部分使用Spring和OSGi的特性開發第一個應用。

這個教程將告訴大家環境的配置並且在最后開發一個HelloWorld的應用。我將在沒有安裝任何工具的情況下從頭包括環境的配置一步一步的講解, 這部分涵蓋安裝Eclipse,Virgo Tomcat Server和安裝Eclipse的開發插件. 你需要對Eclipse有些簡單的認識,這樣能幫助你了解這部分的內容,但對於開發app不是特別重要, 如果你了解它,在之前的工作中用過,那么你幾分鍾就能將它安裝,所以在這里我就不花篇幅進行詳細介紹了。.

安裝完后我會開發一個Hello World的應用跑在VTS上,這將作為一個引導告訴大家怎么開發這樣的應用,這個例子是Spring開發中非常基礎的入門例子,因為我們不需要任何之前的相關經驗。另外你會首先知道怎么安裝開發工具和使用它。

讓我們開始安裝工具, 下面會列出你需要的:

•    VirgoTomcat Server

•    Eclipse

•    TheSpringSource Tool Suite Eclipse plugin

首先需要獲得Virgo Tomcat Server. http://www.eclipse.org/virgo/download/。解壓縮到任意目錄, 記住路徑稍后會用到,我解壓到 /Users/xiaxiayoyo/Personal/Development/virgo-tomcat-server-3.0.2.RELEASE

下面安裝開發工具,如果你還沒有安裝之前提到的Eclipse,到http://www.eclipse.org下載最新版的Eclipse IDE for Java EE,應為這個版本包含所有Web開發中需要的插件。 Eclipse不需要安裝,你只需要解壓到任意目錄就可以了, 當你第一次運行Eclipse,它將要求你選擇一個workspace location。  后面你所有的工程文件將保存在這個目錄下,你可以使用任意目錄,建議使用空目錄這樣會使你后面不會出什么問題。

接下來安裝SpringSource Tool Suite(STS)插件。

SpringSourceTool Suite組件和依賴將從更新站點安裝到基於3.6和3.7版本的Eclipse。 

1. 安裝只有在下面必要條件都滿足才能成功:  Eclipse版本要求:Eclipse 3.6 或者 Eclipse 3.7 WTP版本要求:Eclipse WTP 3.2 或者更高版本

2. 在Eclipse Update Manager中取消所有更新站點:

     2.1 打開Preferences -> Install/Update -> AvailableUpdate Sites in Eclipse

     2.2 全選所有站點先點擊Enable這是Enable按鈕會變成Disable再次點擊

     2.3 點擊“OK”關閉preferences窗口並且保存設置

3. 配置STS更新站點:

     3.1 下載下面對應版本的更新站點書簽文件

•    Eclipse3.6: http://dist.springsource.com/release/TOOLS/composite/e3.6/bookmarks.xml

•    Eclipse3.7: http://dist.springsource.com/release/TOOLS/composite/e3.7/bookmarks.xml

     3.2  打開Preferences -> Install/Update -> AvailableUpdate Sites in Eclipse

     3.3  點擊“Import...” 按鈕選擇剛才下載的bookmarks.xml. 點擊“Open” 完成導入.

     3.4  驗證列表中是否有加入的新站點,如果成功關閉 preference窗口

4. 安裝
      4.1 通過Help -> Install New Software ...打開update manager

     4.2 在“Workwith” 下拉菜單中選擇名稱為“SpringSourceUpdate Site for Eclipse 3.6” 或 “SpringSource Update Site for Eclipse 3.7”的更新站點

 

確定選擇上圖勾選的組件,並且按照上面的安裝現象進行安裝。

點擊Next根據提示一步一步到Finish安裝結束。

 

接下來安裝SpringSource DM Server Tools插件,經過我的安裝官方給出1.1.1和2.0.0都無法正常安裝,只能安裝snapshots版,通過官方給出的在線更新地址,安裝步驟和上面的一樣,按照下圖的選項安裝。

 

接下來在Eclipse中設置VTS,打開Eclipse的preferences 找到Server -> Runtime Environments。 點擊右邊的Add…-按鈕,選擇SpringSource ->SpringSource dm Server (Runtime) v2.1. 確定勾選上下面的“Create a new local server” . 點擊Next, 你被要求輸入服務器名稱(可以隨意) 然后需要選擇上面你解壓VTS的目錄。所以在我這里我輸入/Users/xiaxiayoyo/Personal/Development/virgo-tomcat-server-3.0.2.RELEASE。

 

最后一步我們會嘗試將它啟動起來。打開“Servers”視圖如果不在你的工作區,可通過菜單打開Window -> Show View-> Other… -> Server -> Servers。選擇剛才配置的服務點擊“Start” (白色三角綠色背景) 或者右鍵菜單選擇"Start". 當狀態變為Started, 使用瀏覽器訪問http://localhost:8080. 你將看到服務器的初始頁. 你可以通過連接打開控制台並且輸入默認密碼(用戶名:admin,密碼:springsource)。

環境配置就結束了. 在下面我們將開發一個hello world應用

應用將由2個bundles組成:The app will consist of two bundles:一個bundle將對外公開一個服務,第二個bundle將包含一個web應用使用第一個bundle的服務來產生一個歡迎頁面。

在大多數情況下更好的辦法是把服務的接口單獨放在一個bundle里面,這樣可以避免依賴更新的問題。本教程我們不那么做。為了創建一個bundle,我們需要在Eclipse中先創建一個工程,選擇 File -> New -> Project…-> EclipseRT 並且創建一個BundleProject。 取名為HelloProvider.在BundleContent頁, 選擇Module Type is “None”. 工程創建完后,右鍵選擇工程在菜單中選擇Properties->TargetedRuntimes確定選擇上面在Eclipse中創建的VTS, 創一個生命服務接口的接口文件,在這里我創建的是com.xiaxiayoyo.helloprovider.HelloProvider這個接口只有一個方法:

 

[html]  view plain copy
 
  1. publicString getGreeting()  

 

接下來創建一個實現類;com.xiaxiayoyo.helloprovider.impl.HelloProviderImpl. 實現定義的接口如下:

 

[html]  view plain copy
 
  1. public String getGreeting() {       
  2.        return "Hello World!";  
  3. }  

 

 

在標准的OSGi系統里, 我們應該創建一個類BundleActivator,它會實例化服務的實現類和注冊它。但在這里我們使用VirgoTomcat   Server,我們不必在這么干了,Spring會幫我們干這件事,不過你仍然可以自己來處理,而且一樣正常工作,這樣是為了將其他項目輕松的遷移到VirgoTomcat server中。

接下來是使用Spring的注冊配置來注冊OSGi服務, 在你bundle工程的META-INF目錄里創建一個新目錄"spring",創建一個文件"osgi-context.xml"。在文件里我們定義一個bean將它注冊為一個OSGi的服務,如下面文件

 

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xmlns:osgi="http://www.springframework.org/schema/osgi"  
  5.   xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd  
  6.    http://www.springframework.org/schema/osgihttp://www.springframework.org/schema/osgi/spring-osgi.xsd">  
  7.     <osgi:serviceinterfaceosgi:serviceinterface="com.xiaxiayoyo.helloprovider.HelloProvider">  
  8.         <beanclassbeanclass="com.xiaxiayoyo.helloprovider.impl.HelloProviderImpl" />  
  9.     </osgi:service>  
  10. </beans>   

 

你可以看到我們將之前接口的實現類注冊為服務。啟動這個bundle,VTS將實例化服務實現類HelloProviderImpl並且把它注冊為接口HelloProvider的服務,所以其他bundle可以使用這個服務。

下面,要讓它們能這么做我們還需要導出這個接口。 打開MANIFEST.MF文件,到runtime頁,在“exported packages”中添加com.xiaxiayoyo.helloprovider包, 不要導出實現類的包。

現在我們需要開發一個web工程通過服務來展示歡迎頁面。接下來創建一個Dynamic Web Project這里我叫做GreeterUI,Target runtime選擇VTS,Configuration選擇VTS,如下圖:

 

點擊下一步 ,在Default output folder里面將輸出路徑改為WebContent/WEB-INF/classes,這里的WebContent為在下個頁面設置的Content Directory,兩個參數一定要確保一直,如下圖:

 

點擊下一步,這里的Context Root無關緊要因為在Bundle里面上下文路徑在MANIFEST.MF里面配置但是還是建議兩個地方一致。如下圖:

 

點擊Finish完成。

接下來還需要幾步來配置下工程右鍵工程選擇Properties,確定Targeted Runtimes為之前創建的VTS,另外由於這個Web Project的開發需要HelloProvider工程提供的服務,所以在這里需要在Project References里勾選HelloProvider工程。當前這個工程是標准的Web工程,我們需要將它加入Spring Project的特性,我們需要再次右鍵工程->Spring Tools->Add OSGiBundle Project Nature,並且選擇EnableBundle Classpath Container。

工程創建完成后,導入必要的依賴包。使用manifest編輯器打開manifest文件(META-INF->MANIFEST.MF),選擇Dependencies頁添加依賴項。配置完后如下:

 

[html]  view plain copy
 
  1. Manifest-Version:1.0  
  2. Module-Type:Web  
  3. Bundle-SymbolicName:GreetUI  
  4. Bundle-Version:1.0.0  
  5. Bundle-Name:GreetUI  
  6. Bundle-ManifestVersion:2  
  7. Web-ContextPath:greeter  
  8. Import-Package:com.xiaxiayoyo.helloprovider,  
  9.  javax.servlet;version="[3.0.0,3.0.0]",  
  10.  javax.servlet.http;version="[3.0.0,3.0.0]",  
  11.  javax.servlet.jsp.el;version="[2.2.0,2.2.0]",  
  12.  javax.servlet.jsp.jstl.core;version="[1.2.0.v20110728,1.2.0.v20110728]",  
  13.  org.eclipse.virgo.web.dm;version="[3.0.2.RELEASE,3.0.2.RELEASE]"  
  14. Import-Bundle:com.springsource.org.apache.taglibs.standard;version="[1.1.2.v20110517,1.1.2.v20110517]"  
  15. Import-Library:org.springframework.spring;version="[3.0.5.RELEASE,3.0.5.RELEASE]"  
  16. Bundle-ClassPath:/WEB-INF/classes/  



 

注意一定要設置Classpath,不然會響應404錯誤。

接下來創建Web應用的控制器,創建一個 class叫 com.xiaxiayoyo.greeterui.GreetingsController ,代碼如下:

 

[html]  view plain copy
 
  1. @Controller  
  2. public class GreetingsController {  
  3.    
  4.         @RequestMapping(value"/greeting"method = { RequestMethod.GET,RequestMethod.POST})  
  5.         public ModelAndView greeting(HttpServletRequest request) {  
  6.                 ModelAndViewmv = new ModelAndView();  
  7.                 mv.addObject("greeting",provider.getGreeting());  
  8.                 mv.setViewName("greeting");  
  9.                 returnmv;  
[html]  view plain copy
 
  1. }  
  2. @Autowired  
  3. private HelloProvider provider;  



 

provider會被Virgo Tomcat Server注入之前注冊的OSGi服務HelloProvider,The setter will be used by the DM server to set thereference to the HelloProvider service which we have registerd in the OSGiservice registry before. 方法greeting在用戶發起請求的時候會被spring調用。這個例子的邏輯比較簡單,只是從HelloProvider中拿到歡迎信息然后把信息放入ModelAndView內,ViewName是用來渲染頁面的JSP文件名。

接下來需要配置來告訴Spring來為控制器裝配OSGi服務,Spring會先查找HelloProvider然后將引用注入到GreetingsController,你需要告訴Spring那些服務是必須的,那么需要,創建一個context-osgi.xml,內容如下:

 

[html]  view plain copy
 
  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?>  
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:osgi="http://www.springframework.org/schema/osgi"  
  4. xmlns:context="http://www.springframework.org/schema/context"  
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd   
  7.        http://www.springframework.org/schema/osgi    
  8.        http://www.springframework.org/schema/osgi/spring-osgi.xsd   
  9.        http://www.springframework.org/schema/context    
  10.        http://www.springframework.org/schema/context/spring-context.xsd">  
  11. <osgi:referenceidosgi:referenceid="helloProvider"  
  12. interface="com.xiaxiayoyo.helloprovider.HelloProvider"/>  
  13. </beans>  

 

 

接下來需要在Web.xml里加入Spring MVC相關的配置,如下:

 

[html]  view plain copy
 
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <web-appxmlns:xsiweb-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3. xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5. id="WebApp_ID"version="2.5">  
  6. <display-name>GreetUI</display-name>  
  7. <context-param>  
  8. <param-name>contextConfigLocation</param-name>  
  9. <param-value>    
  10. /WEB-INF/osgi-context.xml   
  11. </param-value>  
  12. </context-param>  
  13.    
  14. <context-param>  
  15. <param-name>contextClass</param-name>  
  16. <param-value>    
  17. org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext    
  18. </param-value>  
  19. </context-param>  
  20.    
  21. <servlet>  
  22. <servlet-name>springDispather</servlet-name>  
  23. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  24. <load-on-startup>1</load-on-startup>  
  25. </servlet>  
  26.    
  27. <servlet-mapping>  
  28. <servlet-name>springDispather</servlet-name>  
  29. <url-pattern>*.do</url-pattern>  
  30. </servlet-mapping>  
  31.    
  32. <listener>  
  33. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  34. </listener>  
  35.    
  36. <welcome-file-list>  
  37. <welcome-file>index.html</welcome-file>  
  38. <welcome-file>index.htm</welcome-file>  
  39. <welcome-file>index.jsp</welcome-file>  
  40. <welcome-file>default.html</welcome-file>  
  41. <welcome-file>default.htm</welcome-file>  
  42. <welcome-file>default.jsp</welcome-file>  
  43. </welcome-file-list>  
  44. </web-app>  



 

 

DispatcherServlet是Spring MVC中負責轉發的,所以我們需要告訴它怎么處理請求,我們在相同的目錄下面再創建一個配置文件,配置文件名稱的前綴和ServletName一樣,springDispather-servlet.xml,內容如下:

 

[html]  view plain copy
 
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:osgi="http://www.springframework.org/schema/osgi"  
  4. xmlns:context="http://www.springframework.org/schema/context"  
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.       http://www.springframework.org/schema/beans/spring-beans.xsd    
  7.        http://www.springframework.org/schema/osgi    
  8.        http://www.springframework.org/schema/osgi/spring-osgi.xsd   
  9.        http://www.springframework.org/schema/context    
  10.       http://www.springframework.org/schema/context/spring-context.xsd">  
  11.    
  12. <context:component-scanbase-packagecontext:component-scanbase-package="com.xiaxiayoyo.greeterui"/>  
  13. <beanidbeanid="viewResolver"  
  14. class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  15. <propertynamepropertyname="prefix"value="/WEB-INF/jsp/" />  
  16. <propertynamepropertyname="suffix" value=".jsp" />  
  17. </bean>   
  18. </beans>  

 

 

最后一步, 我們需要一個JSP來渲染我們的歡迎頁面. 創建目錄/WEB-INF/jsp創建greeting.jsp.

內容如下:

 

[html]  view plain copy
 
  1. <%@ page contentType="text/html;charset=UTF-8"language="java" %>  
  2. <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <html>  
  4.     <head>  
  5.         <title>Hello!</title>  
  6.     </head>  
  7.     <body>  
  8.         <c:out value="${greeting}"/>  
  9.     </body>  
  10. </html>  



 

 

開發就結束了,剩下的是部署。打開Server視圖,右鍵VTS選擇 “Add and Remove Projects…” 添加GreeterUI和HelloProvider兩個工程. 當你啟動服務器你會看到下面類似的日志輸出:

 

[html]  view plain copy
 
  1. [2012-04-2215:13:31.078] start-signalling-5          <WE0000I> Starting web bundle 'GreetUI' version '1.0.0' with context path'/greeter'.   
  2. [2012-04-2215:13:31.505] start-signalling-5          <WE0001I> Started web bundle 'GreetUI' version '1.0.0' with context path'/greeter'.   
  3. [2012-04-2215:13:31.507] start-signalling-5          <DE0005I> Started bundle 'GreetUI' version '1.0.0  



 

這個時候打開游覽器訪問http://localhost:8080/greeter/greeting.do,並且你會看到HelloProvder接口的返回內容。

 

參考文檔:

 

Spring DM Server Tutorial-Hello World

STS-installation_instructions


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM