Bootstrap嵌入jqGrid,使你的table牛逼起来(3)

简介: Bootstrap嵌入jqGrid,使你的table牛逼起来

####④ 、jqGrid的数据操作


数据操作部分,我认为包含有 检索参数传递、分页排序参数传递、sql语句的编写。


关于参数传递,前端的参数封装在③中已有介绍,我们来看一看controller中如何处理数据的。


首先,我们来定义PageGrid,也就是jqGrid中xmlReader的数据源。

package com.honzh.common.page;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("pageGrid")
@SuppressWarnings("rawtypes")
public class PageGrid {
  private int page;
  private int total;
  private int records;
  private List data;
  public int getPage() {
    return this.page;
  }
  public void setPage(int page) {
    this.page = page;
  }
  public int getTotal() {
    return this.total;
  }
  public void setTotal(int total) {
    this.total = total;
  }
  public int getRecords() {
    return this.records;
  }
  public void setRecords(int records) {
    this.records = records;
  }
  public List getData() {
    return this.data;
  }
  public void setData(List data) {
    this.data = data;
  }
}


XStreamComponent.java

package com.honzh.common.page;
import org.apache.commons.lang.StringUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.DefaultMapper;
import com.thoughtworks.xstream.mapper.XStream11XmlFriendlyMapper;
public class XStreamComponent {
  private XStream xstream;
  public static XStreamComponent newInstance() {
    XStreamComponent xmlComponent = new XStreamComponent();
    xmlComponent.alias(new Class[] { PageGrid.class });
    return xmlComponent;
  }
  public XStreamComponent() {
    this.xstream = new XStream(new DomDriver());
  }
  public String toXML(Object obj) {
    return this.xstream.toXML(obj);
  }
  public String toPageXML(Object obj) {
    registerConverter(new MapCustomConverter(new DefaultMapper(XStream11XmlFriendlyMapper.class.getClassLoader())));
    return toXML(obj);
  }
  public Object fromPageXML(String xml) {
    registerConverter(new MapCustomConverter(new DefaultMapper(XStream11XmlFriendlyMapper.class.getClassLoader())));
    return fromXML(xml);
  }
  public Object fromXML(String xml) {
    return this.xstream.fromXML(xml);
  }
  @SuppressWarnings("rawtypes")
  public void processAnnotations(Class type) {
    this.xstream.processAnnotations(type);
  }
  @SuppressWarnings("rawtypes")
  public void processAnnotations(Class[] types) {
    this.xstream.processAnnotations(types);
  }
  @SuppressWarnings("rawtypes")
  public void alias(String name, Class type) {
    this.xstream.alias(name, type);
  }
  @SuppressWarnings("rawtypes")
  public void alias(Class[] types) {
    for (Class type : types) {
      String className = type.getName();
      try {
        String[] classNames = StringUtils.split(className, ".");
        this.xstream.alias(classNames[(classNames.length - 1)], type);
      } catch (Exception ex) {
        this.xstream.alias(className, type);
      }
    }
  }
  public void registerConverter(Converter converter) {
    this.xstream.registerConverter(converter);
  }
  @SuppressWarnings("rawtypes")
  public void useAttributeFor(Class definedIn, String fieldName) {
    this.xstream.useAttributeFor(definedIn, fieldName);
  }
}


MapCustomConverter.java

package com.honzh.common.page;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
public class MapCustomConverter extends AbstractCollectionConverter {
  public MapCustomConverter(Mapper mapper) {
    super(mapper);
  }
  @SuppressWarnings("rawtypes")
  public boolean canConvert(Class type) {
    return (type.equals(HashMap.class)) || (type.equals(Hashtable.class))
        || (type.getName().equals("java.util.LinkedHashMap"))
        || (type.getName().equals("sun.font.AttributeMap"));
  }
  @SuppressWarnings({ "rawtypes" })
  public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    Map map = (Map) source;
    for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
      Map.Entry entry = (Map.Entry) iterator.next();
      writer.startNode(entry.getKey() == null ? "null" : entry.getKey().toString());
      writer.setValue(entry.getValue() == null ? "" : entry.getValue().toString());
      writer.endNode();
    }
  }
  @SuppressWarnings("rawtypes")
  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    Map map = (Map) createCollection(context.getRequiredType());
    populateMap(reader, context, map);
    return map;
  }
  @SuppressWarnings({ "rawtypes", "unchecked" })
  protected void populateMap(HierarchicalStreamReader reader, UnmarshallingContext context, Map map) {
    while (reader.hasMoreChildren()) {
      reader.moveDown();
      Object key = reader.getNodeName();
      Object value = reader.getValue();
      map.put(key, value);
      reader.moveUp();
    }
  }
}
相关文章
|
JSON 前端开发 数据格式
bootstrap table表格的点击详情按钮操作
bootstrap table表格的点击详情按钮操作
101 1
|
6月前
|
前端开发 容器
bootstrap table 设置自定义列宽
【5月更文挑战第4天】bootstrap table 设置自定义列宽
|
前端开发 JavaScript
Bootstrap Table根据参数搜索功能
Bootstrap Table根据参数搜索功能
95 0
|
前端开发
bootstrap table表格去掉排序箭头
bootstrap table表格去掉排序箭头
202 2
|
前端开发
bootstrap table表格外面的边框全部去掉
bootstrap table表格外面的边框全部去掉
209 1
|
前端开发
bootstrap table点击修改按钮给弹框赋值
bootstrap table点击修改按钮给弹框赋值
50 0
|
JSON 前端开发 数据库
Bootstrap Table使用教程(请求json数据渲染表格)
Bootstrap Table使用教程(请求json数据渲染表格)
151 0
|
前端开发
bootstrap table分页悬停颜色改变
bootstrap table分页悬停颜色改变
38 0
|
前端开发
解决bootstrap table刷新加载时白色闪屏问题
解决bootstrap table刷新加载时白色闪屏问题
80 0
|
前端开发
bootstrap table+layer实现一个表格删除
bootstrap table+layer实现一个表格删除
55 0
下一篇
无影云桌面