【发布时间】:2017-09-01 23:38:30
【问题描述】:
我使用的是 spring boot 1.5.2,我的 spring rest 控制器看起来像这样
@RestController
@RequestMapping("/")
public class HomeController {
@RequestMapping(method=RequestMethod.GET)
public String index() {
return "index";
}
}
当我转到http://localhost:8090/assessment/ 时,它会到达我的控制器,但不会返回我的 index.html,它位于 src/main/resources 或 src/main/resources/static 下的 maven 项目中。如果我访问这个 url http://localhost:8090/assessment/index.html,它会返回我的 index.html。我查看了本教程https://spring.io/guides/gs/serving-web-content/,他们使用百里香叶。我必须使用 thymeleaf 或类似的东西让我的弹簧休息控制器返回我的视图吗?
我的应用程序类看起来像这样
@SpringBootApplication
@ComponentScan(basePackages={"com.pkg.*"})
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
当我将 thymeleaf 依赖项添加到我的类路径时,我收到此错误(500 响应代码)
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers
我想我确实需要百里香叶?我现在将尝试正确配置它。
更改我的控制器方法以返回 index.html 后它可以工作
@RequestMapping(method=RequestMethod.GET)
public String index() {
return "index.html";
}
我认为 thymeleaf 或类似的软件可以让您省略文件扩展名,但不确定。
【问题讨论】:
-
对于初学者,将其设为
Controller而不是RestController -
现在它仍然到达控制器,但我得到一个 404,无论 index.html 是在 src/main/resources 还是 src/main/resources/static 中。 localhost:8090/assessment/index.html 仍然有效
标签: spring-mvc spring-boot thymeleaf