原创

ssm整合thymeleaf模板引擎

温馨提示:
本文最后更新于 2020年12月10日,已超过 663 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

一、引入jar包

1、非maven项目

下载以下jar包放入WEB-INF/lib目录下
attoparser-2.0.5.RELEASE.jar
thymeleaf-spring5-3.0.9.RELEASE.jar
thymeleaf-3.0.9.RELEASE.jar
unbescape-1.1.6.RELEASE.jar
下载链接 密码:1997

2、maven项目

待补充

二、替换的jsp的模板引擎

将以下配置信息在spring-mvc.xml中替换掉原来的模板引擎

 <!--视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine" ref="templateEngine"/>
    </bean>
    <!-- templateEngine -->
    <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"/>
    </bean>
    <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <!--解决html页面中文乱码问题-->
        <property name="characterEncoding" value="UTF-8"/>
        <property name="prefix" value="/WEB-INF/templates/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML5"/>
    </bean>

原配置:

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:contex="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd"> 
    <!--静态资源过滤-->
    <mvc:default-servlet-handler/>
    <mvc:resources location="/js/"  mapping="/js/**" />
    <mvc:resources location="/css/"  mapping="/css/**" />
    <!--扫描包:controller-->
    <contex:component-scan base-package="com.it1997.controller"/>
<!--需要替换的开始-->
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/template/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--需要替换的结束-->
    <!--解决页面json乱码-->
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
      <constructor-arg value="UTF-8" />
    </bean>
    <!--开启注解驱动  -->
    <mvc:annotation-driven >
        <mvc:message-converters>
        <ref bean="stringHttpMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

修改后配置:

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:contex="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd"> 
    <!--静态资源过滤-->
    <mvc:default-servlet-handler/>
    <mvc:resources location="/WEB-INF/resources/"  mapping="/resources/**" />

    <!--扫描包:controller-->
    <contex:component-scan base-package="com.it1997.controller"/>
    <!--替换的开始-->
   <!--视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine" ref="templateEngine"/>
    </bean>
    <!-- templateEngine -->
    <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"/>
    </bean>
    <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <!--解决html页面中文乱码问题-->
        <property name="characterEncoding" value="UTF-8"/>
        <property name="prefix" value="/WEB-INF/templates/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML5"/>
    </bean>
    <!--替换的结束-->
    <!--解决页面json乱码-->
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
      <constructor-arg value="UTF-8" />
    </bean>
    <!--开启注解驱动  -->
    <mvc:annotation-driven >
        <mvc:message-converters>
        <ref bean="stringHttpMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

您的点赞就是对我最大的支持,愿您事事没烦恼,天天没Bug!

正文到此结束
本文目录