我是一个grails和新手。
我正在尝试更新域类中的一个字段,前提是该字段的值由用户在“编辑” View 中提供。
我的网域类别如下:
类CertificateInfo {
static auditable = [ignore:['dateCreated','lastUpdated','account']]
String entityId
String certificate
String status
Timestamp dateCreated
Timestamp lastUpdated
Account account
CertificateInfo(){
status = "ACTIVE"
}
static belongsTo = [account: Account]
//Db mappings not included
static constraints = {
entityId size: 1..300, blank: false, unique: true
certificate size: 1..4000, blank:false
status in:['ACTIVE','INACTIVE'], blank: false
}
}
在我的编辑表单中,相关字段为:
<td>
<input type="file" name="certificate" value="${CertificateInfo ?.certificate}"/>
</td>
我想在这里做的是:
a)如果提供了新文件,则在按钮上单击,该文件应被接受为证书。此部分有效,新文件用于更新证书
b)如果未提供新文件,则应仅保留旧值。这部分无效,因为我不知道如何处理。
我很高兴有任何建议。
谢谢!
最佳答案
用户指南中对此进行了概述-http://grails.org/doc/latest/guide/theWebLayer.html#uploadingFiles
使用第一个示例,您将执行以下操作
def f = request.getFile('certificate')
if (f.empty) {
// do nothing
} else {
f.transferTo(new File('/some/local/dir/myfile.txt'))
// modify your object here and save.
mycertificate.certificate = 'myfile.txt'
}
关于grails - 如果在 View 上提供了值,则更新一个字段,否则保留先前的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11697156/