-1

嗨,我是 Grails 的新手,我已经实现了删除收件箱中的消息的删除操作,但现在我想更改 flash 消息以显示“2 条消息已删除”而不是“消息 4 已删除,消息 5 已删除”,如果删除多条消息。请协助,以下是我的删除操作

定义删除(){

    def messageInstance = Message.get(params.id)

    // render error if no message found
    if (!messageInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'message.label', default: 'Message'), params.id])
        redirect(action: "phone")
    } else {
        // get the device that we are deleting from
        //specify phoneNumber so as to delete the device by phoneNumber
        def device = MessagingDevice.findByPhoneNumber(params.phoneNumber)

        // invoke deleteFromDevice on domain object, which returns true if successful
        if (messageInstance.deleteFromDevice(device)) {
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'message.label', default: 'Message'), params.id])
            redirect(action: "phone", params:[phoneNumber:params.phoneNumber])
        }
    }
}
4

3 回答 3

0

如果你查看你的default.deleted.message钥匙,messages.properties你会看到它看起来像

default.deleted.message={0} {1} deleted

只需添加一个包含所需键和值的新行。

inbox.deleted.message={0} messages deleted

并在您的控制器中使用它

flash.message = message(code: 'inbox.deleted.message', args: [deletedMessageCount])

当然,您需要deletedMessageCount在使用它之前定义一个值。

于 2013-01-07T17:00:32.720 回答
0

在代码中:

int count = 5 //number of deleted messages

flash.message = message(code: 'default.deleted.messages', args: [message(code: 'messages.label', default: 'Messages'), count])

在messages.properties中:

messages.label=Messages
default.deleted.messages={1} {0} deleted
于 2013-01-07T09:10:49.157 回答
0

你可以这样做:

    def device = MessagingDevice.findByPhoneNumber(params.phoneNumber)

    // invoke deleteFromDevice on domain object, which returns number of objects deleted
    def count = messageInstance.deleteFromDevice(device)
    if (count) {
        flash.message = message(code: 'default.deleted.message', args: [message(code: 'message.deleted.label', default: '{0} Messages deleted'), count])
        redirect(action: "phone", params:[phoneNumber:params.phoneNumber])
    }

在你的messages.properties

message.deleted.label={0} deleted
于 2013-01-07T16:05:04.540 回答