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这个接口只有一个方法:

  1. publicString getGreeting()

接下来创建一个实现类;com.xiaxiayoyo.helloprovider.impl.HelloProviderImpl. 实现定义的接口如下:

  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的服务,如下面文件

  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页添加依赖项。配置完后如下:

  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 ,代码如下:

  1. @Controller
  2. public class GreetingsController {
  3. @RequestMapping(value= "/greeting", method = { RequestMethod.GET,RequestMethod.POST})
  4. public ModelAndView greeting(HttpServletRequest request) {
  5. ModelAndViewmv = new ModelAndView();
  6. mv.addObject("greeting",provider.getGreeting());
  7. mv.setViewName("greeting");
  8. returnmv;
  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,内容如下:

  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相关的配置,如下:

  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. <context-param>
  14. <param-name>contextClass</param-name>
  15. <param-value>
  16. org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext
  17. </param-value>
  18. </context-param>
  19. <servlet>
  20. <servlet-name>springDispather</servlet-name>
  21. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22. <load-on-startup>1</load-on-startup>
  23. </servlet>
  24. <servlet-mapping>
  25. <servlet-name>springDispather</servlet-name>
  26. <url-pattern>*.do</url-pattern>
  27. </servlet-mapping>
  28. <listener>
  29. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  30. </listener>
  31. <welcome-file-list>
  32. <welcome-file>index.html</welcome-file>
  33. <welcome-file>index.htm</welcome-file>
  34. <welcome-file>index.jsp</welcome-file>
  35. <welcome-file>default.html</welcome-file>
  36. <welcome-file>default.htm</welcome-file>
  37. <welcome-file>default.jsp</welcome-file>
  38. </welcome-file-list>
  39. </web-app>

DispatcherServlet是Spring MVC中负责转发的,所以我们需要告诉它怎么处理请求,我们在相同的目录下面再创建一个配置文件,配置文件名称的前缀和ServletName一样,springDispather-servlet.xml,内容如下:

  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. <context:component-scanbase-packagecontext:component-scanbase-package="com.xiaxiayoyo.greeterui"/>
  12. <beanidbeanid="viewResolver"
  13. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <propertynamepropertyname="prefix"value="/WEB-INF/jsp/" />
  15. <propertynamepropertyname="suffix" value=".jsp" />
  16. </bean>
  17. </beans>

最后一步, 我们需要一个JSP来渲染我们的欢迎页面. 创建目录/WEB-INF/jsp创建greeting.jsp.

内容如下:

  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两个工程. 当你启动服务器你会看到下面类似的日志输出:

  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

Virgo Tomcat Server 指南-Hello World的更多相关文章

  1. Tomcat权威指南-读书摘要系列2

    2. 配置Tomcat 2.1. 重定向Web应用程序的目录 将工程文件与Tomcat分离 复制conf和webapps文件夹到分离目录: 配置CATALINA_BASE环境变量,值为分离目录: 2. ...

  2. Tomcat权威指南(第二版)下载pdf 高清完整中文版-百度云下载

    本书是一本万能工具,其主题涵盖了Apache Tomcat这一广受欢迎的开源servlet.JSP容器和高性能的web server. <Tomcat权威指南>对管理员和web站点管理员而 ...

  3. tomcat server.xml中文版

    原文:http://www.blogjava.net/baoyaer/articles/107278.html Tomcat Server的结构图 该文件描述了如何启动Tomcat Server &l ...

  4. eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Sertomcat

    eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Serto ...

  5. JDK和Tomcat环境变量,以及用MyEclipse新建Web Project测试Tomcat Server

    [请尊重原创版权,如需引用,请注明来源及地址] 在此之前一直用的Eclipse挺顺手的,今天突然想换MyEclipse试试,不知安装MyEclipse的时候我选错了什么选项,反正JDK和Tomcat的 ...

  6. Tomcat Server Timeouts属性的设置

    在启动Tomcat Server时,经常会出现启动时间过长的错误,如下图所示(为了方便截图,Start Timeout被设置为5秒钟,一般为45秒钟). 双击Tomcat v7.0 Server at ...

  7. tomcat server需要重启的时刻

    1.修改了web project的任何配置文件,都需要重启tomcat 2.修改了任何java class文件,都需要重启tomcat server 3.在项目中添加了任何的文件,包括配置文件.jav ...

  8. Tomcat Server Configuration Automation Reinforcement

    目录 . 引言 . 黑客针对WEB Server会有那些攻击面 . 针对Tomcat Server可以做的安全加固 . Managing Security Realms with JMX . 实现对T ...

  9. Eclipse提示Tomcat miss丢失bug:The Tomcat server configuration at \Servers\Tomcat v5.5 Server at localhost-config is missing.

    Eclipse提示Tomcat miss丢失bug:The Tomcat server configuration at \Servers\Tomcat v5.5 Server at localhos ...

随机推荐

  1. 【Android 】Service 全面总结

    1.Service的种类 按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  2. Atitit.信息论原理概论attilax总结

    Atitit.信息论原理概论attilax总结 1. <信息论基础(原书第2版)>((美)科弗(Cover...)[简介_书评_在线阅读] - 当当图书.html1 2. <信息论- ...

  3. atititt.java定时任务框架选型Spring Quartz 注解总结

    atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz  (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...

  4. HTML5游戏开发进阶指南(亚马逊5星畅销书,教你用HTML5和JavaScript构建游戏!)

    HTML5游戏开发进阶指南(亚马逊星畅销书,教你用HTML5和JavaScript构建游戏!) [印]香卡(Shankar,A.R.)著 谢光磊译 ISBN 978-7-121-21226-0 201 ...

  5. Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS

    题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...

  6. css之属性部分

    这篇写的是今天的学习到的属性,一共20个. 属性再多,但也要会使用,会在使用时可以记起它,才能起到它为我们所需要的作用. 样式属性 1.border CSS边框属性允许你指定一个元素边框的样式和颜色. ...

  7. win10蓝屏问题,关于驱动kisSaasUrlRedirectKnl64.sys 的

    上周末刚从win7升级到win10:今天出现了两次蓝屏了,都是显示: xxxxxxx 百度知道链接---http://zhidao.baidu.com/question/164141456570387 ...

  8. [界面开发新秀]免费的AYUI,开发360领航版系列教程[2/40]

            <界面开发风AYUI-基于WPF By AY> 大家好! 距离上篇博客发布有10天了,因为我在开发AYUI4.X效果更惊艳 我是AY,很高兴,终于可以写自己的作品的,网络博 ...

  9. apache 80端口部属多站点配置

    1.在httpd.conf文件里启用虚拟主机功能,即去掉下面配置项前面的# #LoadModule vhost_alias_module modules/mod_vhost_alias.so 2..在 ...

  10. Maven Learning - Direct Dependencies & Transitive Dependencies

    Dependencies declared in your project's pom.xml file often have their own dependencies. The main dep ...