Spring不提供html文件

Kas*_*par 3 html java spring spring-mvc

所以我试图按照本指南关于如何使用Spring提供html文件:http: //spring.io/guides/gs/serving-web-content/

我有完全相同的文件夹结构和完全相同的文件,但是当我运行spring boot服务器时,我localhost:8080/greeting只会显示greetingGreetingController它返回的字符串,就是它,如果我查看页面源代码,其中没有html .

我找不到任何相关的答案,因为所有类似的答案仍然使用.xml文件驱动的Spring,您在.xml文件中声明视图.但是本指南明确指出不需要使用.xml.它应该像那样工作.

制图:

@RestController
public class GreetingController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required = false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }
}
Run Code Online (Sandbox Code Playgroud)

使用@Controller抛出错误:

2015-02-25 14:50:14.830 ERROR 2378 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [greeting]: would dispatch back to the current handler URL [/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause

javax.servlet.ServletException: Circular view path [greeting]: would dispatch back to the current handler URL [/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
Run Code Online (Sandbox Code Playgroud)

编辑:

解决方案:a)使用@Controller而不是@RestController b)在IntelliJ中启动应用程序时,请确保创建在执行Main类之前运行的Gradle任务.

Pre*_*ric 6

您正在使用@RestController(文档)而不是@Controller.

一个便利注释,它本身用@Controller和@ResponseBody注释.

并且@ResponseBody无论方法返回什么,只返回调用者,greeting在您的情况下为字符串.

至于你得到的圆形视图路径异常,它可能与ViewResolvers 有关.从春季启动文档

ViewResolver有很多可供选择的实现,而Spring本身并不认为你应该使用哪些.另一方面,Spring Boot会根据它在类路径和应用程序上下文中找到的内容为您安装一个或两个

因此,依赖项中可能缺少某些内容.你有spring-boot-starter-thymeleaf依赖配置吗?