You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非Spring Boot环境下Spring+Thymeleaf编码问题及CharacterEncodingFilter配置

Fixing UTF-8 Encoding Issue with Session Attributes in Spring 5 + Thymeleaf 3 (Non-Spring Boot)

Hey there, let's work through this encoding problem where your ${session.userName?:anybody} is showing up as "????????". You've already taken some solid initial steps, but there are a few key configurations you might be missing—let's break it down clearly:

1. Explicitly Configure Thymeleaf for UTF-8

Since you're not using Spring Boot, Thymeleaf doesn't get auto-configured for UTF-8 out of the box. This is often the missing piece when session attributes render as garbage characters. Add these beans to your dispatcher-servlet.xml (or your Spring MVC configuration file):

<!-- Thymeleaf Template Resolver -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/templates/"/> <!-- Adjust to match your template path -->
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML5"/>
    <!-- Critical: Set encoding for template files -->
    <property name="characterEncoding" value="UTF-8"/>
</bean>

<!-- Thymeleaf Template Engine -->
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver"/>
</bean>

<!-- Thymeleaf View Resolver -->
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine"/>
    <!-- Ensure response output uses UTF-8 -->
    <property name="characterEncoding" value="UTF-8"/>
</bean>

2. Configure Your Server (e.g., Tomcat) for UTF-8

Even if your app is set to UTF-8, the server might be decoding requests or session data using a default charset. For Tomcat, edit conf/server.xml and update the Connector node with these settings:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
           URIEncoding="UTF-8"
           useBodyEncodingForURI="true"/>
  • URIEncoding="UTF-8" ensures GET request parameters and URI paths are decoded as UTF-8.
  • useBodyEncodingForURI="true" makes the server use the request body's encoding (set by your filter) for URI decoding too.

3. Verify Your CharacterEncodingFilter Priority

Your existing web.xml filter configuration looks correct, but double-check that it's the very first filter in the file. Filters execute in declaration order—if another filter runs before it, the encoding could get overridden:

<!-- Must be the first filter in web.xml -->
<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>

<!-- All other filters go below this -->

The forceEncoding="true" setting ensures both request and response are forced to use UTF-8, which is exactly what you need.

4. Quick Final Checks

  • Confirm that when you set session.userName in your controller/service, the string is properly encoded as UTF-8 (e.g., if it's coming from a database, ensure your DB connection uses UTF-8).
  • Your HTML page already has the correct meta tags, so that's covered:
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    

After making these changes, restart your server and test again—your session attribute should render correctly instead of those question marks.

内容的提问来源于stack exchange,提问作者Alexander Mitenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:38:54