我正在使用 Spring Boot 简单应用程序来显示 JSP。 然而,页面不是渲染 JSP,而是下载到浏览器中。 请建议?
application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tcadmin?zeroDateTimeBehavior=convertToNull
spring.datasource.username=tcuser
spring.datasource.password=tcuserpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=9090
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
server.context-path=/internal
spring.mvc.view.prefix:/
spring.mvc.view.suffix:.jsp
我创建了文件夹 src/main/webapp 并将 printReciept.jsp 放入其中。
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.a2b</groupId>
<artifactId>A2BInternalModules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>A2BInternalModules</name>
<description>Sitemap Generator for A2B</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Tomcat Embed -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>required</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
PrintRecieptController.java
@RequestMapping(value = "/PrinReciept", method = RequestMethod.GET)
public String welcome(Model model) {
model.addAttribute("message", "Hello World !!!");
return "printReciept";
}
printReciept.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Print Receipt
</body>
</html>
Controller 受到攻击,但不是渲染 JSP,而是在浏览器中下载该页面。请提出建议。
最佳答案
在 Maven 依赖项中,检查 tomcat-embed 版本并在 Maven 中心找到相关的 tomcat-jasper 版本依赖项。 JSP 编译需要此 jar。由于它没有编译,因此可以下载。
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.19</version>
</dependency>
我的嵌入 tomcat 版本是 9.0.19,我得到了与该版本相关的 jasper。
关于Spring Boot下载jsp文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47138622/