简体   繁体   English

Spring boot 不显示 index.html

[英]Spring boot not showing index.html

I have this in com.tuke.doto.Controllers / RootController.java :我在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";
    }
}

and I have frontpage.html in main/resources/static (I also tried to move it into main/resources/templates我在main/resources/staticfrontpage.html (我也尝试将它移到main/resources/templates

When I do request on localhost:8080 I see this:当我在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

and in my console on jetbrains intellij I see this:在我的 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.)

I've also included server.error.whitelabel.enabled=false into main/resources/application.properties我还包括server.error.whitelabel.enabled=falsemain/resources/application.properties

Where is the problem?问题出在哪里? Isn't that the simplest example so why it doesn't work?这不是最简单的例子,为什么它不起作用?

EDIT maybe I should install some dependency to make it work... this is how my build.gradle looks right now:编辑也许我应该安装一些依赖项以使其工作......这就是我的 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')
}

There are a few assumptions we have to make with this question.对于这个问题,我们必须做出一些假设。 Do you mean src /main/templates?你的意思是src /main/templates 吗? We also have to assume you are using Thymeleaf?我们还必须假设您正在使用 Thymeleaf? Is that dependency in your gradle path?您的gradle路径中是否存在这种依赖性?

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

IF you are just using static html, you may not need a template library like thyme leaf.如果您只是使用静态 html,则可能不需要像百里香叶这样的模板库。 Simple return ModelAndViewObject.简单返回 ModelAndViewObject。 Place your html in src/main/resources/static将你的 html 放在 src/main/resources/static

example例子

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

Try to adding the following to your configuration class尝试将以下内容添加到您的配置类中

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

and the following to application.config以及以下到 application.config

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

put .html file inside src/main/resources/templates.html文件放入src/main/resources/templates

And then try to access localhost:8080/myapp/frontpage然后尝试访问localhost:8080/myapp/frontpage

If you are trying to simply run an html file, do ensure that the project structure is the following for a Spring Boot app如果您只是尝试运行 html 文件,请确保 Spring Boot 应用程序的项目结构如下

在此处输入图片说明

After following this structure, you will be able to access index.html via launching localhost:8080遵循此结构后,您将能够通过启动 localhost:8080 访问 index.html

For CRA React, below 2 methods can be used对于 CRA React,可以使用以下 2 种方法

  • Copy contents from CRA build folder to either target/classes/static or target/classes/public将内容从 CRA build文件夹复制到target/classes/statictarget/classes/public

This will automatically map index.html with MVC .这将自动将 index.html 与MVC映射。 You can see this during startup of spring-boot application as Adding welcome page: class path resource [public/index.html] Incase this Method fails, alternatively use below您可以在 spring-boot 应用程序的启动过程中看到这一点Adding welcome page: class path resource [public/index.html]此方法失败,也可以在下面使用

  • create a ModelAndView object as below创建一个ModelAndView对象,如下所示
@RequestMapping("/")
public ModelAndView frontend(){
  return new ModelAndView("frontend.html")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM