Spring Boot JSP 404

后端 未结 14 1346
陌清茗
陌清茗 2020-11-28 07:17

I\'m trying to add a jsp page in my Spring Boot service. My problem is that every time I try to go to that page I have this:

Whitelabel Error Page

相关标签:
14条回答
  • 2020-11-28 07:31

    Ensure that you have jasper and jstl in the list of dependencies:

        <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>
    

    Here is a working starter project - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp

    0 讨论(0)
  • 2020-11-28 07:32

    You can use thymeleaf with jsp but you have to write:

    spring.thymeleaf.excluded-view-names=#jsp file without extension
    

    in application.properties file

    0 讨论(0)
  • 2020-11-28 07:33

    my issue was Spring vesrion : I found that since 1.4.3 version and above stops supporting the embedded JSPs . So I change version to 1.4.1 , it's worked for me.

    an other thing take off :

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

    it will not work with it .

    0 讨论(0)
  • 2020-11-28 07:35

    In newer versions of Spring, following needs to be put in application.properties file:

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp

    Also, JSP files need to be put under src/main/resources/META-INF/resources/WEB-INF/jsp

    0 讨论(0)
  • 2020-11-28 07:35

    We were adding Spring Boot to our system in order to run it as executable application without standalone tomcat and also faces the 404 status during JSP initialization.
    What should be done for fixing it:

    a) Add dependencies to your pom file (WARNING: tomcat-embed-jasper must have compile scope no provided):

    <parent>
     <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.3.3.RELEASE</version>
         <relativePath/>
     </parent>
     <dependencies>
       <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
       </dependency>
       <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
       </dependency>
     <dependencies>
    

    b) Add spring boot maven plugin for building your application:

    <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
    </build>
    

    c) Check that you are using *.war file for your artifact, not jar (because of JSP support limitation):

    <packaging>war</packaging>
    

    e) Now you should be able to run your spring boot application using maven plugin or with command line - java -jar /you/path/application-name.war:

    f) But if you are using multi-module maven project and want to run spring boot application using IntelliJ Idea it is important to change "Working directory" -> $MODULE_DIR$:

    0 讨论(0)
  • 2020-11-28 07:38

    Try and add your error jsp files under error folder.

    application.properties
    spring.mvc.view.prefix=/views/jsp/
    spring.mvc.view.suffix=.jsp
    
    jsp files :
    /views/jsp/error/401.jsp
    /views/jsp/error/404.jsp - to display 404 instead of default whitelabel page
    
    0 讨论(0)
提交回复
热议问题