【发布时间】:2011-08-04 16:39:46
【问题描述】:
我有一个 Spring MVC bean,我想通过设置编码 UTF-8 返回土耳其字符。但是虽然我的字符串是“şŞğĞİıçÇöÖüÜ”,但它返回为“??????çÇöÖüÜ”。而且当我查看响应页面(即 Internet Explorer 页面)时,编码是西欧 iso,而不是 UTF-8。
代码如下:
@RequestMapping(method=RequestMethod.GET,value="/GetMyList")
public @ResponseBody String getMyList(HttpServletRequest request, HttpServletResponse response) throws CryptoException{
String contentType= "text/html;charset=UTF-8";
response.setContentType(contentType);
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setCharacterEncoding("utf-8");
String str="şŞğĞİıçÇöÖüÜ";
return str;
}
【问题讨论】:
-
如果它已经对你有用,你能接受吗?
-
response.setContentType(contentType);你不需要使用框架编写这样的代码,尤其是 Spring。产生 = "text/plain;charset=UTF-8" 是你的选择
标签: java spring-mvc utf-8 character-encoding