【问题标题】:Thymeleaf Controller Not RenderingThymeleaf 控制器不渲染
【发布时间】:2020-10-09 17:40:43
【问题描述】:

更新:将注释从@RestController 切换到@Controller,现在我在尝试点击http://localhost:8080/api/v1/create_short_url 时得到一个404。我在控制器中添加了一个 System.out.println 并看到它正在打印,所以我知道它正在进入控制器。我认为它只是找不到模板。

@Controller 
@RequestMapping("/api/v1") 
public class UrlShorteningController {

    @GetMapping("/create_short_url")
public String newShortUrl(Model model) {
    System.out.println("^^^^^^^");
    model.addAttribute("longUrl",
        new String());
    return "new-short-url-form";
}

请求


我有一个控制器,我希望它可以呈现一个 HTML 模板。相反,它只返回控制器的名称。我在这里做错了什么?

实际

预期: html页面的渲染

代码

src/main/java/com/example/urlshortener/api/UrlShorteningController.java 中的控制器:

@RestController
....
@GetMapping("/create_short_url")
public String newShortUrl(Model model) {
    model.addAttribute("longUrl",
        new String());
    return "new-short-url-form";
}

build.gradle:

... 
dependencies {
...
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
...
}

src/main/resources/templates/new-short-url-form.html 中的 Thymeleaf 模板

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>New Short Url</title>
</head>
<body>
<form method="POST" th:action="@{/create_short_url_thymeleaf}" th:object="${String}">
    <h1>Enter a url to shorten</h1>
    <input type="text" id="longUrl" th:field="*{String}"/>
</form>
</body>
</html>

【问题讨论】:

    标签: java spring spring-mvc thymeleaf


    【解决方案1】:

    src/resources/templates/new-short-url-form.html 中的 Thymeleaf 模板

    模板的路径应该是 src/ma​​in/resources/templates/

    【讨论】:

    • 好收获!不幸的是,将它移到那里似乎没有帮助:/同样的问题。
    【解决方案2】:

    您使用的是GetMapping 而不是RequestMapping。如果您希望提供模板,请使用 RequestMappingGetMapping 仅用于获取请求,它解释了为什么您收到的是文字字符串而不是模板。

    【讨论】:

    • 我过去也通过 get 映射成功地做到了这一点 - github.com/ericadohring/greetings/commit/…。如果你拉取那个 repo,签出那个提交,然后运行它,它会站起来并返回一个类似的模板。
    • 错了,@GetMapping注解只是@RequestMapping(method = RequestMethod.GET)的快捷方式,显示here
    • 如果你用那个提交 sha 支持那个应用程序,它就可以工作。
    • 您需要替换@RestController 并使用RequestMapping。
    • RestController 和 RequestMapping OR GetMapping 的组合返回表单名称的字符串(就像我在原始问题中所做的那样)。带有 RequestMapping 或 GetMapping 的控制器返回 404。
    【解决方案3】:

    我意识到我的编辑器没有选择视图名称。我不知道为什么,因为文件夹结构似乎正确。我尝试将其重命名为“某种形式”,但它突然起作用了。不确定 html 文件名是否有长度限制。

    【讨论】:

      【解决方案4】:

      尝试使用 ModelAndView

      @Controller 
      @RequestMapping("/api/v1") 
      public class UrlShorteningController {
      
          @GetMapping("/create_short_url")
      public ModelAndView newShortUrl() {
       ModelAndView modelAndView = new ModelAndView();
          System.out.println("^^^^^^^");
      modelAndView.addObject("longUrl",
              new String());
      modelAndView.setViewName("new-short-url-form")
      return modelAndView;
      }
      

      &lt;html lang="en" xmlns:th="http://www.thymeleaf.org"&gt;替换你的html标签

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-16
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 2013-01-27
        相关资源
        最近更新 更多