JasperException springframework.org/tags/form只能在Embedded Tomcat中解析

时间:2016-03-02 01:49:02

标签: java spring jsp tomcat embedded-tomcat-8

为什么我通过使用<%@ taglib prefix =" form"来获取以下错误URI =" HTTP://www.springframework.org/tags/form"%>在嵌入式Tomcat中?如果我使用相同的POM尝试相同的JSP而没有嵌入式依赖关系,并将其部署到Tomcat服务器,那么根本就没有错误。

我在这里读了一些类似问题的问题,一些答案建议添加某些依赖项。我试过了,但老实说,我认为这个问题与嵌入式Tomcat直接相关。

closest question found让我想知道在应用程序启动期间我错过了一些配置,但在我的情况下,我还没弄清楚在应用程序启动过程中我还能设置什么。

入门级:

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年/ 3月/ 03年编辑

从Eclipse

成功运行

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

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

从as&#39; C:\ STS \ wsRestTemplate \ AuthFileUpload \ target \ classes&#39;

加载WEB-INF资源

从java -jar myExecutableJar.jar运行,我得到HTTP状态404 - /在浏览器中上传

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

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

***我们可以看到没有&#34;加载WEB-INF ...&#34;信息。显然,因为additionWebInfClassesFolder.exists()返回false 那么&#34; resourceSet = new EmptyResourceSet(resources)&#34;。在这个场景中我需要加载WEB-INF,就像在Eclipse中一样?

1 个答案:

答案 0 :(得分:0)

您正在停用TLD扫描:

/[^a-zA-Z0-9\s]|\s\s+/

评论前面的行,它应该有效。

干杯