猿问

Spring Boot Web应用程序中未呈现JSP文件

我有一个使用嵌入式Tomcat(默认设置)启动并运行的Spring Boot Web应用程序。当它提供JSP文件作为渲染我在控制器中指定的视图的一部分时,JSP不会这样渲染,而是打印出内容。例如:


index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<!DOCTYPE html>

<html>

    <head></head>

    <body>Test</body>

</html>

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


Test


MM们
浏览 679回答 3
3回答

猛跑小猪

确保您pom.xml指定了Tomcat JSP依赖关系,如下所示:<dependency>&nbsp; &nbsp; <groupId>org.apache.tomcat.embed</groupId>&nbsp; &nbsp; <artifactId>tomcat-embed-jasper</artifactId>&nbsp; &nbsp; <scope>provided</scope></dependency>似乎嵌入式Tomcat将JSP呈现视为可选的。如下所述,有时还需要此JAR:<dependency>&nbsp; &nbsp; <groupId>javax.servlet</groupId>&nbsp; &nbsp; <artifactId>jstl</artifactId>&nbsp; &nbsp; <scope>provided</scope></dependency>(我补充说,因为该JAR应该包含在servlet容器中。

炎炎设计

您不需要一个依赖关系,而只需两个依赖关系(jasper和jstl)pom.xml即可。&nbsp; &nbsp;<dependencies>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.tomcat.embed</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>tomcat-embed-jasper</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <scope>provided</scope>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.servlet</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jstl</artifactId>&nbsp; &nbsp; </dependency></dependencies>

缥缈止盈

对我来说,工作就像Dan提到的一样。删除提供的范围。<dependency>&nbsp; <groupId>org.apache.tomcat.embed</groupId>&nbsp; <artifactId>tomcat-embed-jasper</artifactId></dependency><dependency>&nbsp; <groupId>javax.servlet</groupId>&nbsp; <artifactId>jstl</artifactId></dependency>多谢你们!
随时随地看视频慕课网APP
我要回答