利用struts2 springdm felix实现一个web应用

原文:http://bill-xing.iteye.com/blog/793517


springdm是spring和osgi之间的桥梁,利用这个桥梁在osgi的bundle中,可以方便的利用spring的IOC、AOP、配置等特征来管理bundle中的对象,在Struts2的发布包中,自带了一个struts2-osgi-demo-plugin的例子工程,其中就是一个利用springdm管理osgi struts2 bundle的实现。

   这个例子配置运行的步骤如下:

   1、建立一个普通的web工程,在web的配置文件web.xml中加入对于struts2、springdm、osgi的支持,通过context-param的配置,制定在工程中,spring的加载类是OSGi的实现:org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext,也指定了bundle中的spring配置文件所在的位置/META-INF/spring目录。

Xml代码 
  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" version="2.4"  
  3.     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  5.     <display-name>webapp</display-name>  
  6.     <context-param>  
  7.         <param-name>log4jConfigLocation</param-name>  
  8.         <param-value>/WEB-INF/log4j.xml</param-value>  
  9.     </context-param>  
  10.     <filter>  
  11.         <filter-name>struts2-prepare</filter-name>  
  12.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>  
  13.     </filter>  
  14.     <filter>  
  15.         <filter-name>struts2-execute</filter-name>  
  16.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>  
  17.     </filter>  
  18.   
  19.     <filter-mapping>  
  20.         <filter-name>struts2-prepare</filter-name>  
  21.         <url-pattern>/*</url-pattern>  
  22.     </filter-mapping>  
  23.     <filter-mapping>  
  24.         <filter-name>struts2-execute</filter-name>  
  25.         <url-pattern>/*</url-pattern>  
  26.     </filter-mapping>  
  27.     <listener>  
  28.         <listener-class>org.apache.struts2.osgi.StrutsOsgiListener</listener-class>  
  29.     </listener>  
  30.   
  31.     <listener>   
  32.         <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>   
  33.     </listener>   
  34.   
  35.     <listener>  
  36.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  37.     </listener>  
  38.     <listener>  
  39.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  40.     </listener>  
  41.   
  42.     <context-param>  
  43.         <param-name>contextClass</param-name>  
  44.         <param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>  
  45.     </context-param>  
  46.   
  47.     <context-param>  
  48.         <param-name>contextConfigLocation</param-name>  
  49.         <param-value>osgibundle:/META-INF/spring/*.xml</param-value>  
  50.     </context-param>  
  51.   
  52.     <welcome-file-list>  
  53.         <welcome-file>index.html</welcome-file>  
  54.         <welcome-file>index.htm</welcome-file>  
  55.         <welcome-file>index.jsp</welcome-file>  
  56.         <welcome-file>default.html</welcome-file>  
  57.         <welcome-file>default.htm</welcome-file>  
  58.         <welcome-file>default.jsp</welcome-file>  
  59.     </welcome-file-list>  
  60. </web-app>  

 

  2、把Struts的core、osgi、felix实现jar,放到web的lib目录下,实现web工程自动加载felix引擎到系统的运行环境中,并在struts.xml配置文件中设置两个常量:

Xml代码 
  收藏代码
  1. <constant name="struts.objectFactory" value="osgi" />  
  2. <constant name="struts.objectFactory.delegate" value="springOsgi" />  

 

3、在src目录下建立bundles目录,并建立以数字命名的子目录,子目录的名字代表在其中的bundles启动的基本,在这个例子中,需要放入有关springdm(2.0)的bundles、spring(3.0.0)和log实现及其他一些必须的bundles,如下图所示:

然后再建立一个3的子目录,把struts2-osgi-demo-bundle-2.1.8.1.jar放入其中,在application Server上启动web工程,进行访问,可以获得这样的界面:

 

就表示这个demo运行成功了,在demo中有spring和struts2的配置,其中体现了springdm的作用。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值