Home > Java > javaTutorial > body text

How to Resolve UTF-8 Encoding Problems in Spring MVC?

DDD
Release: 2024-11-11 09:28:02
Original
317 people have browsed it

How to Resolve UTF-8 Encoding Problems in Spring MVC?

Spring MVC UTF-8 Encoding

Problem:

When displaying UTF-8 characters on JSP pages, the resulting characters are encoded incorrectly, despite setting the encoding in both the page and the ModelAndView.

Solution:

To resolve this issue, follow these steps:

  1. Register Spring's CharacterEncodingFilter:

    <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> 
    Copy after login
  2. Set URIEncoding in Tomcat's server.xml:
    If you are using Tomcat, ensure URIEncoding is set to UTF-8 in the server.xml:

    <Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8" />
    Copy after login
  3. Encode special characters in the controller:
    Use uXXXX encoding for special characters in the controller to avoid encoding issues:

    return new ModelAndView("home", "utftest", "\u00f6lm");
    Copy after login

By following these steps, you can ensure correct UTF-8 encoding for characters displayed on your JSP pages within your Spring MVC application.

The above is the detailed content of How to Resolve UTF-8 Encoding Problems in Spring MVC?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template