struts2+dojo实现datagrid
ACTION:
package test.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class JSONDataAction extends ActionSupport {
private String identifier="name";
private String label="name";
private List<Map<String,Object>> items=new ArrayList<Map<String,Object>>();
public JSONDataAction(){
items.clear();
Map<String,Object> mapItem=null;
for(int i=0;i<15;i++)
{
mapItem=new HashMap<String,Object>();
mapItem.put("name", "name"+i);
mapItem.put("baseFlavor", "banana"+i);
mapItem.put("mixins", "fudge"+i);
mapItem.put("calories", 280+i);
mapItem.put("fat", 16+i);
mapItem.put("source", 1+i);
mapItem.put("rating", 3+i);
items.add(mapItem);
}
}
public String execute(){
return this.SUCCESS;
}
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<Map<String,Object>> getItems() {
return items;
}
public void setItems(List<Map<String,Object>> items) {
this.items = items;
}
}
struts.xml(注意package要继承json-default):
<package name="jsontest" extends="json-default">
<action name="getJSONResult" class="test.action.JSONDataAction">
<result type="json" />
</action>
</package>
JSP(DOJO的版本是:1.10.4):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>One Subrow</title>
<style type="text/css">
@import "dojoroot/dijit/themes/tundra/tundra.css";
@import "dojoroot/dojox/grid/resources/tundraGrid.css";
</style>
<script type="text/javascript" src="dojoroot/dojo/dojo.js" djConfig="parseOnLoad: true"></script>
<script type="text/javascript">
dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dojo.data.ItemFileWriteStore")
</script>
</head>
<body class="tundra">
<span dojoType="dojo.data.ItemFileReadStore"
jsId="icStore" url="getJSONResult.action">
</span>
<div align="center" >
<table id="grid" dojoType="dojox.grid.DataGrid"
store="icStore"
clientSort="true"
style="width: 800px; height: 200px;">
<thead>
<tr>
<th field="name">Flavor</th>
<th field="baseFlavor" >Base Flavor</th>
<th field="calories" >Calories</th>
<th field="fat" >Fat</th>
<th field="mixins">Mixins</th>
<th field="source" >Source</th>
<th field="rating">Rating</th>
</tr>
</thead>
</table>
</div>
</body>
效果:

浙公网安备 33010602011771号