用json-lib如何来处理数据,在前台实现分页

来源:互联网 发布:全站仪数据 编辑:程序博客网 时间:2024/04/27 16:29

//查询出所有的员工信息
    public void selectAllPersonInfo() throws IOException{
        response.setContentType("text/html;charset=utf8");
        PrintWriter out = response.getWriter();
       
        int start = Integer.parseInt(request.getParameter("start"));  //得到起始和每页显示的数据
        int limit = Integer.parseInt(request.getParameter("limit"));
        int end = start + limit; //下一面起页条数
       
        workList = this.workerService.selectAllWorker(); //查询出所有的员工信息
        int totalProperty = workList.size();  //总条数

        if(end > totalProperty){
            end = totalProperty; //最大条数只能为总条数
        }
  
        //为分页准备数据
        List<Human> list = new ArrayList<Human>();
        for(int i=start; i<end; i++){
            //把当前页所有的数据显示在页面上
            list.add(new Human(i,"林邪" + i,"男",new Date(),"本科"+ i,"部门"+i,"牛X人一个"));
        }
   
        //定义分页所需要的格式
        PageBean pageBean = new PageBean(totalProperty,list);
        JSONObject jObject = JSONObject.fromObject(pageBean,JsonUtil.configJson("yyyy-MM-dd"));

        out.print(jObject.toString());
        out.flush();
        out.close();
    }

 

这里处理这段代码就是用来处理json数据的,定义的PageBean代码:

/**
 * 这个类用来封装totalProperty和root属性
 * @author Administrator
 *
 */
public class PageBean {
    public PageBean(int totalProperty, List root) {
        super();
        this.totalProperty = totalProperty;
        this.root = root;
    }
    public PageBean() {
        super();
        // TODO Auto-generated constructor stub
    }
    private int totalProperty;
    private List root;
   
   
    public int getTotalProperty() {
        return totalProperty;
    }
    public void setTotalProperty(int totalProperty) {
        this.totalProperty = totalProperty;
    }
    public List getRoot() {
        return root;
    }
    public void setRoot(List root) {
        this.root = root;
    }
}

还有一个就是专门来用来处理日期的类:我们要重写Processor,看下面的代码 :来定义一个工具类:

public class DateJsonValueProcessor implements JsonValueProcessor {

     public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss"; 
         private DateFormat         dateFormat; 
      
         /**
          * 构造方法.
         * 
          * @param datePattern 日期格式
          */ 
         public DateJsonValueProcessor(String datePattern){ 
            try { 
                 dateFormat = new SimpleDateFormat(datePattern); 
             } catch (Exception ex) { 
                 dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN); 
             } 
         } 
      
         public Object processArrayValue(Object value, JsonConfig jsonConfig) { 
             return process(value); 
         } 
      
         public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { 
             return process(value); 
         } 
     
         private Object process(Object value) { 
             return dateFormat.format((Date) value); 
         } 
        
}

然后就是上面的JsonUtil类了:

public class JsonUtil {
     public static JsonConfig configJson(String datePattern) { 
          JsonConfig jsonConfig = new JsonConfig(); 
          jsonConfig.setExcludes(null); 
          jsonConfig.setIgnoreDefaultExcludes(false); 
         jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); 
          jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor(datePattern)); 
       
          return jsonConfig; 
      } 
}

 

 

运行的结果如下: