您的位置:首页 > Web前端 > JavaScript

JS通过JSON传值给Action

2013-04-24 18:36 267 查看
<input>的name是动态获取的时候可以通过构成JSON传值给Action

jsp代码:

<script type="text/javascript" src="js/json2.js"></script>

<srcipt>

    function p_edit(id) {    

        var name={

                id:document.getElementById("id"+id).value,

                area:document.getElementById("area"+id).value,

        };

        name=JSON.stringify(name);

        targetForm = document.forms[0];

        targetForm.action = "edit.action?param="+encodeURIComponent(name);

    }

</script>

<s:iterator value="resultList1" status="stat" var="f">

         <td><input type="text"

                             name="<s:property value='key' /><s:property value="#stat.count" />"

                              value="<s:property value="resultList1[#stat.index][key]" />" /></td>

</s:iterator>

<td><input type="image" src="./images/edit.png"

                    onclick="p_edit(<s:property value="#stat.count" />);" /></td>

java代码

import net.sf.json.JSONObject; 

HttpServletRequest request = ServletActionContext.getRequest();

JSONObject json;

 json = JSONObject.fromObject(request.getParameter("param"));

  //转码,解决request.getparameter 乱码

setPlot_area(new String(json.get("area").toString().getBytes("ISO8859-1"),"UTF-8"));

setPlot_id(new String(json.get("id").toString().getBytes("ISO8859-1"),"UTF-8"));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JSON