首页 > 解决方案 > Spring Boot 无法呈现(非常)简单的 index.jsp

问题描述

我正在尝试渲染一个只有一个 h1 标签的 html .jsp ......感觉它应该可以工作,但它没有。

如何让 index.jsp 文件呈现到我的浏览器中?请帮忙!

我正在使用:Maven 3.5.4 Java 1.8.0_181 STS 版本:3.9.5.RELEASE

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1> hello</h1>
</body>
</html>

我成功地记录到路由被击中,但 index.jsp 没有被读取。

package com.coding.test1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
@Controller
public class Test1Application {

    public static void main(String[] args) {
        SpringApplication.run(Test1Application.class, args);
    }
    @RequestMapping("/")
    public String index() {
        System.out.println("+++++hit main+++++++");
        return "index.jsp";
    }
}

日志

+++++点击主键+++++++

浏览器显示

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 10 20:49:47 PDT 2018
There was an unexpected error (type=Not Found, status=404).
No message available

应用程序属性

spring.mvc.view.prefix=/WEB-INF/

块引用

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.coding.test1</groupId>
    <artifactId>test1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>test1</name>
    <description>test1</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

文件夹结构 https://cdn.discordapp.com/attachments/477766353709826054/488922584260411392/unknown.png (没有足够的代表张贴我的目录图片)

编辑:添加 ServletInitializer.java

package com.coding.test1;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Test1Application.class);
    }

}

记录资源:https ://github.com/GNouchi/SO-52268550/blob/master/README.md

标签: javaspringspring-mvcjspspring-boot

解决方案


在你的 application.properties 添加这一行

spring.mvc.view.suffix=.jsp

并改变

return "index.jsp"

return "index"

另外,作为旁注,我建议您将您的课程转移@controller到另一个课程。


推荐阅读