简体   繁体   English

显示错误消息Grails-引导程序

[英]Showing error message Grails - Bootstrap

From my grails Controller class i need to show an error message from the GSP file. 从我的grails Controller类中,我需要显示GSP文件中的错误消息。

def myControllerMethod() {

    if (fruit==apple){
       // Do something
    } else {
       Show the error message, from the GSP file. 
    }

}

In the GSP file. 在GSP文件中。 I have the following code which is the error message i want to show for 5 seconds and then it should disappear. 我有以下代码,这是我要显示5秒钟的错误消息,然后它应该消失。

<body>
...
<div class="alert alert-danger" role="alert">
  <a href="#" class="alert-link">...</a>
</div>

</body>

You need Javascript / JQuery for that. 为此,您需要Javascript / JQuery。 Below an example: 下面是一个例子:

setTimeout( function(){ 
    $('div.alert').fadeOut("slow"); }, 
5000 );

More information with setTimeout here 有关setTimeout的更多信息,请点击此处

You can also use delay() (more information here ) 您还可以使用delay()(更多信息在此处

Example: 例:

def myController() {
    def message
    if (fruit==apple){
       // Do something
    } else {
       message = 'Your message' 
    }
    return [message:message, ...]
}

In your view: 您认为:

<g:if test="${ message }">
 ...
</g:if>

And don't forget to enable alert: 并且不要忘记启用警报:

$(".alert").alert()

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM