0

我创建了一个 OGI 包,名为:

com.sdl.ws.integration.profserv.webservice.connector.server.external.beans

我可以在下面看到捆绑包:

 http://localhost:4502/system/console/bundles

以下是我的 JSP 代码,我正在尝试使用它来访问 bundlecontext

 <%@ page import="org.osgi.framework.BundleContext"%>  
 <%@ page import="org.osgi.framework.FrameworkUtil"%>
 <%@ page import="com.sample.osgi.components.FormattingServiceImpl.*"%>  
 <%@ page import="org.osgi.service.cm.ConfigurationAdmin"%>
 <%@ page import="org.osgi.service.cm.Configuration"%>   
  <%@ page import="org.osgi.service.packageadmin.PackageAdmin"%>   

 <%
    BundleContext bundleContext =      FrameworkUtil.getBundle(FormattingServiceImpl).getBundleContext();  

  %>

我收到以下错误:FormattingServiceImpl 无法解决

我尝试使用多种方法来导入 jar,但没有任何效果,

这是我的捆绑包的配置结构:

在此处输入图像描述

我被困在如何从 JSP 访问捆绑上下文的问题上,有人有任何建议/更正吗?

4

1 回答 1

2

您可能只是缺少 .class 来引用您的 FormattingServiceImpl 类 - 以下对我来说适用于裸 Apache Sling 实例,使用 Sling 的 Resource 接口而不是 FormattingServiceImpl 但具有相同的模式:

  <%@ page import="org.osgi.framework.BundleContext"%>  
  <%@ page import="org.osgi.framework.FrameworkUtil"%>
  <%@ page import="org.apache.sling.api.resource.Resource"%>  

  <%
    BundleContext bc = FrameworkUtil.getBundle(Resource.class).getBundleContext();
  %>

  <%= bc %>

话虽如此,在 JSP 脚本中获取 BundleContext 的 IMO 很少有有效用例,我首先会质疑是否需要这样做。

此外,您的代码似乎表明 FormattingServiceImpl 是一个实现类,并且由定义它的包导出,这通常也是一个坏主意。一般来说,bundle 应该只导出包含它们提供的服务接口的包。

于 2013-03-18T10:04:05.043 回答