首页 > 解决方案 > Spring Boot 未加载正确的模板,无法调试

问题描述

我是 Spring Boot 新手,一直在关注本指南: https ://spring.io/guides/gs/serving-web-content/

控制器看起来像这样:

`package hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }

}`

因为它返回一个字符串“greeting”,它会寻找greeting.html。不知道怎么做,但我相信因为 Maven 的依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

从这一点来看,一切都很好。我可以看到位于 src/main/resources/templates/greeting.html 中的 greeting.html 模板

现在我开始创建新模板并尝试以相同的方式访问它们,似乎一切都失败了。

我接下来所做的是将greeting() 的返回字符串更改为“greeting2”,以确保它确实是映射视图greeting.html 的那个。但奇怪的是在那之后我仍然可以访问greeting.html。

我认为这是缓存问题,所以我清除了浏览器缓存并仍然遇到这种情况。

我什至更改了 Controller 方法返回视图的方式并使用了 ModelAndView 对象,但仍然没有运气。

我迷路了,请帮助。谢谢

另外....我无法调试。尝试在 Application.java 中作为 Java 应用程序调试

@SpringBootApplication
public class Application {

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

}

并在我的控制器处放置断点,它不会进入那些行。

标签: javaspringspring-bootspring-mvcthymeleaf

解决方案


推荐阅读