通过js访问action带参数过去为乱码。用的struts2,配置文件中的乱码包括过滤均已配
jsp代码:<script type="text/javascript" charset="utf-8">
function search() {
var param = document.getElementById("param").value;
window.location.href="${ctx}/retraining/findUserByParam.html?param="+param;
}
</script>
action代码:public String findUserByParam(){
String param = getRequest().getParameter("param");
System.out.println(param);
userResultList = partenerManager.findUserByParam(param);
return SUCCESS;
}在后台打印就为乱码了,jsp页面编码都设置了

解决方案 »

  1.   

    这么写试试看public String findUserByParam(){
            String param = getRequest().getParameter("param");
            param = new String(param.getBytes("ISO-8859-1"),"utf-8");
            System.out.println(param);
            userResultList = partenerManager.findUserByParam(param);
            return SUCCESS;
        }
      

  2.   

    1.js
    var param = encodeURI(encodeURI(document.getElementById("param").value));
    window.location.href="${ctx}/retraining/findUserByParam.html?param="+param;2.Action
    param = URLDecoder.decode(param, "UTF-8");
      

  3.   

    1.js
    var param = encodeURI(encodeURI(document.getElementById("param").value));
    window.location.href="${ctx}/retraining/findUserByParam.html?param="+param;2.Action
    param = URLDecoder.decode(param, "UTF-8");
      

  4.   


    页面用encodeURI编码
    后台用URLDecoder解码
      

  5.   

    发生此问题是因为字符编码出现了问题:
    可能原因有二:一在接受参数之前没有进行字符转码,
    一般采用过滤器完成此功能,也可在代码中直接转码例如:
    public String findUserByParam(){
            request.setCharacterEncoding("utf-8");
            String param = getRequest().getParameter("param");
            System.out.println(param);
            userResultList = partenerManager.findUserByParam(param);
            return SUCCESS;
        }第二,与使用的服务器的配置有关,因为在处理get请求时转码工作有服务器
    进行处理,因此,需要修改服务器的配置文件。例如:tomcat需要修改server.xml
    具体修改内容如下:

     <Connector port="8000" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443" />
    修改为
    <Connector port="8000" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443"
       URIEncoding="UTF-8"/>
    即可。
      

  6.   

    window.location.href="${ctx}/retraining/findUserByParam.html?param="+param;
    改为
    window.location.href=="${ctx}/retraining/findUserByParam.html?param="+encodeURI(param)