工作中用到了Extjs,从后台获取数据的时候,用到了extjs自己的Ext.data.store方法,然后封装了ExtGridReturn方法,
目的:前台用到Ext.data.store读取从后台传过来的数据,后台封装成ExtGridReturn类型
前台如下:
this.store = new Ext.data.Store({
remoteSort:true,
baseParams:{
start:0,
limit:this.pageSize
},
proxy:new Ext.data.HttpProxy({
method:‘POST‘,
url:this.getAllUrl
}),
reader:new Ext.data.JsonReader({
totalProperty:‘results‘,
root:‘rows‘
},[‘rulesId‘,‘rulesTitle‘,‘rulesContent‘,‘rulesType‘,‘updateUser‘,{
name:‘updateTime‘,
type:‘date‘,
dateFormat:‘time‘
},‘rulesAuthor‘,‘isTop‘,{
name:‘createTime‘,
type:‘date‘,
dateFormat:‘time‘
}])
});
当前的数据读取器是带有root和totalProperty的,所以,后台封装的类型如下:
package cn.edu.hbcf.common.vo;
import java.util.List;
/**
* Ext Grid返回对象
*
* @author LiPenghui
*
*/
public class ExtGridReturn {
/**
* 记录总条数
*/
private int results;
private int otherCount;
/**
* 所有数据
*/
private List<?> rows;
public ExtGridReturn() {
}
public ExtGridReturn(int results, List<?> rows) {
this.results = results;
this.rows = rows;
}
public int getResults() {
return results;
}
public void setResults(int results) {
this.results = results;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
public int getOtherCount() {
return otherCount;
}
public void setOtherCount(int otherCount) {
this.otherCount = otherCount;
}
}
Controller中的方法就可以这样写啦:如下:
@RequestMapping(value="/selectBaseRules",method=RequestMethod.POST)
@ResponseBody
public Object selectBaseRules(ExtPager pager,int rulesType){
Criteria criteria = new Criteria();
if(pager.getStart() !=null &&pager.getLimit() !=null){
criteria.setStart(pager.getStart());
criteria.setLimit(pager.getLimit());
criteria.setOracleStart(pager.getStart());
criteria.setOracleEnd(pager.getStart() + pager.getLimit());
}
try {
criteria.put("rulesType", rulesType);
List<BaseRules> list = baseRulesService.selectBaseRules(criteria);
int total= baseRulesService.getTotalCount();
return new ExtGridReturn(total, list);
} catch (Exception e) {
e.printStackTrace();
return new ExceptionReturn(e);
}
},
mybatis中的xml配置文件如下:
<select id="selectBaseRules" resultMap="BaseRulesMap" parameterType="Criteria">
<include refid="common.Oracle_Pagination_Head" />
select t.rules_id rulesId,
t.rules_title rulesTitle,
t.rules_content rulesContent,
t.rules_type rulesType,
t.update_time updateTime,
t.rules_author rulesAuthor,
t.is_top isTop,
t.create_time createTime,
t.UPDATE_USER updateUser,
s.account account,
s.real_name realName,
s.user_id userId
from SMS_BASE_RULES t
left join spauth.base_users s
on s.user_id = t.update_user
<where>
t.rules_type=#{condition.rulesType,jdbcType=INTEGER}
</where>
<include refid="common.Oracle_Pagination_Tail" />
</select>
很简单的封装了Extjs读取的类型。