(Spring + JSP + jQuery-AJAX + JSON)设置UTF-8问题的环境?

我正在使用Spring 3.xjava进行chat project ,需要Multi-language support

这就是我所做的。

我的JSP有:

     

我的web.xml有:

  characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter  encoding UTF-8   forceEncoding true    characterEncodingFilter /*  

在我的Tomcat server.xml有:

   

我的Java environment有:

  JAVA_TOOL_OPTIONS -Dfile.encoding=UTF8 

在我的Spring-controller有:

 @RequestMapping(value="sendMessage.html",method=RequestMethod.POST) public @ResponseBody String sendMessage(HttpSession session,@RequestParam String intxnId,@RequestParam String message, HttpServletRequest request,HttpServletResponse response){ String contentType= "text/html;charset=UTF-8"; response.setContentType(contentType); //response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json; charset=UTF-8"); try { request.setCharacterEncoding("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("text/plain;charset=UTF-8"); System.out.println("Send Message UTF-8 ----------------- "+ message); String json = null; BasicChatProtocol protocol = CustomerManagement.protocol.put(intxnId, chatProtocol.getProtocol()); HashMap result = send.send(message, intxnId, protocol); result.put("name",(String) session.getAttribute("nickName")); ObjectMapper map = new ObjectMapper(); if(result.size()!= 0){ try { json = map.writeValueAsString(result); result.clear(); result = null; System.out.println("Send Message :::::::: : "+json); } catch (Exception e) { e.printStackTrace(); } } return json; } 

我的jQuery-AJAX将是:

 function sendMessage(){ var intxnId = $("#hide").val(); var message = $("#message").val(); alert("send : \n intxnId : "+intxnId+"\nmessage : "+message); $.ajax({ type: "POST", cache: false, url: contexPath + "/sendMessage.html", async: true, contentType: "application/x-www-form-urlencoded; charset=utf-8", scriptCharset: "utf-8", dataType: 'html', data: "intxnId=" + intxnId +"&message="+ encodeURIComponent(message), success: function(response){ if(response != null && response !="" && response !="null"){ var txt = '{"data":['+response+']}'; var json = eval ("(" + txt + ")"); for(i =0;i<json.data.length;i++){ var data = json.data[i]; var name = data.name; var message = data.message; var time = data.time; alert("Name : "+name+"\nMessage : "+message+"\ntime : "+time); var createHTML = send(name,message,time); $("#messageDisplayArea").append(createcreateHTML); }; } }, error: function(e){ alert('Error: ' + e); }, }); } 

但是当我发送当地语言信息அவர்களுக்கு (泰米尔语)时,我只得到了???????alert boxview page

但我在控制台中获得了local languageSysOut in controller )并且所有Special Characters有效。

注意:我认为我遇到了response from the controller问题。因为当我将messagecontroller我在javascript alert中将message as small boxes 。 但当response来了,我得到了?????alert box

我的控制台打印,

 Send Message UTF-8 ----------------- அவர்களுக்கு Mesge what I send :::: அவர்களுக்கு Send Message :::::::: : {"message":"அவர்களுக்கு","time":"time","name":"Human"} 

我不知道我错过了什么。

注意:我没有使用任何data base

希望我们的堆栈用户能提供更好的解决方案。 肯定赞赏好的答案。

我用这个测试控制器试过你的javascript:

 @Controller public class TestController { @RequestMapping(value = "/test", method = POST, produces = "application/json; charset=utf-8") public @ResponseBody ResponseEntity sendMessage(HttpSession session, @RequestParam String intxnId, @RequestParam String message, HttpServletRequest request, HttpServletResponse response) { System.out.println("Send Message UTF-8 ----------------- " + message); String json = null; HashMap result = new HashMap(); result.put("name", "test"); result.put("message", message); result.put("time", "time"); ObjectMapper map = new ObjectMapper(); if (!result.isEmpty()) { try { json = map.writeValueAsString(result); System.out.println("Send Message :::::::: : " + json); } catch (Exception e) { e.printStackTrace(); } } HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Content-Type", "application/json; charset=utf-8"); return new ResponseEntity(json, responseHeaders, HttpStatus.CREATED); } } 

它就像一个魅力,我使用Chrome浏览器在警报弹出窗口中看到响应中的UTF-8字符。

我的test.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%request.setCharacterEncoding("UTF-8");%>       

Test

你能尝试在@RequestMapping中使用produce =“text / plain; charset = UTF-8”吗?

 @RequestMapping(value = "/test", method = POST, produces = "text/plain;charset=UTF-8") public @ResponseBody 

还检查一下: Spring MVC中的UTF-8编码问题

我会在浏览器中尝试一些调试。 在浏览器中使用一些开发人员工具(Chrome中的开发人员工具, Firefox中的Firebug扩展或任何其他浏览器中的任何其他此类工具)并尝试检查您为ajax获取的HTTP响应 – 检查HTTP标头是否已正确设置(utf8编码)等等,也许如果ypur特殊字符在那里正确显示 – 所以你可以看到,如果你从服务器得到正确答案。

顺便说一句 – 我发现你发布的代码没有任何错误,一切看起来都不错。 只需仔细检查,您正在使用ajax显示响应的页面,具有utf8编码集…