java - 可执行的 Spring Boot jar 无法解析 View

标签 java spring-boot

我在构建可执行的 spring boot jar 文件时遇到问题。

配置:

@SpringBootApplication
public class RootConfig {

    public static void main(String[] args) {
        SpringApplication.run(RootConfig.class, args);
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
}

Controller :

@Controller
public class Index {

    @RequestMapping(value = "/")
    public String index() {
        return "index";
    }
}

pom:

<?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>groupId</groupId>
    <artifactId>bootTest2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>provided</scope>-->
        </dependency>
    </dependencies>

</project>

jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  Test page!
  </body>
</html>

结构:

structure

从想法开始:

idea

从命令行启动可执行jar:

enter image description here

如何解决这样的问题?

最佳答案

运行使用嵌入式 servlet 容器(并打包为可执行存档)的 Spring Boot 应用程序时,JSP 支持存在一些限制。

  1. With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work because of a hard coded file pattern in Tomcat.
  2. With Jetty it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to any standard container.

您可能想看看这个示例 here

Reference

关于java - 可执行的 Spring Boot jar 无法解析 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43606882/

相关文章:

java - 如何使用Spring Boot对WebFlux进行异常处理?

reactjs - React + springboot csrf

spring - 如何检测嵌入式或独立的应用程序服务器?

java - Java 语言中的美化类

java - 通过 iframe 加载时文件保存对话框出现两次

java - 父类返回子类

java - 将 OnMapClickListener 与 Intent Activity 结合使用

java - spring boot 并行传出请求的最佳实践是什么?

java - 在 Java 中不使用 replace 替换字符串中的单词

spring-boot - 我可以从 Spring Security Oauth 检索访问 token 吗?