【问题标题】:Thymeleaf template not processed by SpringSpring 未处理 Thymeleaf 模板
【发布时间】:2015-02-19 01:48:25
【问题描述】:

我正在尝试将 Thymeleaf 与 Spring 一起使用,使用 spring-boot。当我访问映射到控制器的 URL 时,我希望会导致使用 Thymeleaf 模板,我只是在浏览器中得到一个空白页面。

我正在使用自动配置,结果如下(摘自@987654321@):

{
    "positiveMatches":{
        "ThymeleafAutoConfiguration":[
            {
                "condition":"OnClassCondition",
                "message":"@ConditionalOnClass classes found: org.thymeleaf.spring4.SpringTemplateEngine"
            }
        ],
        "ThymeleafAutoConfiguration.DefaultTemplateResolverConfiguration":[
            {
                "condition":"OnBeanCondition",
                "message":"@ConditionalOnMissingBean (names: defaultTemplateResolver; SearchStrategy: all) found no beans"
            }
        ],
        "ThymeleafAutoConfiguration.ThymeleafDefaultConfiguration":[
            {
                "condition":"OnBeanCondition",
                "message":"@ConditionalOnMissingBean (types: org.thymeleaf.spring4.SpringTemplateEngine; SearchStrategy: all) found no beans"
            }
        ],
        "ThymeleafAutoConfiguration.ThymeleafViewResolverConfiguration":[
            {
                "condition":"OnClassCondition",
                "message":"@ConditionalOnClass classes found: javax.servlet.Servlet"
            },
            {
                "condition":"OnWebApplicationCondition",
                "message":"found web application StandardServletEnvironment"
            }
        ],
        "ThymeleafAutoConfiguration.ThymeleafViewResolverConfiguration#thymeleafViewResolver":[
            {
                "condition":"OnBeanCondition",
                "message":"@ConditionalOnMissingBean (names: thymeleafViewResolver; SearchStrategy: all) found no beans"
            },
            {
                "condition":"OnPropertyCondition",
                "message":"matched"
            }
        ],
        "ThymeleafAutoConfiguration.ThymeleafWebLayoutConfiguration":[
            {
                "condition":"OnClassCondition",
                "message":"@ConditionalOnClass classes found: nz.net.ultraq.thymeleaf.LayoutDialect"
            }
        ],
    },
    "negativeMatches":{
        "ThymeleafAutoConfiguration.DataAttributeDialectConfiguration":[
            {
                "condition":"OnClassCondition",
                "message":"required @ConditionalOnClass classes not found: com.github.mxab.thymeleaf.extras.dataattribute.dialect.DataAttributeDialect"
            }
        ],
        "ThymeleafAutoConfiguration.ThymeleafConditionalCommentsDialectConfiguration":[
            {
                "condition":"OnClassCondition",
                "message":"required @ConditionalOnClass classes not found: org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect"
            }
        ],
        "ThymeleafAutoConfiguration.ThymeleafSecurityDialectConfiguration":[
            {
                "condition":"OnClassCondition",
                "message":"required @ConditionalOnClass classes not found: org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"
            }
        ],
    }
}

我完全没有应用程序配置——这个阶段的一切都依赖于自动配置。

我的控制器(目前只有一个)如下所示:

@RestController
@RequestMapping("/")
public class MainController {

    @RequestMapping(value = "main.html", method = RequestMethod.GET)
    public void index( Model model ) {
        model.addAttribute( "name", "Gorgonzola" );
    }
}

我有以下项目布局:

src/
    main/
        java/
            attendance/
                MainController.java
        resources/
            templates/
                main.html

templates/main.html 包含:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:th="http://www.thymeleaf.org">

    <head>
        <title>Attendance</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>

    <body>

        <p th:text="'Hello, ' + ${name} + '!'" />

    </body>

</html>

@987654322@ 包括:

"{[/main.html],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}": {
    "bean": "requestMappingHandlerMapping",
    "method": "public void attendance.MainController.index(org.springframework.ui.Model)"
},

我正在使用 Gradle spring-boot 插件并使用 Gradle bootRun 任务运行应用程序。

我尝试将--debug 添加到bootRun 参数中,当我将浏览器指向@987654323@ 时,我得到了:

2015-02-19 17:36:14.754 DEBUG 9168 --- [tp1565713391-18] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /main.html
2015-02-19 17:36:14.756 DEBUG 9168 --- [tp1565713391-18] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/main.html]

...所以我怀疑我缺少一个简单的参数。但它是什么?

谁能看到为什么我的模板没有被处理?或者建议我可以采取的进一步诊断步骤?

【问题讨论】:

    标签: spring spring-mvc spring-boot thymeleaf


    【解决方案1】:

    我设法完成了这项工作。它有一些问题:

    • @RestController 注释应该是我不小心添加的 当然读@Controller
    • main.html 中的 DOCTYPE 规范似乎与自动配置的 Thymeleaf 方言不兼容 - 它只需要读取 &lt;!DOCTYPE html&gt;
    • 我接受了 hrrgttnchml 关于返回要使用的模板的裸名的建议

    所以现在它可以工作了。很抱歉回答了我自己的问题,但是经过几个小时的摆弄之后,有一定程度的关闭。

    【讨论】:

      【解决方案2】:

      您的控制器没有定义要渲染的视图。默认情况下,ViewResolver 不会根据您的 RequestMapping 执行此操作。只需让您的 main 方法返回一个 String 并添加一个 return 语句,如 return "main";一切都应该正常。

      【讨论】:

      • 我试过了(事实上,那是我最初的实现)。我只是得到一个包含字符串“main”(并且没有其他内容)的网页。在日志中,我有: 2015-02-19 19:42:01.563 DEBUG 8832 --- [tp1565713391-16] o.s.b.a.e.mvc.EndpointHandlerMapping :查找路径 /main.html 的处理程序方法 2015-02-19 19:42 :01.566 DEBUG 8832 --- [tp1565713391-16] o.s.b.a.e.mvc.EndpointHandlerMapping:没有找到 [/main.html] 的处理程序方法
      猜你喜欢
      • 2019-10-11
      • 2017-03-08
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 2016-01-26
      • 2017-09-25
      • 1970-01-01
      • 2019-10-31
      相关资源
      最近更新 更多