grails - 显示flash消息 [英] grails - display flash message

查看:89
本文介绍了grails - 显示flash消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个显示对象列表。如果在删除对象时存在 DataIntegrityViolation ,我想显示一条消息。我在做的是:

  def delete(){

def instanceToDelete = Myobject.get (params.id)
try {
instanceToDelete.delete(flush:true)
redirect(action:list,id:params.id)
}
catch(DataIntegrityViolationException e){
flash.message =some message
//我想刷新包含flash.message的div
}
}

以下是应该显示的Flash消息:

 < g:if test =$ {flash.message}> 
< div class =alert alert-errorstyle =display:block> $ {flash.message}< / div>



对不起 - 我知道这是一个愚蠢的问题,但我真的找不到解决方案。

flash object是存储键/值对的 Map ,因此您可以为错误消息定义自己的键。例如:

  try {
instanceToDelete.delete(flush:true)
flash.message =成功删除对象

catch(DataIntegrityViolationException e){
flash.error =无法删除对象
}
重定向(操作:list,然后,你可以检查 flash

c $ c>包含错误键的对象,并为该类消息使用不同的样式:

 < g:if test =$ {flash.error}> 
< div class =alert alert-errorstyle =display:block> $ {flash.error}< / div>
< / g:if>
< div class =messagestyle =display:block> $ {flash.message}< / div>
< / g:if>


I’m new to Grails, and I have a question that should be easy for most of you.

I have a page displaying an object list. I want to display a message if there’s a DataIntegrityViolation when an object is deleted. What I’m doing is:

def delete() {

    def instanceToDelete= Myobject.get(params.id)
    try {
        instanceToDelete.delete(flush: true)
        redirect(action: "list", id: params.id)
    }
    catch (DataIntegrityViolationException e) {
        flash.message = "some message"
        //I want to refresh the div containing the flash.message here
    }
}

Here is where the flash message should be displayed:

  <g:if test="${flash.message}">
  <div class="alert alert-error" style="display: block">${flash.message}</div>

Sorry — I know it’s a silly question, but I really can't find a solution.

解决方案

The flash object is a Map which stores key/value pairs, so you can define your own key for error messages. For example:

try {
    instanceToDelete.delete(flush: true)            
    flash.message = "successfully deleted object"
 }
 catch (DataIntegrityViolationException e) {
    flash.error = "could not delete object"            
 }
redirect(action: "list", id: params.id)

Then you can check the flash object containing the error key, and use a different style for that kind of message:

<g:if test="${flash.error}">
  <div class="alert alert-error" style="display: block">${flash.error}</div>
</g:if>
<g:if test="${flash.message}">
  <div class="message" style="display: block">${flash.message}</div>
</g:if>

这篇关于grails - 显示flash消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆