SSM环境下配置Thymeleaf时出现ServletContextTemplateResolver no Matching constructor

开始时报错的配置为:

<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
       <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".html"></property>
        <property name="templateMode" value="HTML5"></property>
    </bean>
    <bean id="templateEngine"
          class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>
    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
    </bean>

在ApplicationContext-springmvc.xml中修改配置为:

<!--视图解析器-->
    <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
       <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".html"></property>
        <property name="templateMode" value="HTML5"></property>
    </bean>
    <bean id="templateEngine"
          class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>
    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
    </bean>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM(Spring + Spring MVC + MyBatis)是一种经典的Java开发框架组合,而Thymeleaf是一种流行的模板引擎。下面是SSM整合Thymeleaf的基本步骤: 1. 首先,确保你已经正确配置好了Spring、Spring MVC和MyBatis。 2. 添加Thymeleaf的依赖到你的项目中。你可以在Maven的pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- 其他依赖... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> ``` 3. 在Spring配置文件中启用Thymeleaf视图解析器。在Spring MVC的配置文件(例如`springmvc.xml`)中添加以下配置: ```xml <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 其他配置... --> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".html" /> <property name="order" value="2" /> <!-- 设置优先级,确保Thymeleaf视图解析器在其他视图解析器之前 --> </bean> <bean id="thymeleafViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.ThymeleafView" /> <property name="templateEngine" ref="templateEngine" /> <property name="order" value="1" /> <!-- 设置优先级,确保Thymeleaf视图解析器优先于其他视图解析器 --> </bean> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> </bean> <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML" /> </bean> ``` 4. 创建Thymeleaf的模板文件。在`/WEB-INF/templates/`目录下创建HTML模板文件,例如`index.html`。 5. 在你的Controller中返回Thymeleaf视图。在你的Controller方法中,使用`ModelAndView`对象返回Thymeleaf视图的名称(不包含后缀),例如: ```java @GetMapping("/index") public ModelAndView getIndexPage() { ModelAndView modelAndView = new ModelAndView("index"); // 设置模型数据 modelAndView.addObject("message", "Hello, Thymeleaf!"); return modelAndView; } ``` 6. 在Thymeleaf模板文件中使用模型数据。在Thymeleaf模板文件中,使用Thymeleaf的表达式语法来展示模型数据。例如,在`index.html`中展示消息: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>SSM with Thymeleaf</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 这样,你就完成了SSMThymeleaf的整合。当访问`/index`路径,将会渲染`index.html`模板并显示消息 "Hello, Thymeleaf!"。请根据你的项目需求进行适当的配置和调整。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值