gpt4 book ai didi

grails - 当我从datepicker获取日期并以groovy解析它时,无法保存对象

转载 作者:行者123 更新时间:2023-12-02 14:49:46 27 4
gpt4 key购买 nike

我是新手。我在GSP页面中使用datepicker。当我使用它获取日期并在服务器端对其进行解析时,无法保存该对象。

我的域名是

class PurchaseOrder {
Date orderDate;
Date deliveryDate;
Double totalQuantity;
Double deliveredQuantity;
Double totalCost;
Long totalItem;
Long deliveredTotalItem;
Long requisition

static belongsTo = [branch:OrganizationalBranch,orderedBy: Employee, supplier: Supplier]
static hasMany = [itemDetails:OrderDetails]



static constraints = {
deliveryDate(nullable: true);
deliveredQuantity(nullable: true);
deliveredTotalItem(nullable: true);

itemDetails(nullable: true);
orderDate(nullable: true);
totalItem(nullable: true);
totalQuantity(nullable: true);
totalCost(nullable: true);
}
}

我的GSP是:
<div class="fieldcontain ${hasErrors(bean: purchaseOrderInstance, field: 'deliveryDate', 'error')} ">
<label for="deliveryDate">
<g:message code="purchaseOrder.deliveryDate.label" default="Delivery Date" />

</label>
<g:field type="deliveryDate" id="deliveryDate" name="deliveryDate" class="datepicker" />

</div>

而Controller的代码是:
try {
String orderDateSt = params?.deliveryDate
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy")
def orderDate = new Date().parse("MM/dd/yyyy", orderDateSt.trim())
purchaseOrderInstance.deliveryDate = orderDate
}catch (Exception ex) {
ex.getMessage()
ex.printStackTrace()
}

错误是:
purchase.PurchaseOrderController  - Field error in object 'com.sb.ims.purchase.PurchaseOrder' on field 'deliveryDate': rejected value [06/30/2016]; codes [com.sb.ims.purchase.PurchaseOrder.deliveryDate.typeMismatch.error,com.sb.ims.purchase.PurchaseOrder.deliveryDate.typeMismatch,purchaseOrder.deliveryDate.typeMismatch.error,purchaseOrder.deliveryDate.typeMismatch,typeMismatch.com.sb.ims.purchase.PurchaseOrder.deliveryDate,typeMismatch.deliveryDate,typeMismatch.java.util.Date,typeMismatch]; arguments [deliveryDate]; default message [Unparseable date: "06/30/2016"]

我怎么解决这个问题。

最佳答案

如果您的Grails版本大于等于2.3,则应该能够使用BindingFormat批注而不是在 Controller 中解析日期:

导入org.grails.databinding.BindingFormat

class PurchaseOrder { 
@BindingFormat('MM/dd/yyyy')
Date orderDate
@BindingFormat('MM/dd/yyyy')
Date deliveryDate
....
}

关于grails - 当我从datepicker获取日期并以groovy解析它时,无法保存对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37542558/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com