java - Spring引导不显示index.html

标签 java spring

我在com.tuke.doto.Controllers/RootController.java中有这个:

package com.tuke.doto.Controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RootController {

    @RequestMapping("/")
    public String root() {
        return "frontpage";
    }
}

我在 main/resources/static 中有 frontpage.html (我还尝试将其移至 main/resources/templates

当我在 localhost:8080 上请求时,我看到这个:

HTTP Status [404] – [Not Found]

Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.15

在我的 jetbrains intellij 控制台中我看到了这个:

javax.servlet.ServletException: Circular view path [error]: would dispatch back to the current handler URL [/error] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

我还将 server.error.whitelabel.enabled=false 包含到 main/resources/application.properties

问题出在哪里?这不是最简单的例子吗?为什么它不起作用?

编辑也许我应该安装一些依赖项才能使其工作...... 这就是我的 build.gradle 现在的样子:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-websocket')

    compile("org.springframework.boot:spring-boot-devtools")

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

最佳答案

如果你只是使用静态html,你可能不需要像百里香叶这样的模板库。简单返回ModelAndViewObject。将您的 html 放入 src/main/resources/static

示例

@GetMapping("/")
public ModelAndView index() {
    return new ModelAndView("frontpage.html");
}

关于java - Spring引导不显示index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45249823/

相关文章:

java - Java中线程的高效异常处理

java - ASP.NET 页面调用 Java Applet 和字段失去焦点

java - 使用restTemplate的Post方法,错误java.net.UnknownHostException

java - Spring 批处理作业存储库表未在我配置的架构中创建

java - 为什么 Spring ProxyFactoryBean 隐式定义有效?

java - 如何将 CXF 从 2.7 迁移到 3.0?

Java Long parselong 抛出 NumberformaException

java - 如何在单个 TextView 项中添加多个字符串值

java - 使 Java volatile 工作

java - Spring:如何使过滤器抛出自定义异常?