zoukankan      html  css  js  c++  java
  • 从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+"&id="+id+"&realName="+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 } 

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

  • 相关阅读:
    @XmlAccessorType @XmlType 详解
    eclipse properties 文件查看和编辑插件 Properties Editor
    对象设计解耦的方法IOC和DI
    J2EE7与Servlet3.x
    关于maven
    Spring版本功能变更&Spring4.x的新特性
    2017第37周三
    Java线程池
    Java并发包
    Java内部类
  • 原文地址:https://www.cnblogs.com/gwq369/p/5596126.html
Copyright © 2011-2022 走看看