js action 乱码

来源:互联网 发布:app刷量软件源码 编辑:程序博客网 时间:2024/06/05 15:16

js代码串中文字符串到action中出现乱码解决方案  

 前台要用

String job_name=new String(request.getParameter("job_name").getBytes("ISO-8859-1"),"UTF-8");

1、在通过js传值给Action前,运用encodeURIComponent() 函数先对要传的值进行编码。

2、然后在Action里获取编码并把编码转化成中文编码格式。

3、具体运用如下:在JavaScript里编码:

var value= encodeURIComponent(strValue);//对strValue进行编码

var url = "login!test.action?value="+value;//将value值附带在URL并传给loginAction中的test方法处理

sendRequest(url, test_callback);//发送处理请求 .... 在Action里获取编码并把转化成中文:

ActionContext ctx = ActionContext.getContext();

HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);//定义request对象

String value = new String(request.getParameter("value").getBytes("ISO-8859-1"),"UTF-8");//获取编码并把转化成中文



这个方法很有效啊 

原创粉丝点击