如何使用Thymeleaf在Spring中配置CharacterEncodingFilter?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Thymeleaf在Spring中配置CharacterEncodingFilter?相关的知识,希望对你有一定的参考价值。

不是春靴!

请告诉我,如何用“????????”解决问题会话属性的EL“$ {session.userName?:anybody}”的字符(编码问题)?

Spern 5.0.2,Thymel

页:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<head th:fragment="header">
<title>NFCS Management</title>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel='stylesheet'
href='webjars/bootstrap/3.2.0/css/bootstrap.min.css'>
</head>
<body>
<div class="container">
    <div th:include="layout :: authFragment" class="row"></div>
    <div class="row">
        <h1>Welcome to Spring MVC</h1>
        <h2 data-th-text="'Hello, ' + ${session.userName?:anybody} + '!'" />
    </div>
</div>
<script type="text/javascript" src="webjars/jquery/3.2.1/jquery.min.js">
</script>
<script type="text/javascript"
    src="webjars/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>

检查对象内容的编码 - 它是utf-8。首先是过滤顺序,如下面的web.xml文件所示。

veb.hml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>bootstrap</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
答案

我找到了问题的根源。

我在TemplateResolver和ViewResolver中添加了一个显式属性name =“characterEncoding”value =“UTF-8”

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

    <constructor-arg ref="servletContext" />
</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" />
    <property name="order" value="1" />
    <property name="characterEncoding" value="UTF-8"/>
    <property name="viewNames" value="*" />
</bean>

以上是关于如何使用Thymeleaf在Spring中配置CharacterEncodingFilter?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring boot 2 + Webflux + Thymeleaf 中配置 i18n?

如何将布局方言添加到spring-boot thymeleaf自动配置文件

Thymeleaf + Spring (not Boot) - 如何显示来自 messageSource 的消息

如何使用 spring-boot 和 thymeleaf 设置标准 i18n 语言环境?

如何在 Spring Boot 中使用 Thymeleaf 保存角色?

如何使用 Thymeleaf 在 Spring Boot 应用程序中正确缓存数据