项目中采用ext去请求后台,要求从后台返回一个日期类型的数据。具体的做法如下。在前台的js中
            this.eventStore = new Ext.data.JsonStore({
        id: 'eventStore',
        root: 'evts',
        url: '/OA/showSchedule.do?method=queryMyScheduleList',
        fields: [{name:'id',type:'int'},
                 {name:'cid',type:'int'},
                 'title', 
                 {name:'start', type:'date', dateFormat:'Y-m-d H:i:s'},
                 {name:'end', type:'date', dateFormat:'Y-m-d H:i:s'},
                 {name:'ad', type:'boolean'},
                 "url",
                 "notes",
                 "rem"]
    });
            
            this.eventStore.load();后台action中
List<Agenda> list = scheduleManagerService.querySchedule(paraAgenda);

JSONArray nodeArray = new JSONArray();
Agenda agendaEntity = null;

for (int i = 0; i < list.size(); i++) {
JSONObject node = new JSONObject();
agendaEntity = (Agenda)list.get(i);

node.put("id", agendaEntity.getId());
node.put("cid", agendaEntity.getId());
node.put("title", agendaEntity.getTitle());
node.put("start", DateUtil.formatTime(agendaEntity.getFromDate()));
node.put("end", DateUtil.formatTime(agendaEntity.getToDate()));
node.put("ad", false);
node.put("notes", agendaEntity.getDesc());
nodeArray.add(node);
}
JSONObject result = new JSONObject();
result.put("evts", nodeArray);
result.put("success", true);
try {
response.getWriter().print(result);
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
return null;问题时,start这个域如果设值成string类型的话,前台不能正常显示。如果设值成date类型的话,返回的jsonObject里面日期不是想象中的那样,变成"Day :xx,Month:xx,Year:xxxx,...."这样的格式,画面上页不能正常显示。
请问一下,怎样设置才能将日期类型的数据通过json数组正确的返回到前台来??

解决方案 »

  1.   

    node.put("start", DateUtil.formatTime(agendaEntity.getFromDate()));{name:'start', type:'date', dateFormat:'Y-m-d H:i:s'},
    这两个都改成string不就行了嘛
      

  2.   

    不好意思,'date'不能改,因为用了ext提供的sample,里面有大量的js会用到这个类型。
    如果改的话,需要改动的东西太多了
      

  3.   


    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    sf.format(你要显示的日期);这样转换一下不就是你要的类型了么!  
      

  4.   

    嗬嗬,这样不行啊,日期格式是转换出来了,但是通过json进行前后台传递的时候,变成了
    "date":4,"day":2,"hours":17,"minutes":18,"month":7,"seconds":34,"time":1249377514000,"timezoneOffset":-480,"year":109在网上找了个方法
    JsonConfig cfg = new JsonConfig();
       cfg.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
        private final String format="yyyy-MM-dd";
        public Object processObjectValue(String key, Object value,
          JsonConfig arg2)
        {
         if(value==null)
          return "";
         if (value instanceof Date) {
          String str = new SimpleDateFormat(format).format((Date) value);
          return str;
         }
         return value.toString();
        }    public Object processArrayValue(Object value, JsonConfig arg1)
        {
         return null;
        }
       });JSONArray json = JSONArray.fromObject(a,cfg);
    不知道行不行,等会试试看