thymeleaf解决中文乱码

一曲冷凌霜 提交于 2019-12-07 20:32:36

在web.xml加了过滤器也是无法解决的

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

需要在spring的配置文件中加入thymeleaf 视图解析器的编码,如:

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

   <property name="characterEncoding" value="UTF-8"/>

</bean>

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">    

<property name="templateResolver" ref="templateResolver" />

</bean>

<bean  class="org.thymeleaf.spring4.view.ThymeleafViewResolver">  

  <property name="templateEngine" ref="templateEngine" /> 

   <property name="order" value="1" />   

 <property name="viewNames" value="*.html,*.xhtml" /> 

   <property name="characterEncoding" value="UTF-8"/>

</bean>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!