我使用IntelliJ创建了一个带有Spring的web应用程序,它运行在Tomcat上,使用胡子和Gradle。
我的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视图是空的。
有人能帮忙吗?
发布于 2018-12-18 08:06:46
我认为您需要配置ViewResolver来按名称解析视图。这里有一个指南:https://www.baeldung.com/spring-mvc-view-resolver-tutorial
发布于 2018-12-18 08:34:56
对我来说,我最终发现应该将文件扩展名重命名为.mustache,而不是使用.html。这将适用于开箱即用的配置。下面是我在网上找到的一个示例代码:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-mustache
我还没有找到确切的方法来覆盖模板路径和文件扩展名,而让应用程序仍然认识到它是一个胡子模板。因此,它将在返回之前进行编译。
https://stackoverflow.com/questions/53828132
复制相似问题