Why does Spring boot not support jsp while it can render the page if we add proper jar reference

后端 未结 3 716
醉话见心
醉话见心 2020-12-10 09:59

It is written everywhere that Spring boot does not support jsp view. In its official document there are three reasons

  • With Jetty and Tomcat, i
相关标签:
3条回答
  • 2020-12-10 10:04

    Embedded Tomcat package (which is used in springboot to create executable jar)does not include JSP by default, we must add the module “org.apache.tomcat.embed:tomcat-embed-jasper” as well.That is the reason why we are adding tomcat-embed-jasper as dependency in springboot, so that we can use the jstl tags in jsp.

    The main reason why springboot does not work properly with jsp as view resolver , when *jar is used as packaging is because of a hard coded file pattern in Tomcat.The issue is that when you are using java -*.jar to deploy a springboot application , the jsp files will not be present in the embedded tomcat and while trying to serve the request you will get a 404 PAGE NOT FOUND. This is because of the jar packaging ,that the jsp files are not getting copied from the WEB-INF folder.If you keep the jsp files under the META-INF/resources folder while using jar as packaging it should work.

    Thymeleaf allows using templates as prototypes, meaning they can be viewed as static files and put in resources/templates folder for spring to pick up.But jsp files will have jstl tags etc which needs to be transpiled by the jasper before rendering , so they cannot be set as static files according to my knowledge.

    When using a WAR(Webapplication archive), the packing will automatically take the resources from the following project structure :

     |-- pom.xml
     `-- src
         `-- main
             |-- java
             |   `-- com
             |       `-- example
             |           `-- projects
             |               `-- SampleAction.java
             |-- resources
             |   `-- images
             |       `-- sampleimage.jpg
             `-- webapp
                 |-- WEB-INF
                 |   `-- web.xml
                 |-- index.jsp
                 `-- jsp
                     `-- websource.jsp
    

    Guides and official sample for using springboot with jsp : Guide , Sample Repo

    The WAR packaging structure insists on keeping jsp files under webapp/ folder and it will work as expected. The maven war goal will copy the files from the webapp folder to the WEB-INF and all resource files like jsp will be at the root of the war packaging.From here, the maven-repackage goal or spring boot repackaging takes care of making the jar/war executable.So, if the files are present in the orginal war , it will be in the executable one as well.The springboot executable war structure is as follows :

    example.war
     |
     +-META-INF
     |  +-MANIFEST.MF
     +-org
     |  +-springframework
     |     +-boot
     |        +-loader
     |           +-<spring boot loader classes>
     +-WEB-INF
        +-classes
        |  +-com
        |     +-mycompany
        |        +-project
        |           +-YourClasses.class
        +-lib
        |  +-dependency1.jar
        |  +-dependency2.jar
        +-lib-provided
           +-servlet-api.jar
           +-dependency3.jar
    

    So for the comment :

    If you put your jsp files in the folder src/main/resources , anything that is put in this directory will be automatically copied to the WEB-INF/classes as per the WAR documentation.

    So , if you keep your jsp files under src/main/resources and configure the following in yml or property file, it should work for WAR archives.I haven't tried it so not sure.

    spring.mvc.view.prefix = /WEB-INF/classes/templates
    spring.mvc.view.suffix = .jsp
    
    0 讨论(0)
  • 2020-12-10 10:16

    If you choose to use JSP. As it turns out, Java servlet containers—including embedded Tomcat and Jetty containers—usually look for JSPs somewhere under /WEB-INF. But if you’re building your application as an executable JAR file, there’s no way to satisfy that requirement. Therefore, JSP is only an option if you’re building your application as a WAR file and deploying it in a traditional servlet container. If you’re building an executable JAR file, you must choose Thymeleaf, FreeMarker, or one of the other options

    0 讨论(0)
  • 2020-12-10 10:28

    Thanks Robin Sun,

    I added in pom org.apache.tomcat.embed:tomcat-embed-jasper and compiled .war and my docker images could recognise my jsps

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