内置Jetty配置JSP支持过程中的常见报错

1. 常见报错及解决

内置jetty,jsp支持报错,基本都是jetty、jsp相关依赖配置问题。下面是几个常见报错的例子:

1.1 JSP support not configured

JSP support not configured

解决: 检查pom文件是否有相关依赖,如果没有则添加。注意使用9.3.0.M1版本不行。使用release版本可以,如:

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-jsp</artifactId>
            <version>9.2.26.v20180806</version>
        </dependency>

注意jetty9.2以后使用apache-jsp

1.2 JSTL标签解析

解决: taglib加上"_rt"

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

更新成:

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>

更新taglib后,问题解决。

1.3 JSP编译

  • org.apache.jasper.JasperException: Unable to compile class for JSP

JasperException: Unable to compile class for JSP

解决:添加WebXmlConfiguration、AnnotationConfiguration

        Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
        classlist.clear();
        classlist.add("org.eclipse.jetty.webapp.WebXmlConfiguration");
        classlist.add("org.eclipse.jetty.annotations.AnnotationConfiguration");
        server.setHandler(getServletContextHandler());

1.4 JSP实现依赖

  • NoClassDefFoundError: org/apache/jasper/runtime/JspSourceImports
    JasperException NoClassDefFoundError:JspSourceImports

解决:jetty9.2之前jsp实现是Glassfish,9.2之后为Apache,对应依赖jetty-jsp更新为apache-jsp

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>apache-jsp</artifactId>
            <version>9.4.10.v20180503</version>
        </dependency>

如果jetty-jsp和apache-jsp依赖同时都添加了,则有以下报错:

  • org.apache.jasper.JasperException: javax.el.ELException: java.lang.IllegalAccessException
  • org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.apache.jasper.runtime.TagHandlerPool.get(Ljava/lang/Class;)Ljavax/servlet/jsp/tagext/Tag;

NestedServletException

NestedServletException: Handler dispatch failed nested exception is java.lang.NoSuchMethodError: org.apache.jasper.runtime.TagHandlerPool

1.5 EL表达式支持

  • org.apache.jasper.JasperException: javax.servlet.ServletException: javax.el.ELException: Provider com.sun.el.ExpressionFactoryImpl not found
    添加相关依赖即可:
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2</version>
        </dependency>

备注:javax.el.javax.el-api javax.el-api-3.0.0.jar中无此ExpressionFactoryImpl实现。

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>

2. 小结

注意相关版本依赖关系即可。pom相关配置:

        <properties>
                <jetty.version>9.4.10.v20180503</jetty.version>
        </properties>

        <!-- web容器- jetty -->
        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty.aggregate/jetty-all -->
        <dependency>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>jetty-all</artifactId>
            <version>${jetty.version}</version>
            <type>pom</type>
        </dependency>
        <!-- jsp实现-->
        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/apache-jsp -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>apache-jsp</artifactId>
            <version>${jetty.version}</version>
        </dependency>

        <!--el表达式-->
        <!-- https://mvnrepository.com/artifact/javax.el/el-api -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2</version>
        </dependency>
原文地址:https://www.cnblogs.com/eaglediao/p/9557719.html