二、建立第一个demo,实现查数据库显示到页面 步骤:1.建立mysql数据库表
2.整合ssh框架
3.用extjs显示
关键注意事项:
ext.data.jsonreader中root的含义,例如,请求的action返回的json
此时root属性为应这样填写:root:list 1、建立mysql数据库,如图2-1所示
图2-1
2、整合ssh框架
由于extjs处理的为json数据,则应将action返回的置为json格式
action类如下所示,返回为list
package com.hanhexin.action;import java.util.arraylist;import java.util.list;import org.apache.struts2.convention.annotation.result;import org.springframework.beans.factory.annotation.autowired;import org.springframework.context.annotation.scope;import org.springframework.stereotype.component;import com.hanhexin.entity.person;import com.hanhexin.service.ipersonservice;import com.opensymphony.xwork2.actionsupport;@component(personaction)@scope(prototype)public class personaction extends actionsupport{ private person person; @autowired private ipersonservice personservice; list list = new arraylist(); public string list(){ person = new person(); list = personservice.getlistbyperson(person, 1, 10); system.out.println(list.size()+action+++++++++++++++++++++); return success; } public list getlist() { return list; } public void setlist(list list) { this.list = list; }}
配置struts.xml文件返回json,如下所示
一定要设...
3、用extjs显示到页面 jsp文件如下所示
my jsp 'main.jsp' starting page
解释
1)ext.onready为ext.loader.onready的别名
extjs api中的解释如下所示
此时用到的只是第一个参数,后面两个参数没有用到
2)var proxy = new ext.data.httpproxy({url:'list.action'});
请求action,其中返回的数据为
封装到了var proxy中
3)var record = ext.data.record.create(),用于解析json,其中属性为json中相对应的数据项
4)var reader = new ext.data.jsonreader({
root:'list'
},record);
用于解析json,其中root为json中的list,即2)中的list
5)var store = new ext.data.store()用于存储从action返回的,并由jsonreader解析的数据。
6)var grid = new ext.grid.gridpanel()用于显示数据
其中renderto:ext.getbody() 保证了显示到html页的body中。
4、最终效果如图2-3所示
图2-3
源码下载地址:http://download.csdn.net/detail/hhxin635612026/7602631
