zoukankan      html  css  js  c++  java
  • spring Thymeleaf 中文乱码

    前言:

    项目用springmvc+Thymeleaf ,但是发现html中的所有中文都乱码。

    但是自己的html的编码确定已经是UTF-8,在web.xml文件里面也添加了CharacterEncodingFilter,还是没有解决。

    在网上搜了好久,网上的说法是为ThymeleafViewResolver添加characterEncoding的属性,但是自己尝试之后还是没有解决。

    解决办法:

    同时为ServletContextTemplateResolver和ThymeleafViewResolver添加characterEncoding=UTF-8的属性。代码如下。(重要部分红色标出了)、

    复制代码
    <!-- 前端的渲染魔板引擎thymeleaf-->
        <bean id="templateResolver"
              class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
            <property name="prefix" value="/pages/"/>
            <property name="suffix" value=".html"/>
            <!-- 设置thymeleaf可以解析html的mode-->
            <property name="templateMode" value="LEGACYHTML5"/>
            <property name="cacheable" value="false"/>

        <!--这里要设置编码 --> <property name="characterEncoding" value="UTF-8"/> </bean> <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"/> </bean> <!-- 配置引擎的viewResoler--> <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine"/>
          <!--这里要设置编码 -->
         <property name="characterEncoding" value="UTF-8"/>
        </bean>
    复制代码
  • 相关阅读:
    Ztree下拉框多选
    FullCalendar日程插件
    viscose 前端常用插件
    一些词
    关于require()和export引入依赖的区别
    关于CMD/AMD和Common.js/Sea.js/Require.js
    vue中的双向数据绑定原理简单理解
    Vue-cli简单使用
    webpack简单配置
    vuex基础
  • 原文地址:https://www.cnblogs.com/hyl8218/p/5811973.html
Copyright © 2011-2022 走看看