如果使用 tagdir 属性,JSP 编译问题与嵌入式 jetty 相关

发布于 2024-12-02 08:38:29 字数 2553 浏览 1 评论 0原文

我正在尝试测试在应用程序的 WebContent 目录中定义为 .tag 文件的 JSP 标记。 webproject布局如下:

  • Project
  • WebContent
    • WEB-INF
      • 标签
        • 标签1.标签
      • 观看次数
        • tagTest.jsp

使用独立的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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-12-09 08:38:30

正确的 JSTL 核心 taglib 声明是

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

尝试使用此声明,看看它是否可以解决问题。

The proper JSTL core taglib declaration is

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Try with this one and see if it solves the problem.

我只土不豪 2024-12-09 08:38:30

根据您使用的 jetty 版本,您可能需要启用 TLD 扫描。

我使用 Jetty 7,并且通过在要使用标记库的上下文中设置属性来启用它:

webCtx.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", 
    ".*/.*jsp-api-[^/]*\\.jar$|.*/.*jsp-[^/]*\\.jar$|.*/.*taglibs[^/]*\\.jar$");

请参阅 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 的值:

".*/org\\.apache\\.taglibs\\.standard\\.glassfish-1\\.2\\.0\\.v201112081803\\.jar"

它实际上有效,并且它确认了 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:

webCtx.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", 
    ".*/.*jsp-api-[^/]*\\.jar$|.*/.*jsp-[^/]*\\.jar$|.*/.*taglibs[^/]*\\.jar$");

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:

".*/org\\.apache\\.taglibs\\.standard\\.glassfish-1\\.2\\.0\\.v201112081803\\.jar"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文