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

相关文章
相关标签/搜索