spring boot 下载jsp文件

Jee*_*ets 7 spring spring-boot

我正在使用 spring boot 简单应用程序来显示 JSP。然而,不是呈现 JSP,而是在浏览器中下载页面。请建议?

应用程序属性

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
Run Code Online (Sandbox Code Playgroud)

我已经创建了文件夹 src/main/webapp 并将 printReciept.jsp 放在其中。

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>
Run Code Online (Sandbox Code Playgroud)

打印接收控制器.java

@RequestMapping(value = "/PrinReciept", method = RequestMethod.GET)
    public String welcome(Model model) {
        model.addAttribute("message", "Hello World !!!");
        return "printReciept";
    }
Run Code Online (Sandbox Code Playgroud)

打印收据.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>
Run Code Online (Sandbox Code Playgroud)

控制器受到攻击,但不是呈现 JSP,而是在浏览器中下载该页面。请提出建议。

小智 8

在 Maven 依赖项中检查 tomcat-embed 版本并在 maven central 中找到相关的 tomcat-jasper 版本依赖项。JSP 编译需要这个jar。由于未编译,因此可以下载。

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.19</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

我的嵌入 tomcat 版本是 9.0.19,我得到了与该版本相关的 jasper。


小智 5

我遇到了同样的错误,但我没有将负责转换 jsp 文件的 tomcat jasper 放入其中,

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.16</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

只需根据你spring boot使用tomcat添加tomcat-jasper即可


Pyt*_*try 2

您缺少 JEE 依赖项。

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

另外,我强烈建议您将所有 JSP 放入 WEB-INF 文件夹中(对于任何模板引擎都是如此)并选择除 root 之外的前缀。如果您还希望从同一应用程序提供一些类似 REST 的端点,那么它会更安全、更灵活。

您还可以扩展 WebMvcConfigurerAdapter 并覆盖适用的方法。

// Add the JSP view resolver.
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
    registry.jsp();
    // OR
    registry.jsp("/",".jsp");
}
//... snip
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry
        .addViewController("/yourpath")
        .setViewName("yourtemplate");
}
Run Code Online (Sandbox Code Playgroud)

“addViewControllers”很好用,因此您不必为每个通用 JSP 和部分创建控制器。请注意,我没有将“.jsp”添加到视图名称中。

您可以使用根上下文作为前缀,并仍然使用上面的配置。