百里香模板未经Spring处理



我正试图使用弹簧引导将Thymelaf与Spring一起使用。当我访问映射到控制器的URL时,我希望它能导致使用Thymelaf模板,但我在浏览器中只会得到一个空白页面。

我使用的是自动配置,结果如下(从http://localhost:8080/autoconfig截取):

{
    "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>

http://localhost:8080/mappings包括以下内容:

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

我正在使用Gradlespring-boot插件,并使用GradlebootRun任务运行应用程序。

我尝试将--debug添加到bootRun参数中,当我将浏览器指向http://localhost:8080/main.html时,我得到的是:

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]

所以我怀疑我遗漏了一个简单的参数。但它是什么?

有人知道为什么我的模板没有被处理吗?或者建议我可以采取进一步的诊断步骤?

我设法做到了。它有几个问题:

  • 我意外添加的@RestController注释应该当然读@Controller
  • main.html中的DOCTYPE规范似乎与自动配置的Thymelaf方言不兼容——它只需要读取<!DOCTYPE html>
  • 我接受了hrrgttnchml关于返回要使用的模板的裸名称的建议

所以现在它起作用了。很抱歉回答了我自己的问题,但经过几个小时的处理,终于有了一定的结束。

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

最新更新