(Spring + JSP + jQuery-AJAX + JSON) configurando el entorno para el problema de UTF-8?
Estoy haciendo unchat project
enjava
conSpring 3.x
que necesitaMulti-language support
.
Esto es lo que he hecho.
Mi JSP tiene:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Miweb.xml
tiene:
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
En miTomcat server.xml
tiene:
<Connector (...) URIEncoding="UTF-8" />
Mi miJava environment
tiene:
JAVA_TOOL_OPTIONS -Dfile.encoding=UTF8
En miSpring-controller
tiene:
@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<String,String> 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;
}
MijQuery-AJAX
estarán:
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);
},
});
}
Pero cuando envío el mensaje en idioma localஅவர்களுக்கு
(Idioma tamil), solo tengo???????
en elalert box
y elview page
.
Pero tengo ellocal language
en la consola (SysOut in controller
) y todoSpecial Characters
trabajos.
Nota : Creo que estoy teniendo el problema conresponse from the controller
.Porque cuando envío elmessage
alcontroller
tengo elmessage as small boxes
enjavascript alert
. Pero cuando elresponse
vino estoy recibiendo el?????
en elalert box
.
Mi consola imprime,
Send Message UTF-8 ----------------- அவர்களுக்கு
Mesge what I send :::: அவர்களுக்கு
Send Message :::::::: : {"message":"அவர்களுக்கு","time":"time","name":"Human"}
No sé lo que me estoy perdiendo.
Nota : No estoy usando ningunadata base
.
Espero que nuestros usuarios de pila den una mejor solución. Las buenas respuestas son definitivamente apreciadas.