struts配置文件:使用jsonplugin将属性转换成json对象,extends="json-default" result type要为json
root 是只返回该内容,resultObj与action的属性名字一样,即要返回给页面的值
<package name="bps" extends="json-default">
<action name="bps" class="com.alik.bps.action.BpsAction" method="findBps">
<result type="json">
<param name="root">resultObj</param>
</result>
</action>
<action name="msg" class="com.alik.bps.action.MsgAction" method="findMsg">
<result type="json">
<param name="root">resultObj</param>
</result>
</action>
</package>
action中方法
private JSONObject resultObj;//此处定义为JSONObject是因为datagrid默认接收的为json对象而不是json字符串,如果定义为string 类型的页面读取数据时会报错,我之前报错为rows is not defined
此处省略resultObj的set\get方法
public String findMsg() throws Exception
{
Map map = new HashMap();
ArrayList al = new ArrayList();
for(int i=0;i<3;i++)
{
Map m = new HashMap();
m.put("itemid", String.valueOf(i));
m.put("mction", "a");
al.add(m);
}
map.put("total", 3);
map.put("rows", al);
resultObj=JSONObject.fromObject(map); //将map对象转换成为json对象
System.out.println(resultObj);
return SUCCESS;
}
页面
$('#inVariables').datagrid({
rownumbers:true,
title:'输入',
height:200,
singleSelect:false,
idField:'itemid',
url:'msg.action',
queryParams:{"ComKey":"","SvcKey":""},
columns:[[
{field:'ck',checkbox:true},
{field:'itemid',title:'Item ID',width:100,align:'right',
editor:{
type:'text'},
formatter:function(value){
return '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="insertAtCaret('+value+')">'+value+'</a>';
}
},
{field:'mction',title:'Action',width:100,align:'center',
formatter:function(value,row,index){
if (row.editing){
var s = '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="saverow('+index+',\'inVariables\')">保存</a> ';
var c = '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="cancelrow('+index+',\'inVariables\')">取消</a>';
return s+c;
} else {
var e = '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="editrow('+index+',\'inVariables\')">修改</a> ';
var rowid;
if(row.itemid=='')
{
rowid='null';
}
else
rowid=row.itemid;
var d = '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="deleterow('+index+',\'inVariables\','+rowid+')">删除</a>';
return e+d;
}
}
}
]],
toolbar:[{
text:'增加行',
iconCls:'icon-add',
handler:function(){
addRow('inVariables');
}
},{
text:'删除多行',
iconCls:'icon-cut',
handler:function(){deleteRows('inVariables');}
}],
onBeforeEdit:function(index,row){
row.editing = true;
$('#inVariables').datagrid('refreshRow', index);
},
onAfterEdit:function(index,row){
row.editing = false;
$('#inVariables').datagrid('refreshRow', index);
},
onCancelEdit:function(index,row){
row.editing = false;
$('#inVariables').datagrid('refreshRow', index);
},
onClickRow:function(index,row){
newDG(row);
}
});
PS:This blog is copy form the web, some of the contents is not correct, just paste on my blog for further reference
root 是只返回该内容,resultObj与action的属性名字一样,即要返回给页面的值
<package name="bps" extends="json-default">
<action name="bps" class="com.alik.bps.action.BpsAction" method="findBps">
<result type="json">
<param name="root">resultObj</param>
</result>
</action>
<action name="msg" class="com.alik.bps.action.MsgAction" method="findMsg">
<result type="json">
<param name="root">resultObj</param>
</result>
</action>
</package>
action中方法
private JSONObject resultObj;//此处定义为JSONObject是因为datagrid默认接收的为json对象而不是json字符串,如果定义为string 类型的页面读取数据时会报错,我之前报错为rows is not defined
此处省略resultObj的set\get方法
public String findMsg() throws Exception
{
Map map = new HashMap();
ArrayList al = new ArrayList();
for(int i=0;i<3;i++)
{
Map m = new HashMap();
m.put("itemid", String.valueOf(i));
m.put("mction", "a");
al.add(m);
}
map.put("total", 3);
map.put("rows", al);
resultObj=JSONObject.fromObject(map); //将map对象转换成为json对象
System.out.println(resultObj);
return SUCCESS;
}
页面
$('#inVariables').datagrid({
rownumbers:true,
title:'输入',
height:200,
singleSelect:false,
idField:'itemid',
url:'msg.action',
queryParams:{"ComKey":"","SvcKey":""},
columns:[[
{field:'ck',checkbox:true},
{field:'itemid',title:'Item ID',width:100,align:'right',
editor:{
type:'text'},
formatter:function(value){
return '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="insertAtCaret('+value+')">'+value+'</a>';
}
},
{field:'mction',title:'Action',width:100,align:'center',
formatter:function(value,row,index){
if (row.editing){
var s = '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="saverow('+index+',\'inVariables\')">保存</a> ';
var c = '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="cancelrow('+index+',\'inVariables\')">取消</a>';
return s+c;
} else {
var e = '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="editrow('+index+',\'inVariables\')">修改</a> ';
var rowid;
if(row.itemid=='')
{
rowid='null';
}
else
rowid=row.itemid;
var d = '<a href="http://blog.163.com/luckcq@yeah/blog/#" οnclick="deleterow('+index+',\'inVariables\','+rowid+')">删除</a>';
return e+d;
}
}
}
]],
toolbar:[{
text:'增加行',
iconCls:'icon-add',
handler:function(){
addRow('inVariables');
}
},{
text:'删除多行',
iconCls:'icon-cut',
handler:function(){deleteRows('inVariables');}
}],
onBeforeEdit:function(index,row){
row.editing = true;
$('#inVariables').datagrid('refreshRow', index);
},
onAfterEdit:function(index,row){
row.editing = false;
$('#inVariables').datagrid('refreshRow', index);
},
onCancelEdit:function(index,row){
row.editing = false;
$('#inVariables').datagrid('refreshRow', index);
},
onClickRow:function(index,row){
newDG(row);
}
});
PS:This blog is copy form the web, some of the contents is not correct, just paste on my blog for further reference