如何编写业务逻辑并保存在数据库中或返回给用户

时间:2014-03-13 04:49:42

标签: grails

我有两个问题。

1。)我需要将值保存到数据库 AFTER 对其进行一些更改。例如,如果用户参数显示出生日期= 1990-09-14,那么我想计算人的年龄并将其保存在数据库中。我该怎么做。代码如下所示。

2。)有人可以解释下面的代码:

def save() {
    def appointmentInstance = new Appointment(params)
    if (!appointmentInstance.save(flush: true)) {
        render(view: "create", model: [appointmentInstance: appointmentInstance])
        return
    }

    flash.message = message(code: 'default.created.message', args: [message(code: 'appointment.label', default: 'Appointment'), appointmentInstance.id])
    redirect(action: "show", id: appointmentInstance.id)
}

这就是我的理解:

  1. def appointmentInstance = new Appointment(params) - 它接受所有参数并将其保存到名为appointmentInstance的实例

  2. if (!appointmentInstance.save(flush: true)) {
                render(view: "create", model: [appointmentInstance: appointmentInstance])
                return
            }
    
  3. 这里有什么地方

    3。)我也知道这个例子中发生了什么:

        flash.message = message(code: 'default.created.message', args: [message(code: 'appointment.label', default: 'Appointment'), appointmentInstance.id])
        redirect(action: "show", id: appointmentInstance.id)
    

1 个答案:

答案 0 :(得分:1)

要在进行一些更改后将数据保存到数据库中,您可以执行以下操作:

def save() {
     // calculate the age 
      def age = // your calculation

 // now you can save changes to db in 2 ways  

 // 1st way
 params.age = age
 def appointmentInstance = new Appointment(params)
 if (!appointmentInstance.save(flush: true)) {
    render(view: "create", model: [appointmentInstance: appointmentInstance])
    return
 }
// 2nd way

def appointmentInstance = new Appointment(params)
appointmentInstance.age = age
if (!appointmentInstance.save(flush: true)) {
    render(view: "create", model: [appointmentInstance: appointmentInstance])
    return
}

flash.message = message(code: 'default.created.message', args: [message(code: 'appointment.label', default: 'Appointment'), appointmentInstance.id])
redirect(action: "show", id: appointmentInstance.id)

}

def appointmentInstance = new Appointment(params) 

- 它接受所有参数并将其保存到名为appointmentInstance

的实例中
if (!appointmentInstance.save(flush: true)) {
            render(view: "create", model: [appointmentInstance: appointmentInstance])
            return
        }

save()是grails的函数,可以返回true或false。 save方法通知持久化上下文应保存或更新实例。除非使用flush参数,否则不会立即保留该对象。 设置为 true 时,刷新持久性上下文,立即保留对象并更新版本列以进行乐观锁定

渲染的目的是对模型应用内置或用户定义的Groovy模板,以便共享和重用模板

如果您正在使用渲染,并且您不想执行下一行代码,则使用return语句。但是在重定向的情况下,下一行代码没有执行,所以不在那里使用return。

flash.message = message(code: 'default.created.message', args: [message(code: 'appointment.label', default: 'Appointment'), appointmentInstance.id])

Flash是一个临时存储映射,它在会话中存储下一个请求和下一个请求的对象,在下一个请求完成后自动清除保存在那里的对象。 通常用于在gsp上显示消息。

message(code: 'default.created.message', args: [message(code: 'appointment.label', default: 'Appointment'), appointmentInstance.id])

此代码使用位于i18n文件夹中的 messages.properties 文件,所有默认的grails消息都存储在此处。您还可以在此处添加自定义消息,并为消息提供动态值。

redirect(action: "show", id: appointmentInstance.id)

使用HTTP重定向将流从一个操作重定向到下一个操作。您可以在网址中将id作为参数发送。

相关问题