这几天在看 struts2技术内幕 这本书;其中有介绍了使用jetty作为服务器,发布应用程序;
其中的方法,是将jetty嵌入工程启动;书中的例子采用的是jetty6,但是在jetty的官方网站上发现:从jetty7(包含7)开始,已经从原来的 http://jetty.codehaus.org/迁移到:http://www.eclipse.org/jetty/ ;
根据官网提供的版本说明http://www.eclipse.org/jetty/about.php:
考虑到jdk版本,servlet的版本,决定使用7.x版本
下载/而后做嵌入;具体的嵌入过程可以参考日志:Jetty7的嵌入式启动;http://my.oschina.net/u/241182/blog/117575
将struts2官方提供的struts-blank.war包的源码导入eclipse,使用Jetty7嵌入启动;出现以下异常:
发现 /struts-tags 不认,即对于 tld文件没有解析;
按照正常的方式,从servlet2.4(含2.4)版本之后,服务器应该会自动识别 jar包 META-INF目录下的tld文件,但是在jetty中存在问题;
查了好久的Google大神,都未有发现;(其实有一种解决方法:将struts-core.2.x.jar包中META-INF/struts-tags.tld文件复制到工程的WEB-INF/目录下,但是这种方式,总赶脚不爽,遂弃之;)
后想着能否在jetty的wiki(http://wiki.eclipse.org/Jetty)中找到说明;
终于在:http://wiki.eclipse.org/Jetty/Howto/Configure_JSP 其中找到了使用tld的说明:
但是这两部分;都是针对jetty作为应用服务的时候,处理的配置说明;对于嵌入启动没有太多的说明;仅有一句话:If you're deploying via code, the equivalent is to call the WebAppContext.setAttribute(String name, String value) method。
后查看了jetty的源码,发现了org.eclipse.jetty.webapp.MetaInfConfiguration,这个类是用来扫描jar 包下META-INFO目录下文件使用的,这个刚好用的上;并且发现了其中的 addResource方法可用; 尤其是在其中的processEntry方法,给了很大的启示;
遂对原来的jettyStart代码进行修改;
package test;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
/**
* @author yu
*
*/
public class JettyStart {
public static void main(String[] args) throws Exception{
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setDescriptor("./WebContent/WEB-INF/web.xml");
context.setResourceBase("./WebContent");
context.setContextPath("/sblank");
//context.set
context.setParentLoaderPriority(true); ///增加了对struts2 的tld文件的解析处理
java.net.URI uri = new java.net.URI("file:F:/learn/myspace/sblank/lib/struts2-core-2.3.14.jar");
Resource strutsjar = Resource.newResource("jar:"+uri+"!/META-INF/struts-tags.tld");
MetaInfConfiguration struts2tldCfg = new MetaInfConfiguration();
String tldPattern = "org.eclipse.jetty.tlds";
struts2tldCfg.addResource(context, tldPattern, strutsjar);
System.out.println("org.eclipse.jetty.tlds is :"+context.getAttribute(tldPattern));
server.setHandler(context);
server.start();
server.join();
}
}
重点是 其中的20-25行,指定了对struts2-core.xxx.jar文件的扫描;
重新运行JettyStart , 并进行调试,呵呵 ,struts2的标签已经可以搞定了;;同样这种方式也使用于其它的标签库;