Action获取javascriptjson对象并解析:
页面:
var oneRela = new SecretDetailRelaBean(resultRows[i].id,resultRows[i].effectMons,singleNo);
$.post(
'${base}/getObjectTest.htm',
{
oneRela: $.toJSON(oneRela)
},
function (data) //回传函数
{
}
);
//密级明细关联对象
function SecretDetailRelaBean(id,effectMons,singleNo)
{
this.id=id;
this.effectMons=effectMons;
this.singleNo=singleNo;
}
Action:(前提:bean的属性和javascript对象属性对应好)
try {
String json = request.getParameter("oneRela");
DetailSecretRelaBean bean = objectMapper.readValue(json, DetailSecretRelaBean.class);
System.out.println("oneRela: " + bean.getEffectMons());
System.out.println("11111");
}catch(Exception e){
e.printStackTrace();
}
传输数组及其他复杂对象请查看:Jackson解析JSON例子
combobox:
定义:
<input class="easyui-combobox" id="arcDetailListBox" name="arcDetailListBox"
data-options="
url:'',
valueField:'id',
textField:'text',
editable:false,
panelHeight:'auto',
onSelect: function(rec){
setInitDetailData(rec.id,rec.text);
}
"/>
获取当前选中的:
alert($("#arcDetailListBox").combobox('getValue'));//获取当前选中的值
alert($("#arcDetailListBox").combobox('getText'));获取当前选中的文字
动态加载数据:
$('#arcDetailListBox').combobox('clear');//clear
var aaa = [{"id":selectRows[0].detailId,"text":selectRows[0].detailName}];
$('#arcDetailListBox').combobox('loadData',aaa);//加载本数据
$('#arcDetailListBox').combobox('select',selectRows[0].detailId);//默认选中
datagrid:
查询选中的多行
var selectRows = $('#systemRightDataGrid').datagrid('getSelections');//弹出框中选中的行
//循环判断结果列表是否已经存放有选中的行,滤过已有的
for(var i=0;i<selectRows.length;i++){
$('#resultDataGrid').datagrid('appendRow',{
itemid: selectRows[i].systemRightId,
rightId: selectRows[i].systemRightId,
rightName: selectRows[i].systemRightName
});
}
动态加载:
$('#dataGrid_store').datagrid({url:'${base}/entryManage_queryStore.htm'})
点击当前行获取当前行数据:(注意需要斜杠引号的地方)
{field:'opt',title:'---',width:80 ,align:'center',
formatter:function(value,rec){
return '<span style="color:red"><input type="button" value="查看" height="7" onClick="getCurrentRow(\''+rec.storeName+'\')"/></span>';
}}
将当前所有行放入数组:
var resultRows = $('#secretLevelDataGrid').datagrid('getRows');//结果列表中的行
if(resultRows.length == 0) return null;
var arr = new Array();
for(var i=0;i<resultRows.length;i++){
a[i] = new Array(resultRows[i].id,resultRows[i].text)
}
清空datagrid:
$('#grid').datagrid('loadData',{total:0,rows:[]});
ajax传递数组:
页面调用部分:
$('#btn').click(function () {
$.ajax({
type: 'post',
url: './GridTable.aspx/call',
contentType: "application/json; charset=utf-8",
data: '{"userinfo":[{"name":"zs","age":"21"},{"name":"ls","age":"25"}]}',
dataType: 'json',
success: function (result) {
alert(result.d);
}
});
return false;
});
后台调用部分,两种方法均可
[WebMethod]
public static string call(Dictionary<string,string>[] userinfo)
{
System.Threading.Thread.Sleep(5000);
return str[0]["name"];
}
[WebMethod]
public static string test(List<UserinfoEntity> userinfos) {
System.Threading.Thread.Sleep(5000);
foreach (UserinfoEntity userinfo in userinfos) {
return order.rowid;
}
return "什么也没有";
}
最土的方法:
在struts2环境中,界面上有一个js数组,var id=[1,2,3,4,5,6] 利用ajax怎么传递到java后台啊 。。求高手
programmerxiaocai Replied at : 2012-04-18 10:55:51
数组和普通的值传递好像不一样,简单办法是,在前台用遍历数组,拼成字符串 var id="1,2,3,4,5,6",然后后台接收字符串以后用split在进行分割。
bill0605030109 Replied at : 2012-04-18 11:05:22
var id=[1,2,3,4,5,6];
var idString = "";
for(var i = 0;i < id.length;i++)
{
idString += id[i]+",";
}
$.getJSON("xxx.action?id=" + idString,function(data){
});
在action里,request.getParameter("idString")获取。
Alert框confirm框
$.messager.alert("操作提示", "弹出框示例操作成功!");
$.messager.confirm('提示', '你确定删除此条记录吗?', function(r){
if (r){
location.href = '__URL__/del/id/'+row.id;
}
$.messager.show({
title:'信息',
msg:'已经取消了删除操作'
});
});
}else{
$.messager.alert('警告','没有选择任何记录信息!','warning');}
页面:
var oneRela = new SecretDetailRelaBean(resultRows[i].id,resultRows[i].effectMons,singleNo);
$.post(
'${base}/getObjectTest.htm',
{
oneRela: $.toJSON(oneRela)
},
function (data) //回传函数
{
}
);
//密级明细关联对象
function SecretDetailRelaBean(id,effectMons,singleNo)
{
this.id=id;
this.effectMons=effectMons;
this.singleNo=singleNo;
}
Action:(前提:bean的属性和javascript对象属性对应好)
try {
String json = request.getParameter("oneRela");
DetailSecretRelaBean bean = objectMapper.readValue(json, DetailSecretRelaBean.class);
System.out.println("oneRela: " + bean.getEffectMons());
System.out.println("11111");
}catch(Exception e){
e.printStackTrace();
}
传输数组及其他复杂对象请查看:Jackson解析JSON例子
combobox:
定义:
<input class="easyui-combobox" id="arcDetailListBox" name="arcDetailListBox"
data-options="
url:'',
valueField:'id',
textField:'text',
editable:false,
panelHeight:'auto',
onSelect: function(rec){
setInitDetailData(rec.id,rec.text);
}
"/>
获取当前选中的:
alert($("#arcDetailListBox").combobox('getValue'));//获取当前选中的值
alert($("#arcDetailListBox").combobox('getText'));获取当前选中的文字
动态加载数据:
$('#arcDetailListBox').combobox('clear');//clear
var aaa = [{"id":selectRows[0].detailId,"text":selectRows[0].detailName}];
$('#arcDetailListBox').combobox('loadData',aaa);//加载本数据
$('#arcDetailListBox').combobox('select',selectRows[0].detailId);//默认选中
datagrid:
查询选中的多行
var selectRows = $('#systemRightDataGrid').datagrid('getSelections');//弹出框中选中的行
//循环判断结果列表是否已经存放有选中的行,滤过已有的
for(var i=0;i<selectRows.length;i++){
$('#resultDataGrid').datagrid('appendRow',{
itemid: selectRows[i].systemRightId,
rightId: selectRows[i].systemRightId,
rightName: selectRows[i].systemRightName
});
}
动态加载:
$('#dataGrid_store').datagrid({url:'${base}/entryManage_queryStore.htm'})
点击当前行获取当前行数据:(注意需要斜杠引号的地方)
{field:'opt',title:'---',width:80 ,align:'center',
formatter:function(value,rec){
return '<span style="color:red"><input type="button" value="查看" height="7" onClick="getCurrentRow(\''+rec.storeName+'\')"/></span>';
}}
将当前所有行放入数组:
var resultRows = $('#secretLevelDataGrid').datagrid('getRows');//结果列表中的行
if(resultRows.length == 0) return null;
var arr = new Array();
for(var i=0;i<resultRows.length;i++){
a[i] = new Array(resultRows[i].id,resultRows[i].text)
}
清空datagrid:
$('#grid').datagrid('loadData',{total:0,rows:[]});
ajax传递数组:
页面调用部分:
$('#btn').click(function () {
$.ajax({
type: 'post',
url: './GridTable.aspx/call',
contentType: "application/json; charset=utf-8",
data: '{"userinfo":[{"name":"zs","age":"21"},{"name":"ls","age":"25"}]}',
dataType: 'json',
success: function (result) {
alert(result.d);
}
});
return false;
});
后台调用部分,两种方法均可
[WebMethod]
public static string call(Dictionary<string,string>[] userinfo)
{
System.Threading.Thread.Sleep(5000);
return str[0]["name"];
}
[WebMethod]
public static string test(List<UserinfoEntity> userinfos) {
System.Threading.Thread.Sleep(5000);
foreach (UserinfoEntity userinfo in userinfos) {
return order.rowid;
}
return "什么也没有";
}
最土的方法:
在struts2环境中,界面上有一个js数组,var id=[1,2,3,4,5,6] 利用ajax怎么传递到java后台啊 。。求高手
programmerxiaocai Replied at : 2012-04-18 10:55:51
数组和普通的值传递好像不一样,简单办法是,在前台用遍历数组,拼成字符串 var id="1,2,3,4,5,6",然后后台接收字符串以后用split在进行分割。
bill0605030109 Replied at : 2012-04-18 11:05:22
var id=[1,2,3,4,5,6];
var idString = "";
for(var i = 0;i < id.length;i++)
{
idString += id[i]+",";
}
$.getJSON("xxx.action?id=" + idString,function(data){
});
在action里,request.getParameter("idString")获取。
Alert框confirm框
$.messager.alert("操作提示", "弹出框示例操作成功!");
$.messager.confirm('提示', '你确定删除此条记录吗?', function(r){
if (r){
location.href = '__URL__/del/id/'+row.id;
}
$.messager.show({
title:'信息',
msg:'已经取消了删除操作'
});
});
}else{
$.messager.alert('警告','没有选择任何记录信息!','warning');}