现在我们要进入最后一个环节的开发,也就是开发Web Bundle。
我使用的是GWT来作为前端的展示,如果不熟悉这一块的童鞋可以无视它,因为你可以使用你自己熟悉的MVC框架来替换。
这里只需要了解web Bundle开发中的一些主要步骤就好了。
(1)创建一个Dynamic Web Project
通过菜单选择 File >> New >> Project,进入Project的选择,如图
选中Dynamic Web Project,点击Next,进入下图
添加项目名称,点击Next,进入下图
将Default output folder修改为war/WEB-INF/classes。并添加gwt和test的源码目录。点击Next
这里的context root在OSGI下其实可以不用理会,但是一般还是将它设置为自己需要的path。
content directory则需要改成war目录,因为我上面定义的class输出目录是在war/WEB-INF/classes下的。
所以war将作为整个web应用的根目录。点击Finish,完成对project的创建。
创建完的工程的结构如下所示
上图中我又建立了一个lib目录,用来存放需要的jar引用,即开发时的jar,但部署时不再需要,所以不在war/WEB-INF/lib下。
(2)对Porject添加Bundle Project的特性和Spring Project特性
我们需要加入Bundle Project的特性来帮助我们进行Bundle的开发。如图
选择图中的Add Spring Project Nature可以加入对spring开发的一些特性支持,比如对spring配置文件的编辑;选择另一个Add Osgi Bundle Project Nature,加入Bundle Project的特性,这样我们就可以和之前的Project一样编辑MANIFEST.MF文件了,只是在这个项目中编辑的位置有所改变,MANIFEST.MF存放于war/META-INF目录下。
这里还需要注意,还要重新通过右键再访问一次spring tools这个选项,然后选择一项叫做enable bundle classspath...的选项开启与其它bundle之间的联系。(默认如果新建一个bundle project是开启的,但是这里需要手动开启)
(3)对Project作出一些调整
(3.1)部署内容的调整
打开项目的Properties,选中下图中Deployment Assembly选项
由于设置了多个不同的源码目录,默认部署的时候是会将这么多个目录都部署到服务器中的,但是我们这里只需要部署src的内容,所以要手动去掉gwt和test这两个部署目录,选中后点选remove按钮,最终结果如下图
(3.2)Java build path调整
还是进入项目的Properties设置中,这次选择Java Build Path,我们需要将各个源码的输出类文件的路径进行分开管理,不要都放到war/WEB-INF/classes目录下。这是因为gwt和test的类文件是不需要出现在最终部署的web bundle中的,而且如果在开发的时候都输出到同一个目录下,那对gwt和test的修改将会影响到web的部署(因为服务器插件是将war目录下的内容整个进行部署的),这样会影响开发调试的过程。基于这两个原因我们需要将它们的输出目录分开,如下图所示
需要勾选Allow output folders for source folders选项,然后分别为每个源码目录配置输出路径。
例如gwt源码目录的Output folder设置为项目的bin/gwt目录。
(3.3)加入对GWT开发的支持
由于我使用了GWT,所以这里需要开启GWT的开发支持,还是在项目的Properteis中进行设置
(3.3.1)取消GWT的web 部署选项
这里不需要让GWT对项目做单独的web支持,因为默认是勾选的,取消This project has a war directory的选中。
(3.3.2)加入对web toolkit的支持
选中Use Goole Web Toolkit,然后选择自己需要的GWT版本进行开发。
(4)web.xml
web中最重要的配置就是web.xml的配置了。下面贴出配置的内容
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>net.georgezeng.test.web.core</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:conf/spring/*.xml </param-value> </context-param> <context-param> <param-name>contextClass</param-name> <param-value> org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>gwtfilter</filter-name> <filter-class>net.georgezeng.test.web.filter.GWTFilter</filter-class> </filter> <filter-mapping> <filter-name>gwtfilter</filter-name> <url-pattern>/rpc/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springGwtRemoteServiceServlet</servlet-name> <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springGwtRemoteServiceServlet</servlet-name> <url-pattern>/rpc/*</url-pattern> </servlet-mapping> </web-app>
我们可以看到跟一般的web.xml的配置没什么区别,只有一个地方不同,就是对Spring的WebApplicationContext的设置,我们需要对spring的contextClass重新定义
<context-param> <param-name>contextClass</param-name> <param-value> org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext </param-value> </context-param>
由于我们使用的是virgo server,所以这里使用了virgo server对spring在OSGI中的一个WebApplicationContext的实现。
(5)spring的配置
(5.1)osgi-context.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" xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <osgi:reference id="contactService" interface="net.georgezeng.test.service.ContactService" /> </beans>
(5.2)applicationContext.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" xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="net.georgezeng.test.web" /> </beans>
(6)MANIFEST.MF
(6.1)import
(6.2)export
没有需要export的内容
(6.3)手动编辑MANIFEST
前面一直说到不要自己手动编辑MANIFEST.MF文件,但是这里比较特殊,需要在MANIFEST加上一个头信息,这个头信息是IDE的编辑器没有提供设置的,所以需要自己加入,如图
我们需要加上Web-ContextPath这个头信息,这个是用来告诉virgo server部署的web引用的访问上下文根路径。
所以我们不需要再在tomcat的核心文件中定义。
(7)关于GWT的开发
这里不具体描述GWT的开发过程了,有兴趣的朋友请下载源码自行研究。
至此整个应用的开发就完成了,下一节将会介绍如何在开发过程中部署应用和操作virgo server
(注:GWT的包太大,就不上传了,需要的童鞋自行下载GWT插件然后重新引入就好了)