如果使用 tagdir 属性,JSP 编译问题与嵌入式 jetty 相关
我正在尝试测试在应用程序的 WebContent 目录中定义为 .tag 文件的 JSP 标记。 webproject布局如下:
- Project
- WebContent
- WEB-INF
- 标签
- 标签1.标签
- 观看次数
- tagTest.jsp
- 标签
- WEB-INF
使用独立的jetty,我尝试加载测试jsp。 tagTest.jsp 只是标记文件的包装器,并使用 tagdir 属性调用它,如下所示:
<%@ taglib prefix="test" tagdir="/WEB-INF/tags" %>
<test:tag1 model="${cat}" />
我已经像这样设置了服务器:
WebAppContext webCtx = new WebAppContext();
webCtx.setContextPath("/jsptest");
webCtx.setDescriptor("WebContent/WEB-INF/test-web.xml");
webCtx.setResourceBase("WebContent");
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[] { webCtx });
server.setHandler(contexts);
我已经设置了一个将请求转发到测试 jsp 的 servlet,因为我无法从 的子目录调用 jsp直接 WebContent:
request.setAttribute("cat", new SomeModel());
getServletContext().getRequestDispatcher("/WEB-INF/views/tagTest.jsp").forward(request, response);
调用此 servlet 会出现以下错误(如果我直接在 WebContent 下复制测试 jsp 并调用为 /jsptest/tagTest.jsp,也会出现相同的错误)
org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:634)
at org.apache.jasper.servlet.JspServletWrapper.loadTagFile(JspServletWrapper.java:280)
at org.apache.jasper.compiler.TagFileProcessor.loadTagFile(TagFileProcessor.java:660)
at org.apache.jasper.compiler.TagFileProcessor.access$000(TagFileProcessor.java:91)
at org.apache.jasper.compiler.TagFileProcessor$TagFileLoaderVisitor.visit(TagFileProcessor.java:719)
...
Caused by: java.lang.NullPointerException
at org.apache.taglibs.standard.tlv.JstlBaseTLV.validate(JstlBaseTLV.java:149)
at org.apache.taglibs.standard.tlv.JstlCoreTLV.validate(JstlCoreTLV.java:105)
at org.apache.jasper.compiler.TagLibraryInfoImpl.validate(TagLibraryInfoImpl.java:949)
at org.apache.jasper.compiler.Validator.validateXmlView(Validator.java:1921)
at org.apache.jasper.compiler.Validator.validate(Validator.java:1888)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:223)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:625)
如何使用嵌入的 jetty 配置 JSP tagdir 标签?如果使用 tagdir,我需要发动战争吗? 常规 jsp 加载正常,标签 uri 正确解析:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> - this works
I'm trying to test JSP tags that are defined as .tag files in my application's WebContent directory. The webproject layout is as follows:
- Project
- WebContent
- WEB-INF
- tags
- tag1.tag
- views
- tagTest.jsp
- tags
- WEB-INF
Using standalone jetty, I'm trying to load test jsp. tagTest.jsp is just a wrapper over the tagfile and invokes it using tagdir attribute like:
<%@ taglib prefix="test" tagdir="/WEB-INF/tags" %>
<test:tag1 model="${cat}" />
I have setup the server like this:
WebAppContext webCtx = new WebAppContext();
webCtx.setContextPath("/jsptest");
webCtx.setDescriptor("WebContent/WEB-INF/test-web.xml");
webCtx.setResourceBase("WebContent");
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[] { webCtx });
server.setHandler(contexts);
I have set up a servlet that forwards the request to test jsp since I was not able to invoke jsp from subdirectory of WebContent directly:
request.setAttribute("cat", new SomeModel());
getServletContext().getRequestDispatcher("/WEB-INF/views/tagTest.jsp").forward(request, response);
Invoking this servlet gives following error (also same error if I copy test jsp directly under WebContent and invoke as /jsptest/tagTest.jsp)
org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:634)
at org.apache.jasper.servlet.JspServletWrapper.loadTagFile(JspServletWrapper.java:280)
at org.apache.jasper.compiler.TagFileProcessor.loadTagFile(TagFileProcessor.java:660)
at org.apache.jasper.compiler.TagFileProcessor.access$000(TagFileProcessor.java:91)
at org.apache.jasper.compiler.TagFileProcessor$TagFileLoaderVisitor.visit(TagFileProcessor.java:719)
...
Caused by: java.lang.NullPointerException
at org.apache.taglibs.standard.tlv.JstlBaseTLV.validate(JstlBaseTLV.java:149)
at org.apache.taglibs.standard.tlv.JstlCoreTLV.validate(JstlCoreTLV.java:105)
at org.apache.jasper.compiler.TagLibraryInfoImpl.validate(TagLibraryInfoImpl.java:949)
at org.apache.jasper.compiler.Validator.validateXmlView(Validator.java:1921)
at org.apache.jasper.compiler.Validator.validate(Validator.java:1888)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:223)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:625)
How to configure JSP with jetty embedded that have tagdir tags? Do I need to have a war if tagdir's are used?
Regular jsp is getting loaded fine, tag uri is getting resolved correctly:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> - this works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的 JSTL 核心 taglib 声明是
尝试使用此声明,看看它是否可以解决问题。
The proper JSTL core taglib declaration is
Try with this one and see if it solves the problem.
根据您使用的 jetty 版本,您可能需要启用 TLD 扫描。
我使用 Jetty 7,并且通过在要使用标记库的上下文中设置属性来启用它:
请参阅 http://wiki.eclipse.org/Jetty/Howto/Configure_JSP#Using_JSTL_Taglibs_for_Jetty_7.x_and_8.x
在我的项目(使用 maven)中,我的标准 TLD 位于 JAR“org. apache.taglibs.standard.glassfish-1.2.0.v2011120803.jar” 理论上就足够了使用以下模式作为 ContainerIncludeJarPattern 的值:
它实际上有效,并且它确认了 jetty 在哪里找到了标签库,但我更愿意保留我在上面链接的 wiki.eclipse.org 页面上找到的先前模式。
如果您想包含自定义标签库,可能需要扩展该模式。
Depending on the version of jetty you are using, you may need to enable scanning of TLD.
I use Jetty 7 and I did enabled it by setting an attribute on the context where I want to use tag libs:
Refer to http://wiki.eclipse.org/Jetty/Howto/Configure_JSP#Using_JSTL_Taglibs_for_Jetty_7.x_and_8.x
On my project (using maven), I have standard TLDs are on the JAR "org.apache.taglibs.standard.glassfish-1.2.0.v2011120803.jar" and theoretically it would be enough to use as value for ContainerIncludeJarPattern the following pattern:
It actually works and it is a confirmation of where do jetty found the tag libs, but I've preferred to leave the previous pattern which I found on the wiki.eclipse.org page linked above.
It may be required to extend the pattern if you want to include custom tag libs.