Spring MVC UTF-8编码 [英] Spring MVC UTF-8 Encoding

查看:250
本文介绍了Spring MVC UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我想开始使用Spring MVC。在尝试的时候,我遇到了编码问题。



我想在我的JSP页面上显示UTF-8字符,所以我添加了一个UTF-8字符串到我的ModelAndView。它看起来像这样:

  @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);
}

}

以显示具有UTF-8字符的字符串,如下所示:

 <%@ page language =javapageEncoding =UTF -8%> 
<%@ page contentType =text / html; charset = UTF-8%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/coreprefix =c%>
<%@ page session =false%>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< title>首页< / title>
< / head>
< body>
< h1> Hello world!< / h1>
< p>< c:out value =ö/>< / p>
< p>< c:out value =$ {utftest}>< / c:out>< / p&
< / body>
< / html>

结果我得到以下结果:

  Hello world! 

ö

ölm

注显示没有编码错误的以下代码< c:out value =ö/>
我也在Springsource Tool Suite中将默认编码设置为UTF-8,但我仍然得到错误的字符。



编辑:



也许我应该提到我使用的是Mac OS x 10.6。对于Spring开发,我使用Spring的Springsource工具包(http://www.springsource.com/developer/sts)。
希望这有助于找出我的设置有什么问题。



编辑2:

感谢McDwell,我只是尝试使用\\\ölm而不是ölm在我的控制器和JSP页面上的编码问题都没了。



这是否意味着我的.java文件使用错误的字符集编码?在哪里可以在Eclipse中更改?



谢谢。

解决方案

Ok们我发现了我的编码问题的原因。



错误在我的构建过程中。我没有在我的 pom.xml 文件中告诉Maven用UTF-8编码构建项目。为此,Maven刚刚从我的系统,即MacRoman采用默认编码,并使用MacRoman编码构建它。



幸运的是,Maven在构建项目时会警告您这一点(但是,由于所有其他消息,警告很有可能从屏幕上消失)。



这是您需要在 pom.xml 文件中设置的属性:

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

感谢大家的帮助。没有你们,我就不能明白这一点!


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.

解决方案

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:

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

Thank you guys for all your help. Without you guys I wouldn't be able to figure this out!

这篇关于Spring MVC UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆