【问题标题】:Cannot display the index.html page in Spring boot在 Spring Boot 中无法显示 index.html 页面
【发布时间】:2018-02-14 02:17:21
【问题描述】:

我尝试创建一个简单的 Spring Boot 应用程序

我创建了spring boot应用类、配置类、控制器类和index.html。

我添加了 Thymeleaf 依赖并将 html 页面放在资源文件夹下 (\src\main\resources\templates\index.html)

但是当我运行应用程序时,它给出了一个错误

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

请给我一个解决方案。

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

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

控制器类

@RestController
public class IndexController {

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

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 页面

<!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>

【问题讨论】:

    标签: java spring-boot thymeleaf


    【解决方案1】:

    尝试执行以下操作:

    1. 将@RestController 替换为@Controller;
    2. 我认为你不需要WebConfiguration;控制器返回“index”,意思是“渲染 index.html 模板”。 Thymeleaf 将在 resources/templates 文件夹中找到模板。

    【讨论】:

      【解决方案2】:

      尝试用@Controller 替换@RestController。我将从 start.spring.io 生成的模板开始。并且一步一步地增加功能。

      或者

      如果您只是进行一些谷歌搜索,您将很容易找到一些实际有效的示例 thymeleaf 项目,从那里开始。

      【讨论】:

        【解决方案3】:

        默认情况下似乎无法访问静态资源。尝试提供对添加资源处理程序的静态资源的访问,例如:

        @Configuration
        public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
        
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/**").addResourceLocations("");
            }
        }
        

        【讨论】:

          【解决方案4】:

          如之前的答案所述,@RestController 应替换为 @Controller

          当我们使用 @RestController 时,返回语句值将作为 String Response 发送。但是当我们使用 @Controller 时,return 语句的值将被视为要渲染的视图。

          参考Diff. b/w @RestController and @Controller

          此外,当我们使用模板引擎时,Spring Boot 会自动在 '\src\main\resources\templates\' 中查找 index.html,这是 Spring Boot 中视图的默认位置百里香叶。请参阅Spring Boot and Template Engines

          因此我们不需要显式定义 WebConfiguration 类,因为我们需要定义在 XML 文件或 Java 类中使用 ViewRegistry 配置的视图解析器。 Spring Boot 消除了对这种 XML 和 Java View Registry 的需求。所以你也可以取消 WebConfiguration 类。

          【讨论】:

            【解决方案5】:

            读取点号 2 here.
            在您的 pom 中添加以下依赖项:

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

            【讨论】:

              猜你喜欢
              • 2019-05-28
              • 2018-05-08
              • 1970-01-01
              • 2019-02-05
              • 2017-09-09
              • 1970-01-01
              • 1970-01-01
              • 2018-02-26
              • 1970-01-01
              相关资源
              最近更新 更多