【问题标题】:Spring boot cannot retrieve html pageSpring Boot 无法检索 html 页面
【发布时间】:2020-04-08 17:26:50
【问题描述】:

我无法通过尝试 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.mvc.view 属性?您可以尝试删除它们吗?
  • 我删除了然后我遇到了“圆形视图路径 [博客]:将再次发送回当前处理程序 URL [/博客]。检查您的 ViewResolver 设置!”错误。奇怪的是,我将所有 html 文件复制到 src 主 Web 应用程序文件夹,然后现在我可以访问所有这些文件,但是您可以想象我无法通过控制器导航。所以我可以打开 index.html 然后可以导航到 blog.html 但是当我尝试 /blog 时它再次返回 404。
  • 见下面我的回答。我假设你还没有在你的项目中添加模板库。

标签: spring-boot spring-mvc


【解决方案1】:

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

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

.

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

  2. 再试一次。

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 2020-01-09
    • 2016-06-14
    • 1970-01-01
    • 2023-04-10
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多