Spring启动没有显示index.html

时间:2017-07-22 01:42:00

标签: 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";
    }
}

我在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')
}

5 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

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

例如

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

答案 2 :(得分: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

答案 3 :(得分:0)

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

enter image description here

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

答案 4 :(得分:0)

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

  • 将内容从CRA build文件夹复制到target/classes/statictarget/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")
}