这个例子演示的是怎么从后台取出数据然后将数据显示在xtemplate中
Ext.Ajax.request({
method:'POST',
url:'Index_list.action', //要访问的url地址
params:{'indexId':indexId}, //传递的参数
success: function (data) {
var response = Ext.util.JSON.decode(data.responseText); //action总返回的json的对象
var tpl = new Ext.XTemplate(
'<tpl for="root">', //
'<p>Index Name :{indexName}</p>',
'<p>Once Limit :{once_limit}</p>',
'<p>Cnt Limit :{cnt_limit}</p>',
'<p> Charge :{charge}</p>',
'<input type="button" value="Show Price" οnclick="showPanel()"/>', //showPanel()是一个函数
'</tpl>' //可以自己定义
);
tpl.compile();
tpl.overwrite(Ext.getCmp("index_Message").body, response);
}
});
struts.xml中的配置信息
<package name="index" namespace="/" extends="json-default">
<action name="Index_*" class="indexAction" method="{1}">
<result name="success" type="json"></result>
</action>
</package>
Action类里面的方法的执行
public String list() throws IOException
{
PrintWriter out = ServletActionContext.getResponse().getWriter();
list = indexService.list(indexId);
jsonString ="{success:true,root:"+createGridJson()+"}";
out.println(jsonString);
out.flush();
out.close();
return SUCCESS;
}
/**
* @return list
*/
private String createGridJson() {
StringBuffer sb = new StringBuffer();
sb.append("[");
for(int i=0;i<list.size();i++)
{
Object [] o = (Object[]) list.get(i);
sb.append("{indexName:'"+o[0]+"',");
sb.append("once_limit:"+o[1]+",");
sb.append("cnt_limit:"+o[2]+",");
sb.append("charge:"+o[3]+"},");
}
sb.deleteCharAt(sb.lastIndexOf(","));
sb.append("]");
return sb.toString();
}
这样前台的xtemplate就能显示啦。我这个Action类中写的传递到前台的json对象可能有些麻烦,大家有好的方法和我分享哦,因为我是个新手,知识想把每天自己学习的内容和大家分享一下。