基于OSGi的JSF Web组件开发问题求解
最近一直在研究OSGi Web组建开发,跑了一些小程序中间出现了不少问题,其中很多是由于对OSGi理解不够深入,当然还有些问题,目前也没有解决的办法,因此贴出来想让大伙帮帮忙。
具体情况如下: 1.拿纯Java代码编写的JSP页面进行测试,结果一切正常,说明JspServlet已经成功加载。 2.编写简单的JSF页面进行测试,结果出现错误提示,具体如下:
osgi> Jul 4, 2008 11:58:22 AM org.mortbay.jetty.servlet.ServletHandler handle WARNING: EXCEPTION org.apache.jasper.JasperException: Unable to read TLD "META-INF/jsf_core.tld" from JAR file "bundleentry://136/src/main/java/webapp/WEB-INF/lib/jsf-impl-1.2_04-p02.jar": org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryValidator class: com.sun.faces.taglib.jsf_core.CoreValidator at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) at javax.servlet.http.HttpServlet.service(HttpServlet.java:689) at org.eclipse.equinox.jsp.jasper.JspServlet.service(JspServlet.java:112) at javax.servlet.http.HttpServlet.service(HttpServlet.java:689) at org.eclipse.equinox.http.servlet.internal.ServletRegistration.handleRequest(ServletRegistration.java:90) at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:111) at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:67) at javax.servlet.http.HttpServlet.service(HttpServlet.java:689) at org.eclipse.equinox.http.jetty.internal.HttpServerManager$InternalHttpServiceServlet.service(HttpServerManager.java:288) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:428) at org.mortbay.jetty.servlet.ServletHandler.dispatch(ServletHandler.java:677) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:568) at org.mortbay.http.HttpContext.handle(HttpContext.java:1530) at org.mortbay.http.HttpContext.handle(HttpContext.java:1482) at org.mortbay.http.HttpServer.service(HttpServer.java:909) at org.mortbay.http.HttpConnection.service(HttpConnection.java:820) at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:986) at org.mortbay.http.HttpConnection.handle(HttpConnection.java:837) at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:245) at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357) at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
起初我怀疑可能是不是那个tld文件已经损毁了,但是当我将整个工程转换成普通Web工程在Tomcat下运行一切正常,因此,我判断,首先可以保证tld文件是正常的。
后来在网上查阅了很多这方面的资料(相关资料确实很少,而且问多答少),有些人说需要在servlet-api-2.5.jar和jsp-api.jar之间做出选择,删掉其中一个jar就可以了,而事实上是这两个jar对我进行OSGi组建开发都至关重要,因此无法删除。
注: 关于代码详情,见附件。
下面是相关用到的约束bundle: id State Bundle 0 ACTIVE org.eclipse.osgi_3.3.2.R33x_v20080105 1 ACTIVE org.eclipse.equinox.common_3.3.0.v20070426 3 ACTIVE javax.servlet_2.4.0.v200706111738 4 ACTIVE org.apache.commons.logging_1.0.4.v200706111724 5 ACTIVE org.apache.commons.el_1.0.0.v200706111724 6 ACTIVE org.apache.commons.logging_1.0.4 7 ACTIVE javax.servlet.jsp_2.0.0.v200706191603 10 ACTIVE org.eclipse.osgi.services_3.1.200.v20070605 11 ACTIVE org.eclipse.equinox.launcher_1.0.1.R33x_v20080118 12 ACTIVE org.eclipse.equinox.jsp.jasper.registry_1.0.0.v20070607 15 ACTIVE org.eclipse.equinox.http.helper_1.0.0.qualifier 16 ACTIVE org.mortbay.jetty_5.1.11.v200706111724 20 ACTIVE org.eclipse.equinox.http.registry_1.0.1.R33x_v20071231 21 ACTIVE org.eclipse.equinox.http.jetty_1.0.1.R33x_v20070816 22 ACTIVE org.eclipse.equinox.registry_3.3.1.R33x_v20070802 23 ACTIVE org.eclipse.equinox.http.servlet_1.0.1.R33x_v20070816 24 ACTIVE org.eclipse.core.jobs_3.3.1.R33x_v20070709 58 ACTIVE org.eclipse.equinox.jsp.jasper_1.0.100.qualifier 76 ACTIVE org.apache.jasper_5.5.17.v200806031609 136 ACTIVE MyJSFBundle_1.0.0
System.out.println("开始注册JspServlet。。。。");
HttpContext commonContext = new BundleEntryHttpContext(context.getBundle(), "/page");
httpService.registerResources("/page", "/", commonContext);
_jspServlet = new JspServlet(context.getBundle(), jspFolder);
Servlet adaptedJspServlet = new ContextPathServletAdaptor(_jspServlet, "/page");
httpService.registerServlet("/page" + "/*.jsp", adaptedJspServlet, null, commonContext);
System.out.println("JspServlet注册结束!");
想请问 httpService.registerServlet("/page" + "/.jsp", adaptedJspServlet, null, commonContext); 这句话是做什么?/page" + "/.jsp这个代表什么意义?
我当时估计可能是由于JsfServlet在加载的过程中出问题了,因此采用了手动加载JsfServlet的方式, 具体操作法如下: 1.新增一个ServletContextListenerServletAdaptor类,具体内容如下:
2.手动加载JsfServlet,代码如下:
这样一来,我又遇到了新的问题:
Nested Exception: java.lang.NoClassDefFoundError: Could not initialize class org.apache.jasper.compiler.JspRuntimeContext at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at com.sun.faces.config.ConfigureListener.isJspTwoOne(ConfigureListener.java:1498) at com.sun.faces.config.ConfigureListener.registerELResolverAndListenerWithJsp(ConfigureListener.java:1542) at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:403) at org.danlley.osgi.jsf.other.ServletContextListenerServletAdaptor.init(ServletContextListenerServletAdaptor.java:30) at org.eclipse.equinox.http.helper.ContextPathServletAdaptor.init(ContextPathServletAdaptor.java:32) at org.eclipse.equinox.http.servlet.internal.ServletRegistration.init(ServletRegistration.java:64) at org.eclipse.equinox.http.servlet.internal.ProxyServlet.registerServlet(ProxyServlet.java:142) at org.eclipse.equinox.http.servlet.internal.HttpServiceImpl.registerServlet(HttpServiceImpl.java:50) at org.danlley.osgi.jsf.other.Activator$HttpServiceTracker.addingService(Activator.java:78) at org.osgi.util.tracker.ServiceTracker$Tracked.trackAdding(ServiceTracker.java:1064) at org.osgi.util.tracker.ServiceTracker$Tracked.trackInitialServices(ServiceTracker.java:926) at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:330) at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:274) at org.danlley.osgi.jsf.other.Activator.start(Activator.java:32) at org.eclipse.osgi.framework.internal.core.BundleContextImpl$2.run(BundleContextImpl.java:999) at java.security.AccessController.doPrivileged(Native Method) at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:993) at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:974) at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:346) at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:260) at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:252) at org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._start(FrameworkCommandProvider.java:260) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute(FrameworkCommandInterpreter.java:150) at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:300) at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java:285) at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:221) at java.lang.Thread.run(Thread.java:619)
但事实上,这个类我已经加载到工程的classpath当中了,不知道是不是还有什么地方没有处理
Apache的JspServlet是不是有同样的package name?
检查一下classpath中(包括系统classpath)有无同名(同package)class。
如果还不行,最后edit jsf_core.tld,把validator注释掉。看能不能过。
由于我在注册JspServlet的时候使用的是org.eclipse.equinox.jsp.jasper中的org.eclipse.equinox.jsp.jasper.JspServlet,而在jsp-api.jar中也有Apache的JspServlet,这里会不会是因为这两个包之间有冲突,我是不是需要对jsp-api.jar做些手术,不知道高手们有什么见解
不知道为什么附件上传不了,因此我在这里将Activator贴出来