At the moment I'm trying to get started with Spring MVC. While trying things out I ran into an encoding issue.
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 the String with UTF-8 characters like this:
<%@ 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 result I get following:
Hello world!
ö
ölm
Note that following code <c:out value="ö" />
was displayed without encoding error.
I also set the default encoding to UTF-8 in Springsource Tool Suite but I'm still getting wrong characters.
Edit:
Maybe I should have mentioned that I'm using a Mac with OS X 10.6. For Spring development I use the Springsource Tool Suite from Spring (http://www.springsource.com/developer/sts). Hope this helps to find out what is wrong with my setting.
Edit 2:
Thanks to McDwell, I just tried out using "\u00f6lm"
instead of "ölm"
in my controller and the encoding issue on the JSP page is gone.
Does that mean my .java files are encoded with wrong character set? Where can I change this in Eclipse?
Thank you.
right-click to your controller.java then properties and check if your text file is encoded with utf-8, if not this is your mistake.
Easiest solution to force UTF-8 encoding in Spring MVC returning String:
In
@RequestMapping
, use:Make sure you register Spring's
CharacterEncodingFilter
in yourweb.xml
(must be the first filter in that file).If you are on Tomcat you might not have set the
URIEncoding
in yourserver.xml
. If you don't set it to UTF-8 it won't work. Definitely keep theCharacterEncodingFilter
. Nevertheless, here's a concise checklist to follow. It will definitely guide you to make this work.To solve this issue you need below three steps:
Set page encoding to UTF-8 like below:
Set filter in web.xml file as below:
Set resource encoding to UTF-8, in case if you are writing any UTF-8 characters in Java code or JSP directly.
Depending on how you render your view, you may also need:
Ok guys I found the reason for my encoding issue.
The fault was in my build process. I didn't tell Maven in my
pom.xml
file to build the project with the UTF-8 encoding. Therefor Maven just took the default encoding from my system which is MacRoman and build it with the MacRoman encoding.Luckily Maven is warning you about this when building your project (BUT there is a good chance that the warning disappears to fast from your screen because of all the other messages).
Here is the property you need to set in the
pom.xml
file:Thank you guys for all your help. Without you guys I wouldn't be able to figure this out!