JSP文件不在Spring Boot web应用程序中呈现

我有一个使用embedded式Tomcat的Spring Boot Web应用程序(默认)。 当它提供JSP文件作为渲染我在控制器中指定的视图的一部分时,JSP不会像这样呈现,而是将内容打印出来。 例如:

的index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html lang="en"> <head></head> <body>Test</body> </html> 

在浏览器中呈现视图时,会显示上面的内容,而不是预期的内容:

 Test 

确保你的pom.xml指定了Tomcat的JSP依赖关系,如下所示:

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

看来,embedded式Tomcat将JSP呈现视为可选项。

如下所述,这个JAR有时也是必须的:

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

(我添加了,因为这个JAR应该包含在servlet容器中。

你将不需要一个,而是两个依赖项(jasper和jstl)在你的pom.xml中来工作。

  <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> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> 

更好的是你可以使用gradle(它正赶上Maven)。 在build.gradle文件中使用这个依赖关系。

// JSP所需的依赖关系

 providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper' 

也为我工作,但我不得不删除

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

对我来说就像丹提到的一样。 删除提供的范围。

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

多谢你们!

我想你错过了一些configuration,因为很容易整合JSP,只需按照下面的步骤

1 – tomcat-embad-jasper依赖

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

2 – 添加下面的configuration是application.properties

 spring.mvc.view.prefix: / spring.mvc.view.suffix: .jsp 

这仍然有一些疑问,然后检查下面的链接

Spring Boot和JSP集成

除了之前描述之外,我解决了我的问题:

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

added ViewResolver:

 @Configuration @ComponentScan @EnableWebMvc public class SpringServletConfig { @Bean public InternalResourceViewResolver resolver() { InternalResourceViewResolver vr = new InternalResourceViewResolver(); vr.setPrefix("/WEB-INF/jsps/"); vr.setSuffix(".jsp"); return vr; } } 

from: 为什么Spring MVC用404响应并报告“在DispatcherServlet中没有find具有URI的HTTP请求的映射”?

只要改变依赖关系

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

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

我面临的问题就像在浏览器中打印jsp文件名而不是其内容。

通过在pom.xml中添加下面的代码片段来呈现jsp页面,它可以正确渲染。

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