问题描述
我使用嵌入式 Tomcat(默认设置)启动并运行了一个 Spring Boot Web 应用程序.当它提供 JSP 文件作为呈现我在控制器中指定的视图的一部分时,JSP 不会被如此呈现,而是打印出内容.例如:
I have a Spring Boot web application up and running using embedded Tomcat (the default). When it serves up JSP files as part of rendering the view I specified in my controller, the JSPs are not being rendered as such, and instead print out the contents. For example:
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>
在浏览器中渲染视图时,会显示上面的内容,而不是预期的内容:
When the view is rendered in the browsers, the contents above are displayed, instead of the expected contents:
Test
推荐答案
确保你的 pom.xml
指定 Tomcat JSP 依赖如下:
Make sure that your pom.xml
specifies the Tomcat JSP dependency as follows:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
似乎嵌入式 Tomcat 将 JSP 渲染视为可选.
It seems that embedded Tomcat treats the JSP rendering as optional.
如下所述,这个 JAR 有时也是必要的:
As mentioned below, this JAR is sometimes necessary as well:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
(我添加了提供,因为这个 JAR 应该包含在 servlet 容器中.
(I added provided since this JAR should be included by the servlet container.
这篇关于JSP 文件未在 Spring Boot Web 应用程序中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!