67,512
社区成员
发帖
与我相关
我的任务
分享
$.ajax({
url:'/test/test.do',
type:"POST",
data:"test="+test, //test是中文字符
error: function(msg){
alert('Error loading XML document'+msg);
},
success: function(data)
{
alert(data);
}
})
public class UsbkeyWriteCheckUsbkeyInfo extends Action {
······
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session=request.getSession();
UsbkeyForm usbkeyForm = (UsbkeyForm)form;
System.out.println(usbkeyForm.getJgdm());
String test= request.getParameter("test");
String responseText="1";
PrintWriter out=response.getWriter();
out.println(responseText);
return super.execute(mapping, form, request, response);
}
}
Strut2 + MySql + Hibernate + ajax(jQuery)中文乱码解决办法:
1. 网页页面使用utf-8
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
2. ajax默认使用utf-8传输数据, 但最好使用post, 不要使用get
3. Struts2中使用UTF-8.
3.1 在struts.properties中加上: struts.i18n.encoding=UTF-8
[3.2 使用Filter: 从页面中传输数据到Struts2, 需要使用过滤器把数据在交给struts2前转换成utf-8的: http://stevieliu.blogchina.com/stevieliu/6223098.html]
试过不使用3.2照常可以收到中文.
4. 数据库也要使用utf-8, 在建表时设置好.
http://imysql.cn/charset_tips
5. hibernate.cf.xml配置文件中与数据库的连接符串也要标明使用的编码为utf-8: <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mlm?useUnicode=true&characterEncoding=UTF-8</property>
String test = request.getParameter("test");
//Action收到请求参数默认是以老外的ISO8859-1来解码的
test = new String(test.getBytes("ISO8859-1"),"UTF-8");
就可以解决乱码了。