簡體   English   中英

鑒於調試器命中了我在控制器中設置的斷點,因此Spring Boot返回404。 我該如何解決?

[英]Spring boot return 404 given that debugger hits the breakpoint I set in the controller. How can I fix it?

我使用IntelliJ通過Spring boot創建了一個Web應用程序,該應用程序使用Mustache和Gradle在Tomcat上運行。

我的TestController.java和TestApplication.java在同一包中(com.example.test)。

這是TestApplicaiton.java

@SpringBootApplication
public class TestApplication {

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

}

這是TestController:

@Controller
public class TestController {
    @GetMapping(value = "/")
    public ModelAndView index(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
        return new ModelAndView("index");
    }
}

我將index.html放在/resources/static/resources/templates 仍然,頁面上說

找不到404(白色標簽錯誤頁面)。

如果我將@controller批注更改為@RestController並將返回類型更改為String,它將正確返回該字符串。

因此,解決視圖時似乎出了點問題。 但是,它不會引發異常。 當我進入代碼時,我注意到mvContainer視圖為null。

有人可以幫忙嗎?

我認為您需要配置ViewResolver以按名稱解析視圖。 這是一個指南: https : //www.baeldung.com/spring-mvc-view-resolver-tutorial

對我來說,我終於發現應該將文件擴展名重命名為.mustache而不是使用.html。 這將適用於開箱即用的配置。 這是我在網上找到的示例代碼: https : //github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-mustache

我還沒有找到使模板路徑和文件擴展名覆蓋的確切方法,而讓應用程序仍然識別出它是胡子模板。 因此,它將在返回之前進行編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM