日期:2014-05-16  浏览次数:20460 次

JS向Action传值 乱码问题解决
structs 2 中,如果在js中以一下形式向Action传值:



//获取文本框schoolText中的文本(要传给Action的schoolName)

var school = getElementById("schoolText").value;



//将schoolName文本框的值当做参数schoolName传给Aciton

var url = "listUsersInSchoolAction.action?schoolName="+school;



window.location.href=url;





那么这种情况下,如果输入是中文的话传递到后台就会成为乱码,解决办法——转码:

前台JS中:

     对要传递的参数进行转码: encodeURI(encodeURI(要传递的参数))

     上例中变为:

      var url = "listUsersInSchoolAction.action?schoolName="+encodeURI(encodeURI(school));



后台Acitoin中:

     //获取传递来的参数schoolName

     String school= request.getParameter("schoolName")+"";



    //对传递过来的参数进行转码
     school= java.net.URLDecoder.decode(school, "UTF-8"));




其他转码:

this.schoolName= new String(this.schoolName
       .getBytes("iso8859-1"), "gbk");


this.schoolName= new String(this.schoolName
       .getBytes("iso8859-1"), "utf-8");