从js向Action传中文参数出现乱码问题的解决方法

Action获取jsp表单中的中文参数,只要整个项目都采用UTF-8编码格式都不会出现乱码问题;但JSP中用到JS,并从JS向Action传中文参数,就会出现中文乱的现象。

经过实践发现下面的方法可以解决中文乱码问题: 

JSP的JS中:中文参数用encodeURI(encodeURI(中文参数)),经过两次转码。例如: 

function show(next,id,realName){ 
document.forms['f2'].action="usersearchNextPage?next="+next+"&>encodeURI(encodeURI(realName)); 
document.forms['f2'].submit(); 
} 

其中 realName是中文参数。故在提交的URL中将realName转码两次。encodeURI(encodeURI(realName)) 

Action中:接收中文参数时解码。用:java.net.URLDecoder.decode(realName,"UTF-8"); 

如: 

1 String realName = ServletActionContext.getRequest().getParameter("realName"); 
2 try { 
3 realName = java.net.URLDecoder.decode(realName,"UTF-8"); 
4 } catch (UnsupportedEncodingException e1) { 
5 e1.printStackTrace(); 
6 } 

经过上述处理,问题解决。