Servlet上下文模板解析器错误与Thymeleaf spring MVC集成

时间:2017-03-09 15:41:46

标签: java spring spring-mvc thymeleaf

我曾尝试将thymeleaf与spring mvc一起使用,但在servlet-context的xml配置文件中,它显示了ServletContextTemplateResolver的以下错误:

  

此行找到多个注释:        - 没有在类'org.thymeleaf.templateresolver.ServletContextTemplateResolver'中定义0参数的构造函数        - 没有在类'org.thymeleaf.templateresolver.ServletContextTemplateResolver'中定义0参数的构造函数

这是我的POM.xml和我的Servlet-Context.xml

var query = car.find({});
query.limit(3);


query.exec(function (err, cars) {
    if (err) { throw err; }

    for (var i=0;i< cars.length; i++) {
        console.log('______________________________');
        console.log('ID : ' + cars[i]._id);
        console.log('Make : ' + cars[i].make);
        console.log('model : ' + cars[i].model);
        console.log('seats : ' + cars[i].seats);
        console.log('------------------------------');
    }
});

POM:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />
    <context:component-scan base-package="com.mahfoud.web"/>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- SpringResourceTemplateResolver automatically integrates with Spring's own -->
    <!-- resource resolution infrastructure, which is highly recommended.          -->
    <bean id="viewResolver"  class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
      <property name="templateEngine" ref="templateEngine" />
      <property name="order" value="1" />
      <property name="viewNames" value="*.html,*.xhtml" />
    </bean>

    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
            <property name="prefix" value="/WEB-INF/templates/" />
            <property name="suffix" value=".html" />
            <property name="templateMode" value="HTML5" />
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
      <property name="templateResolver" ref="templateResolver" />
      <property name="enableSpringELCompiler" value="true" />
    </bean>


</beans>

以下是正在发生的事情的屏幕截图: screenshot of the error

1 个答案:

答案 0 :(得分:0)

升级到thymeleaf.spring4后

    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>2.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>2.1.5.RELEASE</version>
    </dependency>

我使用以下代码来实例化模板

<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="HTML5" />
</bean>