`

基于felix框架的struts+springDm的web开发

    博客分类:
  • OSGI
阅读更多

struts最新本身已经支持felix1.4,但felix1.4版本本身不支持fragment,本文通过struts2.2.1和springDM1.2搭建了基于felix的OSGI的web开发。具体步骤如下:

1、建立基于struts2.2.1,springDm1.2,支持felix的osgi的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>webapp2.2.1</display-name>

	<context-param>
		<param-name>struts.osgi.clearBundleCache</param-name>
		<param-value>true</param-value>
	</context-param>
	
	<context-param>
		<param-name>struts.osgi.runLevel</param-name>
		<param-value>4</param-value>
	</context-param>

	<filter>
		<filter-name>struts2-prepare</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
	</filter>
	<filter>
		<filter-name>struts2-execute</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2-prepare</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2-execute</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>org.apache.struts2.osgi.StrutsOsgiListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<!-- 
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	 -->
	<context-param>
		<param-name>contextClass</param-name>
		<param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
	</context-param>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>osgibundle:/META-INF/spring/*.xml,spring/*.xml</param-value>
	</context-param>
	<context-param>
		<param-name>parentContextKey</param-name>
		<param-value>parent-context-bean</param-value>
	</context-param>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 web工程中的struts配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.objectFactory" value="osgi" />
	<constant name="struts.objectFactory.delegate" value="springOsgi" />
	<!-- 
	<constant name="struts.objectFactory.spring.delegate" value="spring" />
	<constant name="struts.convention.result.path" value="/content/"/>
    <constant name="struts.convention.default.parent.package" value="osgi-default" /> 
     -->
</struts> 

 log4j内容如下:

log4j.rootCategory=DEBUG,stdout,R

# Set the level to DEBUG if you want to log all SlideExceptions (some of them aren't errors)
log4j.category.org.apache.slide.common.SlideException=FATAL

########################################

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#log4j.appender.stdout.layout.ConversionPattern=%4p [%t] %c - %m%n
log4j.appender.stdout.layout.ConversionPattern=[%t] %-5p %-20c{2} - %m %n 

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=D:/logger.log

log4j.appender.R.ImmediateFlush=true

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
#log4j.appender.R.layout.ConversionPattern=%4p [%t] %c - %m%n
log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} [%t] %-5p %-30c{3} %x - %m %n

 2、定义time接口的buddle


package com.example.time;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		System.out.println("com.example.time buddle is start!");
		// create a tracker and track the log service
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		System.out.println("com.example.time buddle is stop!");
	}

}
 
package com.example.time.service;

public interface TimeService {
	public String getTime();
	public String getTime(String hello);
}

 配置文件

Manifest-Version: 1.0
Export-Package: com.example.time.service
Bundle-Vendor: EXAMPLE
Bundle-Version: 1.0.0.201011051048
Bundle-Name: Time
Bundle-Activator: com.example.time.Activator
Bundle-ManifestVersion: 2
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-SymbolicName: com.example.time
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

 3.定义接口实现的buddle,具体内容如下:


package com.example.time.local;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext bundleContext) throws Exception {
		System.out.println("com.example.time.local bundle is start");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		System.out.println("com.example.time.local bundle is stop");
	}

}

package com.example.time.local.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.example.time.service.TimeService;

public class LocalTimeService implements TimeService {
	@Override
	public String getTime() {
		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("hello");
		return "The local time:" + formatter.format(date);
	}

	public LocalTimeService() {
		// TODO Auto-generated constructor stub
		System.out.println("hello export service.");
	}

	@Override
	public String getTime(String hello) {
		// TODO Auto-generated method stub
		return hello + " : " + this.getTime();
	}

}

配置文件信息

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Local
Bundle-SymbolicName: com.example.time.local
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.example.time.local.Activator
Bundle-Vendor: EXAMPLE
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: com.example.time.service,
 org.osgi.framework;version="1.3.0"

 2个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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
				http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="TimeService" class="com.example.time.local.service.LocalTimeService"/>
</beans>



<?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.0.xsd
		http://www.springframework.org/schema/osgi
				http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<osgi:service id = "testService" ref="TimeService" interface="com.example.time.service.TimeService"/>
</beans>

 4、定义struts web bundle工程


package com.example.time.web;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext bundleContext) throws Exception {
		System.out.println("com.example.time.web is start");
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		System.out.println("com.example.time.web is stop");
	}

}


package com.example.time.web.action;

import org.apache.struts2.osgi.interceptor.BundleContextAware;
import org.osgi.framework.BundleContext;

import com.example.time.service.TimeService;
import com.opensymphony.xwork2.ActionSupport;

public class TimeAction extends ActionSupport implements BundleContextAware {

	private BundleContext bundleContext;
	private String timeMessage;
	private TimeService timeService;

	public String execute() {
		this.timeMessage = this.timeService.getTime();
		return "success";
	}

	public String getTimeMessage() {
		return this.timeMessage;
	}

	public void setTimeMessage(String timeMessage) {
		this.timeMessage = timeMessage;
	}

	public TimeService getTimeService() {
		return this.timeService;
	}

	public void setTimeService(TimeService timeService) {
		this.timeService = timeService;
	}

	public void setBundleContext(BundleContext arg0) {
		this.bundleContext = arg0;
	}
}

 applictionContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       	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.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/osgi
				http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<osgi:reference id="testService" interface="com.example.time.service.TimeService"/>

	<bean id = "timeAction" class = "com.example.time.web.action.TimeAction" >
		<property name="timeService" ref ="testService"/>
	</bean>
</beans>

 men-info文件如下:

Manifest-Version: 1.0
Export-Package: com.example.time.web;uses:="org.osgi.framework",com.ex
 ample.time.web.action;uses:="com.example.time.service,com.opensymphon
 y.xwork2,org.osgi.framework,org.apache.struts2.osgi.interceptor"
Private-Package: time,.
Struts2-Enabled: true
Bundle-ManifestVersion: 2
Bundle-Name: Web
Bundle-SymbolicName: com.example.time.web
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.example.time.web.Activator
Bundle-Vendor: EXAMPLE
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: com.example.time.service,
 com.opensymphony.xwork2,
 org.apache.struts2.osgi.interceptor,
 org.osgi.framework;version="1.3.0"

 tim.ftl

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>Untitled Document</title>
	</head>
	<body>
			<h1> Hello world,Bundle test.${timeMessage}</h1>
	</body>
</html>

 strut.xml配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 <struts>
    <package name="time-example" namespace="/time" extends="osgi-default">
        <action name="time" class="timeAction">
            <result type = "freemarker">/time/time.ftl</result>
        </action>
    </package>
 </struts>

 将3个工程打包,放到web工程的bundle中的3文件夹下,启动进行访问即可。

 

 

  • 大小: 18.4 KB
  • 大小: 24.8 KB
  • 大小: 24.8 KB
分享到:
评论
5 楼 eyesonmeq 2012-03-06  
parent-context-bean是怎么配置的,我怎么报错,能不能给我一份源码,邮箱是liming@antvision.cn,谢谢!
4 楼 lzcsxx 2012-03-06  
能发一份能跑起来的源码吗?邮箱lzcsxx123@163.com 谢谢
我试了好多次了 springdm就是整合不了,找不到timeAction
3 楼 lzcsxx 2012-03-05  
能发一份能跑起来的源码吗?邮箱lzcsxx123@163.com 谢谢
2 楼 hxboss 2011-09-29  
我想问一下

Import-Package: com.example.time.service, 
com.opensymphony.xwork2, 
org.apache.struts2.osgi.interceptor, 

为什么我在Eclipse中开发这个web bundle时,总不能导入struts2的包?

是不是要在

eclipse 的 osgi 命令行中 安装 struts2 相关的 bundle ?

怎么安装?
1 楼 a4441135 2011-08-23  
osgi.FelixOsgiHost   - The [bundles] directory was not found

相关推荐

    felix+strust+spring-DM的例子

    1、解压后,里面有5个工程,名字为abc的工程是个web工程,用来部署struts插件工程,其他四个是从网上找的获取时间的例子; 2、把abc这个工程导入eclipse,发布到tomcat6.0运行; 3、访问...

    利用felix和struts2实现osgi web

    最新版本的Struts2(如2.1.8.1)开始支持与Felix OSGi的集成,使得开发者能在传统的Web开发环境中利用OSGi的模块化和动态部署特性,而无需大幅度改变开发方式。 4. 示例应用: - 时间服务示例:该示例包括一个Web...

    springDM+felix

    标题中的“springDM+felix”指的是在Java领域中,Spring Dynamic Modules (Spring DM) 和Apache Felix两个开源框架的结合应用。Spring DM是Spring框架的一个扩展,专门用于服务导向架构(SOA)和OSGi(Open Services ...

    Felix 和 Struts2 开发 Web 应用

    Struts2则是一种强大的Web应用开发框架,它继承了Struts1的优点并引入了许多改进。Struts2的核心是Action类,它是处理用户请求的中心组件。它通过拦截器(Interceptor)链来执行一系列预定义的任务,如验证输入、...

    Felix+struts2做的web示例

    标题 "Felix+Struts2做的web示例" 暗示了这是一个使用Apache Felix作为OSGi容器,并结合Struts2框架构建的Web应用程序。Apache Felix是一个开源的OSGi实现,它允许开发者将应用程序分解为模块化组件,使得代码更易于...

    SpringDM笔记7-开发SpringDM Bundle

    SpringDM(Spring Dynamic Modules)是Spring框架的一个扩展,专门用于OSGi(Open Service Gateway Initiative)环境中的应用程序开发。OSGi是一种Java模块化系统,它允许开发者将应用程序拆分成独立的、可热插拔的...

    采用共享jar包部署struts2+spring集成项目会遇到的问题

    而Spring则是一个全面的Java企业级应用开发框架,它支持依赖注入(DI)和面向切面编程(AOP),用于简化应用的配置和管理。 在部署项目时,如果多个应用共享同一个JAR包,可能会出现类加载冲突。这是因为不同的应用...

    SpringDM开发文档

    《SpringDM开发文档》是关于SpringDM框架的详细技术指南,该框架是在OSGi环境中运行Spring应用程序的关键组件。SpringDM,全称为Spring Dynamic Modules,是Spring框架针对OSGi(Open Service Gateway Initiative)...

    osgi选型比较 实例Equinox、Apache Felix与Spring DM

    3. **Spring DM (Spring OSGi)**:SpringSource(现在是VMware的一部分)开发的OSGi整合框架,使得Spring框架可以无缝集成到OSGi环境中。Spring DM提供了在OSGi环境中管理和配置服务的能力,使得开发者能使用Spring...

    OSGi与Spring:Spring DM开发

    Spring DM框架是Spring框架的一个扩展,它支持基于OSGi规范的应用程序开发。Spring DM不仅充分利用了Spring框架的强大功能,还实现了与OSGi规范的高度集成,使得开发者可以在OSGi环境下无缝地使用Spring的各种特性。...

    struts2+Hibernate+felix1.4.0 integrated

    总的来说,"struts2+Hibernate+felix1.4.0 integrated"是一个将现代Web开发技术与模块化应用管理相结合的项目,它展示了如何在Java环境中利用这些工具构建高效、可扩展的Web应用。这样的集成不仅提高了开发效率,也...

    Struts2.1.8+Felix1.4+hibernate集成源码

    Struts2.1.8+Felix1.4+hibernate集成源码,内部是自己通过查资料以及编写部分中间件host-register完成Hibernate3.1.3,Struts2.1.8,felix-1.4的集成。该部分的说明会在博文中说明,敬请关注,大家共同进步。

    SpringDM笔记31-Testing with OSGi and SpringDM

    SpringDM(Spring Dynamic Modules)是Spring框架对OSGi的支持,它简化了在OSGi环境中开发和管理应用程序的过程。 这篇笔记可能讨论了以下几个关键知识点: 1. **OSGi基础**:首先,理解OSGi的基本概念是至关重要...

    Spring DM集成Strtus2(一)

    标题“Spring DM集成Struts2(一)”指出我们要探讨的主题是关于如何在OSGi(Spring DM,即Spring Dynamic Modules)环境中集成Struts2框架。这是一个关键的Java Web开发中的技术结合,因为它允许开发者利用Spring的...

    springDM source

    SpringDM,全称为Spring Dynamic Modules,是Spring框架的一个扩展,专为基于OSGi(Open Services Gateway Initiative)的应用程序设计。OSGi是一种Java模块化系统,它允许开发人员创建可独立部署、热更新和依赖管理...

    SpringDM Runtime

    SpringDM将Spring框架的依赖注入和配置能力引入到OSGi世界,使得开发者能够利用Spring的强大功能来开发和管理OSGi服务。 **OSGi介绍** OSGi是Java平台上的一个开源标准,它定义了一种模块化体系结构,用于创建和...

    Struts2 Felix1.4搭建OSGI web infrastructure Source

    本项目结合了Struts2和Felix1.4,构建了一个基于OSGI的Web基础设施,提供了灵活和可扩展的框架来开发Web应用。 在OSGI环境中,应用被分解为小型的、独立的模块,称为bundle。每个bundle包含一组相关的类和资源,...

    Spring DM

    Spring DM,全称为Spring Dynamic Modules,是Spring框架的一个扩展,主要用于在OSGi(Open Service Gateway Initiative)环境下管理服务和应用程序。OSGi是一种模块化系统,它允许Java应用程序以模块化的形式进行...

    基于java开发的Struts图书馆管理系统.zip

    基于java开发的Struts图书馆管理系统基于java开发的Struts图书馆管理系统基于java开发的Struts图书馆管理系统基于java开发的Struts图书馆管理系统基于java开发的Struts图书馆管理系统基于java开发的Struts图书馆管理...

Global site tag (gtag.js) - Google Analytics