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的特性開發第一個應用。html

這個教程將告訴你們環境的配置而且在最後開發一個HelloWorld的應用。我將在沒有安裝任何工具的狀況下從頭包括環境的配置一步一步的講解, 這部分涵蓋安裝Eclipse,Virgo Tomcat Server和安裝Eclipse的開發插件. 你須要對Eclipse有些簡單的認識,這樣能幫助你瞭解這部分的內容,但對於開發app不是特別重要, 若是你瞭解它,在以前的工做中用過,那麼你幾分鐘就能將它安裝,因此在這裏我就不花篇幅進行詳細介紹了。.java

安裝完後我會開發一個Hello World的應用跑在VTS上,這將做爲一個引導告訴你們怎麼開發這樣的應用,這個例子是Spring開發中很是基礎的入門例子,由於咱們不須要任何以前的相關經驗。另外你會首先知道怎麼安裝開發工具和使用它。web

讓咱們開始安裝工具, 下面會列出你須要的:spring

•    VirgoTomcat Serverapache

•    Eclipse瀏覽器

•    TheSpringSource Tool Suite Eclipse plugintomcat

首先須要得到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。  後面你全部的工程文件將保存在這個目錄下,你可使用任意目錄,建議使用空目錄這樣會使你後面不會出什麼問題。app

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

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

相關文章
相關標籤/搜索