【问题标题】:Grails: Why are flash message only rendered after a refresh if you use ajax?Grails:如果使用 ajax,为什么只在刷新后才呈现 flash 消息?
【发布时间】:2012-01-12 18:04:32
【问题描述】:

问题是:我有一个 remoteForm,它本身工作正常。我调试了控制器,每个命令都运行了。但是,如果发生异常并且我设置了 flash.error 消息。只有在我手动按 F5(刷新)后才会显示该消息。我不明白为什么 AJAX 调用有效,但 flash 消息是空的。

我有一个远程表单(在 index.gsp 中):

       ...
         <g:formRemote name="remoteForm"
                      url="[ controller: 'document',
                       action: 'search']"
                       onLoading="showSpinner();"
                       update="searchBlock"
                       action="${createLink(controller: 'document', action:'search')}"
                       >

        <div class='info_message'>
            ${flash.message}
        </div>
        <div id="searchBlock">will be exchanged</div>
       ......

控制器:

    ......
        } catch (Exception e){
            log.error(e)
            flash.error = e.getMessage()
            flash.message="HINTTEST"
            render (template:'searchNoResult')
            return
        }
    .....

模板(_searchNoResult)有以下内容:

 <span>Sorry, your search yielded no result.</span>

  <g:if test='${flash.error}'>
        <div class='error_message'>
            Error: ${flash.error}
        </div>
    </g:if>
    <g:if test='${flash.messages}'>
        <div class='info_message'>
            Hint: ${flash.message}
        </div>
    </g:if>

文档说:flash 范围存在于当前请求和之后的调用中。如果我使用普通表单并提交一切正常,但我想使用 ajax 和渲染模板,所以我不必经常重复代码。

编辑 1 首先渲染 index.gsp。 remoteForm 使用包含 flash.messages 的模板更改了 seachBlock div。它们没有被填满。

Grails 版本 1.3.7 目前无法升级。

编辑 2 异常 e 不是我最初抛出的异常。消息为空,导致消息为空......

【问题讨论】:

  • 是错字吗?您的控制器正在填充“flash.message”,但您的模板的 if 测试是针对“flash.messages”

标签: grails groovy controller gsp


【解决方案1】:

从您显示的代码中,当 ajax 请求返回响应时,不会使用消息更新 info_message div。 formRemote 仅更新 searchBlock 元素。 如果页面不刷新,flash.message 将无法呈现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多