注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

无线时代辐射无穷

抓紧生宝宝,小心辐射

 
 
 

日志

 
 

Spring-DM在web中的应用  

2011-02-18 23:34:41|  分类: osgi |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

http://chenjumin.javaeye.com/blog/821929

在web中使用Spring-DM时,需要用到spring-osgi-web-extender-x.x.x.jar Bundle,该Bundle的启动级别要比其它的大。

 

一、开发步骤如下:

     1、创建插件工程,最终的目录结构如下:

 

 

     2、添加WEB-INF目录,并在该目录下加入web.xml文件。

 

     3、引入相关的Bundle和包,最终MANIFEST.MF文件的内容如下:


Xml代码 复制代码
Manifest-Version: 1.0   
Bundle-ManifestVersion: 2   
Bundle-Name: P6 Plug-in   
Bundle-SymbolicName: p6;singleton:=true  
Bundle-Version: 1.0.0   
Bundle-Activator: p6.Activator   
Import-Package: javax.servlet,   
 javax.servlet.http,   
 javax.servlet.resources;version="2.4.0",   
 org.osgi.framework;version="1.3.0",   
 org.springframework.osgi.web.context.support;version="1.1.3",   
 org.springframework.web.context;version="2.5.5",   
 org.springframework.web.context.support;version="2.5.5",   
 p3.service;version="1.0.0"  
Require-Bundle: org.springframework.bundle.osgi.core,   
 org.springframework.bundle.osgi.io,   
 org.springframework.bundle.spring.beans,   
 org.springframework.bundle.spring.context,   
 org.springframework.bundle.spring.core,   
 org.springframework.osgi.catalina.osgi,   
 org.apache.jasper  

Manifest-Version: 1.0  Bundle-ManifestVersion: 2  Bundle-Name: P6 Plug-in  Bundle-SymbolicName: p6;singleton:=true  Bundle-Version: 1.0.0  Bundle-Activator: p6.Activator  Import-Package: javax.servlet,   javax.servlet.http,   javax.servlet.resources;version="2.4.0",   org.osgi.framework;version="1.3.0",   org.springframework.osgi.web.context.support;version="1.1.3",   org.springframework.web.context;version="2.5.5",   org.springframework.web.context.support;version="2.5.5",   p3.service;version="1.0.0"  Require-Bundle: org.springframework.bundle.osgi.core,   org.springframework.bundle.osgi.io,   org.springframework.bundle.spring.beans,   org.springframework.bundle.spring.context,   org.springframework.bundle.spring.core,   org.springframework.osgi.catalina.osgi,   org.apache.jasper    

  

     4、创建HelloServlet,其源码如下:


Java代码 复制代码
public class HelloServlet extends HttpServlet{   
    @Override  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)   
            throws ServletException, IOException {   
        doPost(req, resp);   
    }   
  
    @Override  
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)   
            throws ServletException, IOException {   
        resp.setContentType("text/html;charset=GBK");   
           
        WebApplicationContext ctx = WebApplicationContextUtils.   
            getWebApplicationContext(req.getSession().getServletContext());   
        PersonManager pm = (PersonManager)ctx.getBean("osgiPersonManager");   
        if(pm!=null){   
            pm.savePerson("cjm", "123");   
            resp.getWriter().print("调用成功!");   
        }   
    }   
}  

public class HelloServlet extends HttpServlet{   @Override   protected void doGet(HttpServletRequest req, HttpServletResponse resp)     throws ServletException, IOException {    doPost(req, resp);   }     @Override   protected void doPost(HttpServletRequest req, HttpServletResponse resp)     throws ServletException, IOException {    resp.setContentType("text/html;charset=GBK");        WebApplicationContext ctx = WebApplicationContextUtils.     getWebApplicationContext(req.getSession().getServletContext());    PersonManager pm = (PersonManager)ctx.getBean("osgiPersonManager");    if(pm!=null){     pm.savePerson("cjm", "123");     resp.getWriter().print("调用成功!");    }   }  }

  

     5、编辑web.xml文件,其内容如下:


Xml代码 复制代码
<?xml version="1.0" encoding="UTF-8"?>  
<web-app  xmlns="http://java.sun.com/xml/ns/j2ee"    
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    <context-param>    
        <param-name>contextClass</param-name>    
        <param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>    
    </context-param>  
    
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
       
    <servlet>  
        <servlet-name>HelloServlet</servlet-name>  
        <servlet-class>p6.HelloServlet</servlet-class>  
    </servlet>  
       
    <servlet-mapping>  
        <servlet-name>HelloServlet</servlet-name>  
        <url-pattern>/hello</url-pattern>  
    </servlet-mapping>  
</web-app>  

<?xml version="1.0" encoding="UTF-8"?>  <web-app  xmlns="http://java.sun.com/xml/ns/j2ee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   <context-param>        <param-name>contextClass</param-name>        <param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>     </context-param>      <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>      <servlet>    <servlet-name>HelloServlet</servlet-name>    <servlet-class>p6.HelloServlet</servlet-class>   </servlet>      <servlet-mapping>    <servlet-name>HelloServlet</servlet-name>    <url-pattern>/hello</url-pattern>   </servlet-mapping>  </web-app>

    

     6、在WEB-INF目录下加入applicationContext.xml文件,其内容如下:


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"  
    xsi:schemaLocation="   
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">  
       
    <osgi:reference id="osgiPersonManager" interface="p3.service.PersonManager" />  
       
</beans>  

  评论这张
 
阅读(2247)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017