【问题标题】:JSTL support in Spring BootSpring Boot 中的 JSTL 支持
【发布时间】:2018-01-21 21:11:16
【问题描述】:

虽然我知道有some limitations in the JSP support,但我想在Spring Boot Web应用程序中使用带有JSTL标签的JSP。

我的 JSP 文件的摘录:

<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

...

<c:catch var="ex">
    <c:if test="${!fn:contains(param.template,'../') && !fn:startsWith(param.template,'/')}">
        <c:import url=    ...

所以,我在 pom.xml 中添加了以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

但是,如果访问jsp,则会抛出异常:

2017-08-14 13:09:32.259 ERROR 2376 --- [nio-8080-exec-2] o.a.c.c.C.[.[localhost].[/].[jsp]        : Servlet.service() for servlet [jsp] in context with path [] threw exception [The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be
 resolved in either web.xml or the jar files deployed with this application] with root cause

org.apache.jasper.JasperException: The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the jar files deployed with this application
        at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:55) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:293) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:80) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.TagLibraryInfoImpl.generateTldResourcePath(TagLibraryInfoImpl.java:251) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:122) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:434) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:492) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1448) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.Parser.parse(Parser.java:145) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:244) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.ParserController.parse(ParserController.java:105) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:203) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:374) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:351) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:335) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:601) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:368) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) ~[tomcat-embed-jasper-8.5.16.jar:8.5.16]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.t246osslab.easybuggy4sb.core.filters.SecurityFilter.doFilter(SecurityFilter.java:69) ~[classes/:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.t246osslab.easybuggy4sb.core.filters.AuthenticationFilter.doFilter(AuthenticationFilter.java:75) ~[classes/:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.t246osslab.easybuggy4sb.core.filters.EncodingFilter.doFilterInternal(EncodingFilter.java:33) ~[classes/:na]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.16.jar:8.5.16]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

有谁知道解决方案或解决方法?

环境:

  • Spring Boot 1.5.6.RELEASE

我不需要使用 Thymeleaf 的替代解决方案。


编辑:

重现步骤:

  1. git clone https://github.com/k-tamura/easybuggy4sb.git
  2. cd easybuggy4sb
  3. 将以下依赖项添加到 pom.xml。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
          <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    
  4. 在 src/main/resources/application.properties 中添加如下属性(为了避免启动时出现异常):

    server.tomcat.additional-tld-skip-patterns=*.jar
    
  5. 将 test.jsp 文件添加到 src/main/webapp:

    <%@ page pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
  6. mvn spring-boot:run
  7. 访问http://localhost:8080/test.jsp -> 出现以下异常:

    There was an unexpected error (type=Internal Server Error, status=500).
    The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the jar files deployed with this application
    

【问题讨论】:

  • 你用的是哪个版本的jstl?
  • 我使用的是 1.2。它从 Spring Boot 继承默认值。 &lt;parent&gt;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;&lt;version&gt;1.5.6.RELEASE&lt;/version&gt;&lt;/parent&gt;
  • 好的。检查 Pom.xml 中的任何其他依赖项是否也会传递 standard.jar,如果是,则排除此 jar...
  • 我尝试添加 &lt;dependency&gt;&lt;groupId&gt;taglibs&lt;/groupId&gt;&lt;artifactId&gt;standard&lt;/artifactId&gt;&lt;version&gt;1.1.2&lt;/version&gt;&lt;/dependency&gt; 但不起作用...
  • 所以你禁用 taglib 检测并且仍然期望标签工作......

标签: spring maven jsp spring-boot jstl


【解决方案1】:

您需要将jstl 依赖项与1.2 版本一起使用。您还需要将spring-boot-starter-parent 添加到您的pom.xml 中。您可以在下面找到依赖项。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

【讨论】:

  • 为什么需要 spring-boot-starter-parent ?
  • 什么意思?我应该添加 servlet-api 2.5 并降级到 1.4.0.RELEASE 版本吗?
【解决方案2】:

这个问题是由server.tomcat.additional-tld-skip-patterns=*.jar引起的。我会结束这个问题。

【讨论】:

  • 嘿,我遇到了同样的问题,我没有在 application.properties 文件中添加这一行
猜你喜欢
  • 2018-01-29
  • 2011-07-22
  • 2015-03-03
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2018-07-26
  • 1970-01-01
相关资源
最近更新 更多