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"));
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"));
相关文章推荐
- php 和 js 通过 json 传值
- js通过地址栏给action传值(中文乱码全是问号)
- struts2项目jsp页面通过js调取action中的封装的json
- js通过地址栏给action传值(中文乱码全是问号)
- knock.js的subscribe使用 通过$.ajax(); $get();解析JSON数据
- html中通过js获取接口JSON格式数据解析以及跨域问题
- Action可以传值到js里
- Action往js中传递Json(text,object,List)
- php和js如何通过json互相传递数据(转)
- 【struts2】【4】通过ajax向struts2 action请求json
- js传值到action出现中文乱码问题
- struts2-通过action从后台将json数据传到前台的方法(二)
- struts把action里的list转化为js里的json
- js通过url向后台传值中文乱码问题
- php怎么通过Json把数据库查询到的所有东西都传给js
- Struts2:通过action标签向页面传值
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- esui控件validatebox 通过正则判断输入 json传值
- js通过变量获得json数据