如果我们添加适当的jar引用,为什么Spring Boot不支持jsp却可以呈现页面,为什么它不支持jsp [英] Why does Spring boot not support jsp while it can render the page if we add proper jar reference
问题描述
Spring Boot不支持jsp视图的地方到处都有写.在其官方文件中有三个原因
It is written everywhere that Spring boot does not support jsp view. In its official document there are three reasons
- 对于Jetty和Tomcat,如果使用战争包装,它应该可以工作.与java -jar一起启动时,可执行的war将起作用,并且也将 可部署到任何标准容器.在以下情况下不支持JSP 使用可执行jar.
- Undertow不支持JSP.
- 创建自定义error.jsp页面不会覆盖默认视图以进行错误处理.应改用自定义错误页面.
- 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.
对于第一项,使用可执行jar时不支持JSP".但是,当我添加对tomcat-embed-jasper的引用并在application.properties中设置正确的资源路径时,jsp文件也可以很好地呈现.
For the first item, 'JSPs are not supported when using an executable jar'. But when I add reference to tomcat-embed-jasper and set correct resource path in application.properties, the jsp file can also be rendered well.
我想这可能意味着如果不使用其他参考库(例如tomcat-embed-jasper),Spring Boot将不支持jsp.
I guess this may mean that Spring boot not support jsp without invovling other reference libs such as tomcat-embed-jasper.
但是对于百里香,我们还必须导入spring-boot-starter-thymeleaf.为什么我们可以说Spring Boot通过附加的库来支持thymleaf.
But for thymleaf, we also have to import spring-boot-starter-thymeleaf. Why can we say that Spring boot support thymleaf with involving extra libs.
那么我如何理解文档中的第一项?
So how can I understand the first item in document?
推荐答案
嵌入式Tomcat软件包(在springboot中用于创建可执行jar)默认情况下不包括JSP,我们还必须添加模块org.apache.tomcat.embed:tomcat-embed-jasper
.这就是我们在springboot中添加tomcat-embed-jasper
作为依赖项的原因,以便我们可以在jsp中使用jstl标记.
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.
当使用*jar
进行打包时,springboot不能与jsp作为视图解析器一起正常工作的主要原因是 Tomcat中的硬编码文件模式.问题在于,当您正在使用java -*.jar
部署springboot应用程序,则jsp文件将不会出现在嵌入式tomcat中,并且在尝试满足请求时,您会得到 404页面未找到.这是因为jar打包,所以不会从WEB-INF
文件夹复制jsp文件.如果使用jar打包时将jsp文件保留在META-INF/resources
文件夹下,则应该可以.
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允许使用模板作为原型,这意味着可以将它们视为静态文件,并放在resources/templates
文件夹中以供Spring拾取.但是jsp文件将具有jstl标签等,在渲染之前jasper需要对其进行编译,因此据我所知它们无法设置为静态文件.
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.
使用 WAR (Web应用程序存档)时,打包将自动从以下项目结构中获取资源:
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
将springboot与jsp一起使用的指南和官方示例:指南,示例回购
Guides and official sample for using springboot with jsp : Guide , Sample Repo
WAR打包结构坚持将jsp文件保存在webapp/
文件夹下,并且它将按预期工作. maven war目标会将文件从webapp文件夹复制到WEB-INF
,所有资源文件(如jsp)将位于war包装的根目录. jar/war可执行文件.因此,如果文件存在于原始war中,它也将位于可执行文件中. springboot可执行文件war结构如下:
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
所以请发表评论:
如果将jsp文件放在文件夹src/main/resources
中,则根据 WAR
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.
因此,如果将jsp文件保存在src/main/resources下,并在yml或属性文件中配置以下内容,则该文件应可用于WAR存档.我还没有尝试过,所以不确定.
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
这篇关于如果我们添加适当的jar引用,为什么Spring Boot不支持jsp却可以呈现页面,为什么它不支持jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!