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

    spring Thymeleaf 中文乱码

    时间:2016-03-19 19:17:20      阅读:21      评论:0      收藏:0      [点我收藏+]

    标签:

    前言:

    项目用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>
  • 相关阅读:
    linux计算命令的执行时间
    vim中保存其中部分内容到其它文件的方法
    vim打开文本文件末尾显示^M的解决办法
    sshd服务安全优化-修改ssh端口
    内网环境搭建yum仓库
    LVS-dr负载均衡原理及示例搭建
    go 搭建并行处理管道
    mac QuiteTime快捷键
    第十六章 分布式爬虫--准备工作
    mac Idea快捷键
  • 原文地址:https://www.cnblogs.com/hua0129/p/5411629.html
Copyright © 2011-2022 走看看