Spring boot: 404 error when calling JSP using controller

后端 未结 3 668
死守一世寂寞
死守一世寂寞 2021-01-11 20:15

I\'m getting the following error when running my project using Spring Tool Suite,

But in case my problem is I have already added the appropriate dependencie

相关标签:
3条回答
  • 2021-01-11 20:52

    You may also need to add this in pom.xml

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

    UPDATE 1:

    JSP Limitation

    When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

    • With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
    • Undertow does not support JSPs.
    • Creating a custom error.jsp page does not override the default view
      for error handling. Custom error pages should be used instead.

    Scope

    compile This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

    provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

    runtime This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

    Also, Try to change the Following in tomcat-embed-jasper

    Remove <scope>provided</scope> OR change the scope to compile <scope>compile</scope>
    

    JSP Limitations Spring Boot JSP 404

    0 讨论(0)
  • 2021-01-11 20:59

    Looks like you was very close to the working application. The main issue in your code is in <scope>provided</scope> for your Jasper dependency. And also looks like you are running your code from eclipse IDE through the main method.

    Long story short:

    If you would like to run your application through the main method in MyApplication.java then just remove scope provided for the Jasper.

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

    Or you can run your application exactly in that state like you have right now from the console:

    mvn clean spring-boot:run
    

    But I suggest to remove this scope so you could be able to run your code from IDE and from console. In addition to that looks like spring-boot-starter-tomcat dependency is redundant (it must be available within spring-boot-starter-web). In a nutshell please try to use following pom file:

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/>
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
            </dependency>
        </dependencies>
    </project>
    

    Hope my answer will help you.

    0 讨论(0)
  • 2021-01-11 21:06

    I was able to generate a jar from my application and then run it with java -jar myapp.jar But I only managed to run this jar with the version below spring-boot-starter-parent:

    MyApp/pom.xml:

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

    I researched in: Spring Boot JSP 404

    0 讨论(0)
提交回复
热议问题