【问题标题】:How to configure CharacterEncodingFilter in Spring with Thymeleaf?如何在 Spring 中使用 Thymeleaf 配置 CharacterEncodingFilter?
【发布时间】:2018-01-31 15:11:12
【问题描述】:

不是 Spring Boot!

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

Spring 5.0.2,Thymeleaf 3.0.9

页面:

<!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 文件所示。

web.xml:

<?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>

【问题讨论】:

    标签: java spring spring-mvc utf-8 thymeleaf


    【解决方案1】:

    我找到了问题的根源。

    我在 TemplateResolver 和 ViewResolver 中添加了显式 property 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>
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 2023-03-26
      • 2018-05-11
      • 2013-07-02
      • 2011-07-23
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      相关资源
      最近更新 更多