从 Grails 控制器模板中的脚手架函数获取错误列表
我正在使用 Grails 1.3.7。在 Grails 项目中,我想向默认的脚手架控制器添加一个函数 ajaxupdate,该函数与 update 函数执行完全相同的工作,但返回带有列表的 JSON 数据最终的错误。
def ajaxupdate = {
String retMessage = ""
List errMessageList = []
def ${propertyName} = ${className}.get(params.id)
if (${propertyName}) {
${propertyName}.properties = params
if (!${propertyName}.hasErrors() && ${propertyName}.save(flush: true)) {
retMessage = "\${message(code: 'default.updated.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])}"
}
else {
// Add errors in errMessageList
}
}
else {
errMessageList.add("\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}")
}
render(contentType: "text/json") {
answer(
message:retMessage,
errors:errMessageList)
}
}
I'm using Grails 1.3.7. In a Grails project, I want to add a function ajaxupdate to the default scaffolded Controller, which does exactly the same job as the update function, but returns JSON data with the list of the eventual errors.
def ajaxupdate = {
String retMessage = ""
List errMessageList = []
def ${propertyName} = ${className}.get(params.id)
if (${propertyName}) {
${propertyName}.properties = params
if (!${propertyName}.hasErrors() && ${propertyName}.save(flush: true)) {
retMessage = "\${message(code: 'default.updated.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])}"
}
else {
// Add errors in errMessageList
}
}
else {
errMessageList.add("\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}")
}
render(contentType: "text/json") {
answer(
message:retMessage,
errors:errMessageList)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您询问如何获取错误列表,可以访问 错误 类似
If you're asking how to get the errors list, you can access Errors like