Rob*_*Sun 3 java spring jsp thymeleaf spring-boot
到处都写有Spring Boot不支持jsp视图。在其正式文件中有三个原因
- 对于Jetty和Tomcat,如果使用战争包装,它应该可以工作。与java -jar一起启动时,可执行的war将起作用,并且还将可部署到任何标准容器中。使用可执行jar时,不支持JSP。
- Undertow不支持JSP。
- 创建定制的error.jsp页面不会覆盖默认视图以进行错误处理。应改用自定义错误页面。
对于第一项,“使用可执行jar时不支持JSP”。但是,当我添加对tomcat-embed-jasper的引用并在application.properties中设置正确的资源路径时,jsp文件也可以很好地呈现。
我想这可能意味着如果不调用其他参考库(例如tomcat-embed-jasper),Spring Boot将不支持jsp。
但是对于百里香,我们还必须导入spring-boot-starter-thymeleaf。为什么我们可以说Spring Boot通过附加的库来支持thymleaf。
那么我如何理解文档中的第一项呢?
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
Run Code Online (Sandbox Code Playgroud)
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 :
Run Code Online (Sandbox Code Playgroud)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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
282 次 |
| 最近记录: |