【问题标题】:Grails gsp won't render correctlyGrails gsp 无法正确渲染
【发布时间】:2014-08-30 23:18:10
【问题描述】:

这是我的应用程序的主登录页面:

登录后,您会看到此页面:

现在在此页面上,如果您按下“选择/上传”按钮(蓝色圆圈),您会在页面中间部分呈现一个模板,然后它看起来像这样:

如果您等待一段时间以使您的会话变为空,那么我希望“选择/上传”按钮重定向回登录页面,它确实如此,但看起来像这样:

这是与“选择/上传”按钮相关的控制器功能:

def chooseupload = {
        if (session.user == null) {
            redirect(action: customerLogin)
        }
        else {
            def batchList = (Batch.findAllWhere(userId: session.user.id.toLong(), [sort: "lastUpdate", order: "desc"]))
            render(template: 'chooseupload', model:  [batchList: batchList, batchCount: batchList.size()])
        }

    }

这是登录操作的代码:

def customerLogin = {
    } //just renders the view customerLogin.gsp

非常感谢任何建议。如果需要,我很乐意提供更多相关代码。

【问题讨论】:

    标签: grails


    【解决方案1】:

    您似乎在 AJAX 请求中调用 chooseupload。如果您在控制器中调用重定向,浏览器将返回完全装饰(带有页眉和页脚)的页面。为了能够区分 AJAX/noAJAX 调用,我使用以下代码:

    request.xhr ? render( template:'customerLogin' ) : redirect( action:'customerLogin' )
    

    【讨论】:

    • 你完全正确。我不知道为什么我什至不认为我在进行 AJAX 调用。对于您的代码将替换我的代码的哪一部分,我仍然有点困惑。你说我必须在控制器中重定向,我理解这一点。 customerLogin.gsp 是一个完整的视图而不是模板,这就是我对你的代码感到困惑的原因。你能解释一下吗?而且我的上述两种方法都在控制器中,所以我没有按照我应该的方式在控制器中重定向吗?
    • 您应该将redirect(action: customerLogin) 行替换为我发布的代码。我的意思是,您呈现(或重定向到)的页面通过 Grails 的 sitemesh 过滤器获得 decorated。请参阅 grails ref-doc,那里解释得很好
    • 使用您的解决方案,当我在会话为空时单击“选择/上传”按钮时,我现在得到了这个:| Error 2014-08-31 17:28:34,101 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - ControllerExecutionException occurred when processing request: [POST] /FatcaOne_0/customer/chooseupload Unable to load template for uri [/customer/_customerLogin.gsp]. Template not found.. Stacktrace follows: Message: Unable to load template for uri [/customer/_customerLogin.gsp]. Template not found. Line | Method ->> 71 | doCall in com.twc.fatcaone.CustomerController$_closure5$$EOoXEy12
    • 使用render( view:'customerLogin' )
    • 这是我现在使用的代码行request.xhr ? render( template:'customerLogin' ) : render( view:'customerLogin' ),我把它留了一夜,所以会话可能会被取消,当我今天早上点击选择/上传按钮时,这就是我得到的:@ 987654329@
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2011-03-20
    • 1970-01-01
    • 2019-07-21
    • 2015-03-10
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多