Thymeleaf模板未由Spring处理

时间:2015-02-19 01:48:25

标签: spring spring-mvc spring-boot thymeleaf

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

我正在使用自动配置,结果如下(从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"
            }
        ],
    }
}

我绝对没有应用程序配置 - 此阶段的所有内容都取决于autoconfig。

我的控制器(目前只有一个)看起来像这样:

@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)"
},

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

我尝试将--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]

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

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

2 个答案:

答案 0 :(得分:1)

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

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

所以现在它有效。很抱歉回答我自己的问题,但经过几个小时的摆弄后,有一定数量的关闭。

答案 1 :(得分:0)

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