Java extjs 4.0.2a mvc中的日期传递错误
我的模型代码是Java extjs 4.0.2a mvc中的日期传递错误,java,hibernate,spring,extjs4,extjs-mvc,Java,Hibernate,Spring,Extjs4,Extjs Mvc,我的模型代码是 Ext.define('gantt.model.Project', { extend: 'Ext.data.Model', fields : [ { name: 'id', type: 'int', useNull: true, mapping: 'projectid'}, { name: 'title', type: 'string', mapping: 'projecttitle'},
Ext.define('gantt.model.Project', {
extend: 'Ext.data.Model',
fields : [
{ name: 'id', type: 'int', useNull: true, mapping: 'projectid'},
{ name: 'title', type: 'string', mapping: 'projecttitle'},
{ name: 'name', type: 'string', mapping: 'projectname'},
{ name: 'description', type: 'string', mapping: 'projectdesc'},
{ name: 'startdate', type: 'date', mapping: 'startdate', dateFormat :'time'},
{ name: 'enddate', type: 'date', mapping: 'enddate', dateFormat :'time'},
]
});
我的观点是
Ext.define('gantt.view.projectmgt.projectAdd', {
extend: 'Ext.form.Panel',
alias: 'widget.projectadd',
title: 'New Project Detail Input Window',
width: '50%',
xtype:'fieldset',
title: 'Project Details',
collapsible: true,
defaultType: 'textfield',
layout: 'anchor',
defaults: {
anchor: '100%'
},
items :[{
fieldLabel: 'Title',
name: 'title',
xtype: 'textfield'
},{
fieldLabel: 'Name',
name: 'name',
xtype: 'textfield'
},{
fieldLabel: 'StartDate',
name: 'startdate',
xtype: 'datefield',
format: 'Y/m/d'
},{
fieldLabel: 'EndDate',
name: 'enddate',
xtype: 'datefield',
format: 'Y/m/d'
}, {
xtype: 'htmleditor',
name: 'description',
fieldLabel: 'Description',
height: 200,
anchor: '100%'
}],
buttons: [{
text: 'Save',
action: 'show-gantt-view'
},{
text: 'Cancel',
action: 'cancel'
}]
});
我的窗体面板有保存按钮,当我点击它时,我的控制器执行以下方法
createProjectGanttpanel: function(btn) {
var win = this.getProjectAdd().getForm().getValues();
console.log('PANEL VALUES ARE ::'+win["startdate"]);
record = Ext.create('gantt.model.Project');
record.set(win);
this.getProjectsStore().add(record);
this.getProjectsStore().sync();
}
此方法将值指定给模型,然后存储并保存到我的数据库中。但是,当我选择控制台日志中打印的格式为“2012/01/31”的日期时,我遇到了问题。但是当我看到firebug POST选项卡时,它会显示startdate传递为“-19800000”,enddate传递为“-19800000”
在服务器端,当我查看JAVA控制台时,它会向我显示“开始日期”:“1970-01-01T05:30:02”,“结束日期”:“1970-01-01T05:30:02”,这是不正确的。因此,在“我的网格”面板中无法查看正确的日期
我在这里所做的在我的代码中是错误的。帮我找到问题,以便我能尽快解决
我正在使用extjs 4.0.2a mvc和JAVA作为服务器端技术。您的日期格式当前设置为
dateFormat:“time'
,您是否尝试将其更改为:
dateFormat: 'Y-m-d h:i:s'
在类似的设置中,这对我很有效
更新2012-01-30:
查看此API文档以了解有关在ExtJs中格式化日期的更多信息:查看是否对您有帮助-我曾经遇到过类似的问题,并决定在ExtJs和Spring中一致使用ISO 8601格式的日期,这是实现此功能的方法:
总结如下:
使用自定义Jackson ObjectMapper以ISO8601格式写入日期:
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper(){
super.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
}
}
Ext.JSON.encodeDate = function(o)
{
return '"' + Ext.Date.format(o, 'c') + '"';
};
注册此自定义对象映射器:
<mvc:annotation-driven >
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="objectMapper">
<bean class="org.bk.simplygtd.web.spring.CustomObjectMapper"/>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
设置Ext JS以将日期以ISO-8601格式提交回服务器:
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper(){
super.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
}
}
Ext.JSON.encodeDate = function(o)
{
return '"' + Ext.Date.format(o, 'c') + '"';
};
您可以实现一个定制的
Ext.data.writer.writer
,它只需修改所有日期字段,使之能够被Java读取,而不是定制服务器端以接受伪造的日期格式。然后在注册代理时,只需将writer:'javajson'
添加到配置中即可
/**
* Java compatible Json writer
* @class My.component.data.writer.JavaJson
* @extends Ext.data.writer.Json
*/
Ext.define('My.component.data.writer.JavaJson', {
extend: 'Ext.data.writer.Json',
alternativeClassName: 'My.component.data.JavaJsonWriter',
alias: 'writer.javajson',
//inherit docs
getRecordData: function(record, operation) {
var data = this.superclass.getRecordData(record, operation),
fields = record.fields,
fieldItems = fields.items,
nameProperty = this.nameProperty,
field,
name,
value;
for(f = 0; f < fieldItems.length; f++) {
field = fieldItems[f];
if(field.type === Ext.data.Types.DATE) {
name = field[nameProperty] || field.name;
value = record.get(field.name);
data[name] = Ext.isEmpty(value) ? value : value.getTime();
}
}
return data;
}
});
/**
*Java兼容Json编写器
*@class My.component.data.writer.JavaJson
*@extensed Ext.data.writer.Json
*/
Ext.define('My.component.data.writer.JavaJson'{
扩展:“Ext.data.writer.Json”,
alternativeClassName:'My.component.data.JavaJsonWriter',
别名:“writer.javajson”,
//继承文档
getRecordData:函数(记录、操作){
var data=this.superclass.getRecordData(记录,操作),
字段=记录.fields,
fieldItems=fields.items,
nameProperty=this.nameProperty,
领域
名称
价值
对于(f=0;f
在服务器端,Java Date对象以毫秒为单位使用日期,而您的客户机以秒为单位使用日期,否?您能否在设置后记录记录的外观?可能是此set
方法处理不正确。在设置日期后,我的记录看起来像1970-01-01 00:00:00。问题在于日期格式。选择日期后,我得到的日期为2012/01/31,但将其分配给我的模型后,它会变成一些垃圾值,如1970-01-01 00:00:00,发送到服务器。你知道如何解决这个问题吗?@nico_ekito实际上到目前为止,Java在这里什么都不用做。只是使用了extjs。extjs格式只传递Java将要处理的日期。所以我认为问题只在于extjs-dateformat