Spring MVC集成thymeleaf时提示:defined in ServletContext resource [/WEB-INF/SrpingMVCTest-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException

错误提示:

defined in ServletContext resource [/WEB-INF/SrpingMVCTest-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.templateresolver.ServletContextTemplateResolver]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.thymeleaf.templateresolver.ServletContextTemplateResolver.<init>()

解决方法:

由于是从2.0版本升到3.0版本的原因,写法变了,应该在Bean上这样注入:

    <!-- thymeleaf -->
    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">  
      <property name="prefix" value="/WEB-INF/templates/" />  
      <property name="suffix" value=".html" />  
      <property name="templateMode" value="HTML" />  
      <property name="cacheable" value="false" />  
    </bean>  
        
    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">  
      <property name="templateResolver" ref="templateResolver" />  
    </bean>  
    
    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">  
      <property name="templateEngine" ref="templateEngine" />  
      <!--解决中文乱码-->  
      <property name="characterEncoding" value="UTF-8"/>  
    </bean>

参考:

https://stackoverflow.com/questions/38057341/failed-to-instantiate-org-thymeleaf-templateresolver-servletcontexttemplatereso

原文地址:https://www.cnblogs.com/EasonJim/p/7519817.html