Spring MVC Encoding UTF-8

I'm currently trying to get started with Spring MVC. Having tried things, I ran into a coding problem.

I want to display UTF-8 characters on my JSP pages, so I added a String with UTF-8 characters to my ModelAndView. It looks like this:

@Controller public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); @RequestMapping(value="/", method=RequestMethod.GET) public ModelAndView home() { logger.info("Welcome home!"); return new ModelAndView("home", "utftest", "ölm"); } } 

On the JSP page, I just want to display a string with UTF-8 characters as follows:

 <%@ page language="java" pageEncoding="UTF-8"%> <%@ page contentType="text/html;charset=UTF-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Home</title> </head> <body> <h1>Hello world!</h1> <p><c:out value="ö" /></p> <p><c:out value="${utftest}"></c:out></p> </body> </html> 

As a result, I get the following:

 Hello world! ö √∂lm 

Note that the following code <c:out value="ö" /> was displayed without a coding error. I also set the default encoding to UTF-8 in the Springsource Tool Suite, but I still get the wrong characters.

Edit:

Perhaps I should have mentioned that I am using a Mac with OS X 10.6. For Spring development, I use the Springsource Tool from Spring (http://www.springsource.com/developer/sts). Hope this helps find out what is wrong with my setup.

Edit 2:

Thanks to McDwell, I just tried using "\u00f6lm" instead of "ölm" in my controller, and the encoding problem on the JSP page disappeared.

Does this mean that my .java files are encoded with the wrong character set? Where can I change this in Eclipse?

Thank.

+51
java spring-mvc utf-8 character-encoding
May 8 '11 at 2:33
source share
8 answers

Ok guys, I found the cause of my encoding problem.

The error was in my build process. I did not tell Maven in my pom.xml file to build a project with UTF-8 encoding. So Maven just took the default encoding from my system, which is MacRoman, and build it using MacRoman encoding.

Fortunately, Maven warns you about this when creating a project (BUT there is a good chance that the warning will disappear from your screen due to all other messages).

Here is the property to set in the pom.xml file:

 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> ... </properties> 

Thanks guys for your help. Without you guys, I could not understand it!

+37
May 09 '11 at 7:23
source share

Make sure you register Spring CharacterEncodingFilter in web.xml (should be the first filter in this file).

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

If you are on Tomcat, you may not have installed URIEncoding in your server.xml . If you do not install it in UTF-8, this will not work. Definitely keep a CharacterEncodingFilter . However, a concise checklist follows. It will definitely help you do this job.

+113
May 08 '11 at 14:57
source share

In addition to Benjamin's answer (which I just shot) you need to make sure that your files are actually stored using the correct encoding (this will be UTF-8 for source code, JSP, etc., but note that the Property Files Java must be encoded as ISO 8859-1 by definition).

The problem is that it is not possible to determine which encoding was used to store the file. The only option is to open the file using a specific encoding and check if the content makes sense. You can also try to convert the file from the intended encoding to the desired encoding with iconv - if this causes an error, your assumption was incorrect. Therefore, if you assume that hello.jsp is encoded as UTF-8, run "iconv -f UTF-16 -t UTF-8 hello.jsp" and check for errors.

If you need to find out that your files are incorrectly encoded, you need to find out why. This is probably the editor or IDE that you used to create the file. In the case of Eclipse (and STS), make sure that the encoding of the text file (Preferences / General / Workspace) is set to UTF-8 (unfortunately, the default encoding for your platform is used).

What makes it difficult to debug encoding problems is that there are so many components (text editor, borwser, plus each software component between them, in some cases, including the database), and each of them may present an error.

+13
May 08 '11 at
source share

The simplest solution to force UTF-8 encoding in Spring MVC is to return a string:

In @RequestMapping use:

 produces = MediaType.APPLICATION_JSON_VALUE + "; charset=utf-8" 
+4
01 Feb '18 at 18:38
source share

right click on your .java controller, then properties and check if your text file is encoded using utf-8, if that is not your mistake.

+3
May 08 '11 at 20:25
source share

Depending on how you visualize your presentation, you may also need to:

 @Bean public StringHttpMessageConverter stringHttpMessageConverter() { return new StringHttpMessageConverter(Charset.forName("UTF-8")); } 
+3
Apr 6 '14 at 13:19
source share

In addition to Benjamin's answer - in case you use Spring Security , placing CharacterEncodingFilter in web.xml may not always work. In this case, you need to create your own filter and add it to the filter chain as the first filter. To make sure this is the first filter in the chain, you need to add it before the ChannelProcessingFilter in your WebSecurityConfigurerAdapter:

 @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //add your custom encoding filter as the first filter in the chain http.addFilterBefore(new EncodingFilter(), ChannelProcessingFilter.class); http.authorizeRequests() .and() // your code here ... } } 

The order of all filters in Spring Security is available here: HttpSecurityBuilder - addFilter ()

Your UTF-8 encoding filter for your client might look like this:

 public class EncodingFilter extends GenericFilterBean { @Override public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } } 

Remember to add JSPs to your files:

 <%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> 

And remove CharacterEncodingFilter from web.xml, if there is one.

+3
Jul 10 '18 at 11:58
source share

To solve this problem you need three steps below:

  1. Set the page encoding to UTF-8 as shown below:

     <%@ page language="java" pageEncoding="UTF-8"%> <%@ page contentType="text/html;charset=UTF-8" %> 
  2. Set the filter in the web.xml file as shown below:

     <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 
  3. Set the resource encoding to UTF-8 if you write any UTF-8 characters directly in Java or JSP code.

+2
Feb 22 '17 at 5:58
source share



All Articles