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

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

问题描述

目前我正在尝试开始使用 Spring MVC.在尝试时,我遇到了编码问题.

At the moment I'm trying to get started with Spring MVC. While trying things out I ran into an encoding issue.

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

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

}

在 JSP 页面上,我只想显示带有 UTF-8 字符的字符串,如下所示:

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>

结果我得到以下内容:

Hello world!

ö

ölm

请注意,显示以下代码 没有编码错误.我还在 Springsource Tool Suite 中将默认编码设置为 UTF-8,但我仍然收到错误的字符.

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.

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

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.

编辑 2:

感谢 McDwell,我刚刚尝试在控制器中使用 "u00f6lm" 而不是 "ölm" 并且 JSP 页面上的编码问题消失了.

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.

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

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

谢谢.

推荐答案

好的,我找到了编码问题的原因.

Ok guys I found the reason for my encoding issue.

问题出在我的构建过程中.我没有在 pom.xml 文件中告诉 Maven 使用 UTF-8 编码构建项目.因此,Maven 只是从我的系统中获取默认编码,即 MacRoman,并使用 MacRoman 编码构建它.

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.

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

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

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

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天全站免登陆