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.
java spring-mvc utf-8 character-encoding
OemerA May 8 '11 at 2:33 pm 2011-05-08 14:33
source share