首页 > 解决方案 > Spring Boot 无法检索 html 页面

问题描述

我无法通过尝试 localhost:8080/blog 来检索 blog.html 和其他 html。当我尝试访问 /blog 路径时,我得到 Whitelabel 错误页面,因此找不到它的 404,并且在控制台日志中我看到“ ResourceHttpRequestHandler : Path represents URL or has "url:" prefix: [classpath:/templates/blog.html]”警告。当我将@controller 更改为@restcontroller 时,我可以访问/blog 路径,但使用@controller 我无法访问它。先感谢您

我的项目结构:

在此处输入图像描述

Testt.java 所以我的控制器类:

@Controller public class Testt {

    @GetMapping(path = "/blog")     public String getAbout() {      return "blog";  }

}

SecurityConfig.java 所以我的 spring 安全配置类:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers(HttpMethod.GET).permitAll();
    }

}

EstoreAppApplication.java 所以我的 boostrapper springbootjava 类:

@SpringBootApplication
public class EstoreAppApplication {

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

}

我的应用程序属性:

spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html

标签: spring-bootspring-mvc

解决方案


只是一个猜测,因为你没有提到你的 pom.xml 文件,但是:

  1. 确保其中包含以下依赖项,我假设您缺少模板库,我在这里选择 Thymeleaf。

.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 删除spring.mvc.view.属性。

  2. 再试一次。


推荐阅读