Failed to instantiate [org.thymeleaf.templateresolver.ServletContextTemplateResolver]: No default constructor found

后端 未结 2 1062
情书的邮戳
情书的邮戳 2021-01-03 07:39

I want to integrate Spring 4 with Thymeleaf, but I got:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'templateReso         


        
相关标签:
2条回答
  • 2021-01-03 08:06

    Short
    Replace ThymeleafViewResolver with SpringResourceTemplateResolver:

    <bean
        id="templateResolver"
        class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"
        p:prefix="/WEB-INF/templates/"
        p:suffix=".html"
        p:templateMode="HTML5"
    >
    </bean>
    

    Long
    Few days ago I had the same problem when used Tutorial: Thymeleaf + Spring, it is a bit outdated. Solution was found at "Thymeleaf 3 ten-minute migration guide":

    The first difference with the Thymeleaf 2 configuration is that now the recommended template resolver for Spring applications is SpringResourceTemplateResolver

    0 讨论(0)
  • 2021-01-03 08:13

    You will need to hook in your templateEngine to the viewResolver. Here's my working one:

    <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine" ref="templateEngine"/> <!-- THIS -->
    </bean>
    
    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolvers">
            <set>
                <ref bean="webTemplateResolver"/>
            </set>
        </property>
        <property name="additionalDialects">
            <set>
                <bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
            </set>
        </property>
    </bean>
    
    0 讨论(0)
提交回复
热议问题