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:
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>
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" />
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");
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!