我正在使用弹簧启动1.5.2,我的弹簧控制器看起来像这样
@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项目中。如果我去这个网址http://localhost:8090/assessment/index.html,它会返回我的index.html。我看了这个教程https://spring.io/guides/gs/serving-web-content/,他们使用百里香。我是否必须使用百日咳或类似的东西给我的春季休息控制器回复我的观点?
我的应用程序类看起来像这样
@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";
}
我认为百里香或类似的软件可以让你放弃文件扩展名,但不确定。
你的例子是这样的:
您的控制器方法与您的路线“评估”
@Controller
public class HomeController {
@RequestMapping(value = "/assessment", method = RequestMethod.GET)
public String index() {
return "index";
}
}
您在“src / main / resources / templates / index.html”中的Thymeleaf模板
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Hello World!</p>
</body>
</html>
RestController注释从方法而不是HTML或JSP返回json。它是@ Controller和@ResponseBody的结合体。 @RestController的主要目的是创建RESTful Web服务。对于返回html或jsp,只需使用@Controller注释控制器类。
有关详细信息,请在thymeleaf documentation中找到此链接
和错误来becoz你可能没有配置视图解析器根据“springcofigrationaddapter”make MvcConfig类(这需要添加视图解析器)和用户@controller 如果你觉得json类型(As AnjularJs项目)使用@RestController)
我通过从配置类中删除@EnableWebMvc
注释来解决这个问题。
Spring MVC Auto-configuration提供静态
index.html
支持。如果你想完全控制Spring MVC,你可以添加自己的用
@Configuration
注释的@EnableWebMvc
。
从Spring MVC Auto-configuration获取更多细节。