项目中采用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数组正确的返回到前台来??
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数组正确的返回到前台来??
解决方案 »
- 在jsp页面中实现按钮点击打开和保存本地文本文件
- jQuery
- jsp中格式化显示字符串
- 在DispartcheAction中用getParameter()得不到text中的值了,Why?
- tomcat 在压力测试下 cpu 一直99% 怎么解决
- JSTL重要吗?学jsp的一定要学吗?实际开发中用的多吗?
- 求购~~~毕业设计题为工资管理系统系统
- 请教JAVA.WEB怎样才能融会贯通
- 在Weblogic下,用jsp连mysql数据库,总有问题,请大家指点。在线等!!!!!!!!!!
- springmvc之@ResponseBody用的那么多,到底真实的作用是什么
- java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establis
- 学员信息及学员获得学分的展示问题
这两个都改成string不就行了嘛
如果改的话,需要改动的东西太多了
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
sf.format(你要显示的日期);这样转换一下不就是你要的类型了么!
"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);
不知道行不行,等会试试看