简体   繁体   English

在Spring Boot中无法显示index.html页面

[英]Cannot display the index.html page in Spring boot

I tried to create a simple spring boot application 我试图创建一个简单的spring boot应用程序

I created spring boot application class, configuration class, controller class and the index.html. 我创建了spring boot应用程序类,配置类,控制器类和index.html。

I added Thymeleaf dependencies and put html page under resources folder (\\src\\main\\resources\\templates\\index.html) 我添加了Thymeleaf依赖项并将html页面放在资源文件夹(\\ src \\ main \\ resources \\ templates \\ index.html)下

But when I run the application it gives an error 但是当我运行应用程序时,它给出了一个错误

org.thymeleaf.exceptions.TemplateInputException:
Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers

Please give me a solution. 请给我一个解决方案。

@SpringBootApplication
@ComponentScan(basePackages = {"com.mail"})
public class SpringBootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
}

Controller class 控制器类

@RestController
public class IndexController {

    @RequestMapping(value="/", method = RequestMethod.GET)
    String index(){
        System.out.println("..............hit");
        return "index";
    }

WebConfiguration for Thymeleaf Web配置的胸腺

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter{
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}

index.html page index.html页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <title>Spring Framework</title>
</head>
<body>
    <h1> Hello Spring Boot </h1>
</body>
</html>

Try to do the following: 尝试执行以下操作:

  1. Replace @RestController by @Controller; 用@Controller代替@RestController;
  2. I think you don't need WebConfiguration; 我认为您不需要WebConfiguration。 the controller returns “index” which means “render the index.html template”. 控制器返回“ index”,表示“呈现index.html模板”。 Thymeleaf will find the template in the resources/templates folder. Thymeleaf将在resources / templates文件夹中找到该模板。

Try replacing @RestController with @Controller. 尝试将@RestController替换为@Controller。 I would start from the template generated by start.spring.io. 我将从start.spring.io生成的模板开始。 And incrementally add functionalities one step at a time. 并一步一步地添加功能。

Or 要么

If you just do some googling, you will be easily able to find some sample thymeleaf projects that actually works, start from there. 如果您只是进行一些谷歌搜索,就可以轻松地从这里开始找到一些实际可行的样本百里香叶项目。

Looks like static resources are not accessible by default. 看起来静态资源默认情况下不可访问。 Try to provide access to static resources adding resource handler, something like: 尝试通过添加资源处理程序来提供对静态资源的访问,例如:

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("");
    }
}

As described by previous answers, @RestController should be replaced with @Controller . 如先前的答案所述, @RestController应该替换为@Controller

When we use @RestController , return statement value will be sent as String Response . 当我们使用@RestController时 ,return语句的值将作为String Response发送。 But when we use @Controller the return statement value will be considered as the view to be rendered. 但是,当我们使用@Controller时 ,return语句的值将被视为要呈现的视图。

Refer Diff. 请参阅差异。 b/w @RestController and @Controller 黑白@RestController和@Controller

Also, Spring Boot will automatically look for index.html in '\\src\\main\\resources\\templates\\' which is the default location for views in Spring Boot when we are using Template Engines like Thymeleaf. 另外,当我们使用Thymeleaf之类的模板引擎时,Spring Boot将自动在'\\ src \\ main \\ resources \\ templates \\'中查找index.html ,这是Spring Boot中视图的默认位置。 Refer Spring Boot and Template Engines . 请参阅Spring Boot和模板引擎

Hence we do not need to explicitly define WebConfiguration class because then we're required to define view-resolvers that are configured in the XML file or in Java class using ViewRegistry. 因此,我们不需要显式定义WebConfiguration类,因为然后我们需要使用ViewRegistry定义在XML文件或Java类中配置的视图解析器。 Spring Boot eliminates the need for both this XML and Java View Registry. Spring Boot消除了对XML和Java View Registry的需要。 So you can also do away with WebConfiguration class. 因此,您也可以取消WebConfiguration类。

Read point number 2 here . 在此处阅读第2点。
Add the following dependency in your pom: 在pom中添加以下依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

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

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