gpt4 book ai didi

java - Spring 不提供 html 文件

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:10 25 4
gpt4 key购买 nike

所以我正在尝试按照本指南了解如何使用 Spring 提供 html 文件: http://spring.io/guides/gs/serving-web-content/

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

我找不到关于此的任何相关答案,因为所有类似的答案仍然使用 .xml 文件驱动的 Spring,您在 .xml 文件中声明 View 。但是本指南明确表示不需要使用 .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";
}
}

使用@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.)

编辑:

解决方法:a) 使用 @Controller 而不是 @RestControllerb) 在 IntelliJ 中启动应用程序时,确保创建一个在执行主类之前运行的 Gradle 任务。

最佳答案

您正在使用 @RestController(documentation)而不是 @Controller

A convenience annotation that is itself annotated with @Controller and @ResponseBody.

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

至于您遇到的 Circular view path 异常,它可能与 ViewResolver 相关。来自 spring boot 文档

There are many implementations of ViewResolver to choose from, and Spring on its own is not opinionated about which ones you should use. Spring Boot, on the other hand, installs one or two for you depending on what it finds on the classpath and in the application context

因此,依赖项中可能缺少某些内容。您是否配置了 spring-boot-starter-thymeleaf 依赖项?

关于java - Spring 不提供 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28719152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com