JasperException springframework.org/tags/form 只能在嵌入式 Tomcat 中解决

JasperException springframework.org/tags/form cannot be resolved only in Embedded Tomcat

为什么在嵌入式 Tomcat 中使用 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 会出现以下错误?如果我在没有嵌入依赖的情况下尝试使用相同的 JSP 和相同的 POM 并将其部署到 Tomcat 服务器,则根本不会出现错误。

我在这里阅读了一些问题,其中有类似问题的一些答案建议添加某些依赖项。我试过了,但老实说,我认为问题与嵌入式 Tomcat.

直接相关

closest question found 让我想知道我在应用程序启动期间缺少一些配置,但我不知道在我的情况下在应用程序启动期间我还可以设置什么。

首发class:

public class App
{
    private static File getRootFolder() {
        try {
            File root;
            String runningJarPath = App.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().replaceAll("\\", "/");
            int lastIndexOf = runningJarPath.lastIndexOf("/target/");
            if (lastIndexOf < 0) {
                root = new File("");
            } else {
                root = new File(runningJarPath.substring(0, lastIndexOf));
            }
            System.out.println("application resolved root folder: " + root.getAbsolutePath());
            return root;
        } catch (URISyntaxException ex) {
            throw new RuntimeException(ex);
        }
    }
    public static void main(String[] args) throws Exception {
        File root = getRootFolder();
System.setProperty("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", "true");
        Tomcat tomcat = new Tomcat();
        Path tempPath = Files.createTempDirectory("tomcat-base-dir");
        tomcat.setBaseDir(tempPath.toString());
        //The port that we should run on can be set into an environment variable
        //Look for that variable and default to 8080 if it isn't there.
        String webPort = System.getenv("PORT");
        if (webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }
        tomcat.setPort(Integer.valueOf(webPort));
        File webContentFolder = new File(root.getAbsolutePath(), "src/main/webapp/");
        if (!webContentFolder.exists()) {
            webContentFolder = Files.createTempDirectory("default-doc-base").toFile();
        }
        StandardContext ctx = (StandardContext) tomcat.addWebapp("", webContentFolder.getAbsolutePath());
        //Set execution independent of current thread context classloader (compatibility with exec:java mojo)
        ctx.setParentClassLoader(App.class.getClassLoader());
        //Disable TLD scanning by default
        if (System.getProperty(Constants.SKIP_JARS_PROPERTY) == null && System.getProperty(Constants.SKIP_JARS_PROPERTY) == null) {
            System.out.println("disabling TLD scanning");
            StandardJarScanFilter jarScanFilter = (StandardJarScanFilter) ctx.getJarScanner().getJarScanFilter();
            jarScanFilter.setTldSkip("*");
       }
        System.out.println("configuring app with basedir: " + webContentFolder.getAbsolutePath());
        // Declare an alternative location for your "WEB-INF/classes" dir
        // Servlet 3.0 annotation will work
        File additionWebInfClassesFolder = new File(root.getAbsolutePath(), "target/classes");
        WebResourceRoot resources = new StandardRoot(ctx);
        WebResourceSet resourceSet;
        if (additionWebInfClassesFolder.exists()) {
            resourceSet = new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClassesFolder.getAbsolutePath(), "/");
            System.out.println("loading WEB-INF resources from as '" + additionWebInfClassesFolder.getAbsolutePath() + "'");
        } else {
            resourceSet = new EmptyResourceSet(resources);
        }
        resources.addPreResources(resourceSet);
        ctx.setResources(resources);
        tomcat.start();
        tomcat.getServer().await();
    }
}

错误:

    org.apache.jasper.JasperException: The absolute uri: http://www.springframework.org/tags/form cannot be resolved in either web.xml or the jar files deployed with this application
       org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:55)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:277)
           org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:75)
            org.apache.jasper.compiler.TagLibraryInfoImpl.generateTldResourcePath(TagLibraryInfoImpl.java:243)
            org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:124)
           org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:421)
            org.apache.jasper.compiler.Parser.parseDirective(Parser.java:479)
            org.apache.jasper.compiler.Parser.parseElements(Parser.java:1435)
            org.apache.jasper.compiler.Parser.parse(Parser.java:139)
           org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)
         org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
            org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
            org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
            org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
            org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
            org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
           org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.apache.jasper.JasperException: The absolute uri: http://www.springframework.org/tags/form cannot be resolved in either web.xml or the jar files deployed with this application
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:55)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:277)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:75)
org.apache.jasper.compiler.TagLibraryInfoImpl.generateTldResourcePath(TagLibraryInfoImpl.java:243)
            org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:124)
org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:421)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:479)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1435)
            org.apache.jasper.compiler.Parser.parse(Parser.java:139)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
            org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
            org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
            org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

Index.jsp:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h2>Uploading a file.. </h2>
      <form:form method="POST" commandName="file"     enctype="multipart/form-data">
              Upload your file please:
              <input type="file" name="file" />
              <input type="submit" value="upload2" />
              <form:errors path="file" cssStyle="color: #ff0000;" />
       </form:form>
</body>
</html>

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.myCompany</groupId>
       <artifactId>AuthFileUpload</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>
       <name>AuthFileUpload</name>
       <url>http://maven.apache.org</url>
       <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <tomcat.version>8.0.32</tomcat.version>
              <java.version>1.8</java.version>
              <maven.compiler.plugin.version>2.1</maven.compiler.plugin.version>
              <spring.version>4.2.5.RELEASE</spring.version>
       </properties>
       <dependencies>
              <dependency>
                     <groupId>org.springframework</groupId>
                     <artifactId>spring-webmvc</artifactId>
                     <version>${spring.version}</version>
              </dependency>
              <dependency>
                     <groupId>org.apache.tomcat.embed</groupId>
                     <artifactId>tomcat-embed-core</artifactId>
                     <version>${tomcat.version}</version>
              </dependency>
              <dependency>
                     <groupId>org.apache.tomcat.embed</groupId>
                     <artifactId>tomcat-embed-logging-juli</artifactId>
                     <version>${tomcat.version}</version>
              </dependency>
              <dependency>
                     <groupId>org.apache.tomcat.embed</groupId>
                     <artifactId>tomcat-embed-jasper</artifactId>
                     <version>${tomcat.version}</version>
              </dependency>
              <dependency>
                     <groupId>org.apache.tomcat</groupId>
                     <artifactId>tomcat-jasper</artifactId>
                     <version>${tomcat.version}</version>
              </dependency>
              <dependency>
                     <groupId>org.apache.tomcat</groupId>
                     <artifactId>tomcat-jasper-el</artifactId>
                     <version>${tomcat.version}</version>
              </dependency>
              <dependency>
                     <groupId>org.apache.tomcat</groupId>
                     <artifactId>tomcat-jsp-api</artifactId>
                     <version>${tomcat.version}</version>
              </dependency>

              <!-- the next dependencies I added because I read in somewhere someone has fixed similar error by adding them-->
              <dependency>
                     <groupId>commons-fileupload</groupId>
                     <artifactId>commons-fileupload</artifactId>
                     <version>1.3.1</version>
              </dependency>
              <dependency>
                     <groupId>javax.servlet</groupId>
                     <artifactId>jstl</artifactId>
                     <version>1.2</version>
              </dependency>
              <dependency>
                     <groupId>javax.servlet</groupId>
                     <artifactId>servlet-api</artifactId>
                     <version>2.5</version>
              </dependency>
              <dependency>
                     <groupId>taglibs</groupId>
                     <artifactId>standard</artifactId>
                     <version>1.1.2</version>
              </dependency>
              <dependency>
                     <groupId>org.springframework.security</groupId>
                     <artifactId>spring-security-taglibs</artifactId>
                     <version>4.0.4.RELEASE</version>
              </dependency>
       </dependencies>
       <build>
              <finalName>embeddedTomcatSample</finalName>
              <plugins>
                     <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-compiler-plugin</artifactId>
                           <version>${maven.compiler.plugin.version}</version>
                           <configuration>
                                  <source>${java.version}</source>
                                  <target>${java.version}</target>
                           </configuration>
                     </plugin>
                     <plugin>
                           <groupId>org.codehaus.mojo</groupId>
                           <artifactId>appassembler-maven-plugin</artifactId>
                           <version>1.1.1</version>
                           <configuration>
<assembleDirectory>target</assembleDirectory>
                                  <programs>
                                         <program>
<mainClass>launch.Main</mainClass>
                                                <name>webapp</name>
                                         </program>
                                  </programs>
                           </configuration>
                           <executions>
                                  <execution>
                                         <phase>package</phase>
                                         <goals>
                                                <goal>assemble</goal>
                                         </goals>
                                  </execution>
                           </executions>
                     </plugin>
              </plugins>
       </build>
</project>

*** 2016 年编辑/March/03

运行 成功从 Eclipse

应用程序解析根文件夹:C:\STS\wsRestTemplate\AuthFileUpload

正在使用 basedir 配置应用程序:C:\STS\wsRestTemplate\AuthFileUpload\src\main\webapp

正在从 'C:\STS\wsRestTemplate\AuthFileUpload\target\classes'

加载 WEB-INF 资源

运行 来自 java -jar myExecutableJar.jar 我得到了 HTTP 状态 404 - /upload in browser

应用程序解析根文件夹:C:\Program Files\Java\jdk1.8.0_45\jre

正在使用 basedir 配置应用程序:C:\Users\e049447\AppData\Local\Temp\default-doc-base2930321463161270235

*** 我们可以看到没有 "loading WEB-INF..." INFO。显然,因为 additionWebInfClassesFolder.exists() 返回了 false 然后 "resourceSet = new EmptyResourceSet(resources)"。在这种类似于 Eclipse 的情况下,我必须加载 WEB-INF 什么?

您正在禁用 TLD 扫描:

   if (System.getProperty(Constants.SKIP_JARS_PROPERTY) == null && System.getProperty(Constants.SKIP_JARS_PROPERTY) == null) {
        System.out.println("disabling TLD scanning");
        StandardJarScanFilter jarScanFilter = (StandardJarScanFilter) ctx.getJarScanner().getJarScanFilter();
        jarScanFilter.setTldSkip("*");
   }

评论前面几行,它应该可以工作。

干杯