Spring boot 不显示index.html

问题描述 投票:0回答:6

我有这个在

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";
    }
}

并且我在

frontpage.html
中有
main/resources/static
(我也尝试将其移至
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')
}
java spring
6个回答
4
投票

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

示例

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

2
投票

对于这个问题我们必须做出一些假设。 您是指 src/main/templates 吗? 我们还必须假设您正在使用 Thymeleaf? 你的 gradle 路径中有这种依赖吗?

compile 'org.springframework.boot:spring-boot-starter-thymeleaf'

0
投票

尝试将以下内容添加到您的配置类中

    @Bean
    public EmbeddedServletContainerFactory factory(){
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setContextPath("/myapp");
        return factory;
    }

以及 application.config 中的以下内容

spring.resources.chain.strategy.fixed.enabled=true
spring.resources.chain.strategy.fixed.paths=/**

.html
文件放入
src/main/resources/templates

然后尝试访问

localhost:8080/myapp/frontpage


0
投票

如果您尝试简单地运行 html 文件,请确保 Spring Boot 应用程序的项目结构如下

enter image description here

遵循此结构后,您将能够通过启动 localhost:8080 来访问index.html


0
投票

对于CRA React,可以使用以下2种方法

  • 将 CRA
    build
    文件夹中的内容复制到
    target/classes/static
    target/classes/public

这将自动将index.html 与

MVC
进行映射。您可以在 spring-boot 应用程序启动期间看到这一点,如
Adding welcome page: class path resource [public/index.html]
如果此方法失败,也可以使用下面的方法

  • 创建一个
    ModelAndView
    对象如下
@RequestMapping("/")
public ModelAndView frontend(){
  return new ModelAndView("frontend.html")
}

0
投票

据我了解您的查询,请包含在控制器类中 导入 org.springframework.web.bind.annotation.RequestMapping;

100% 有效

© www.soinside.com 2019 - 2024. All rights reserved.