【问题标题】:Spring Boot @RequestParameter Unexpected BehaviorSpring Boot @RequestParameter 意外行为
【发布时间】:2019-03-23 23:36:36
【问题描述】:

我有一个带有以下 @Request 映射的简单 Spring Boot MVC 控制器。我正在使用 Thymeleaf 进行查看。

@RequestMapping({"", "/", "index", "index.html"})
  public String index(){
      return "index";
 }

index.html 存在于 resources/templates/index.html 中,我可以使用 http://localhost:8080 查看它

但是,http://localhost:8080/indexhttp://localhost:8080/index.html 会导致出现白标错误页面。我做错了什么?

【问题讨论】:

标签: spring-mvc spring-boot


【解决方案1】:

您的控制器包的文件夹结构不正确。您应该将控制器包移动到guru.springframework.sfgpetclinic.controller,以便 Spring 可以找到并使用控制器 Bean。

但是为什么输入http://localhost:8080时会显示索引页面?即使您没有指定任何请求映射,Spring 也会自动在 index.html 中搜索http://localhost:8080

所以移动你的控制器包,一切都会正常工作。

【讨论】:

    【解决方案2】:

    您需要在每个网址之前添加 /。因此,您需要将其更改为/index,而不是index。希望对您有所帮助。

    【讨论】:

    猜你喜欢
    • 2016-01-19
    • 1970-01-01
    • 2021-10-02
    • 2018-12-15
    • 2018-06-28
    • 2018-11-23
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多