How to redirect to the last visited page in Grails app?

后端 未结 4 586
猫巷女王i
猫巷女王i 2021-01-31 09:40

I am a newbie in Grails and I am struggling with many simple issues.

For instance, I haven\'t managed to find a proper way to go back to the last visited page when I log

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 09:59

    The easiest way to redirect to the last page, is to use the URI directly:

    Logout
    

    (request.forwardURI is the complete URL as displayed in the browser, while request.contextPath is the URL part representing the app context, eg. "http://localhost:8080/yourApp" - thus, the result of removing the context path from the forward URI is the app-relative URI, eg. "/mycontroller/myaction")

    In your logout action simply redirect to this URI:

    def targetUri = params.targetUri ?: "/"
    redirect(uri: targetUri)
    

    AFAIK, using the referrer is not entirely safe, because this relies on the user agent (browser) to append the referrer HTTP header (which may have been disabled).

    As to your 2nd question: Grails automatically interprets list or map attribute values in GSPs as Groovy expressions. So, this

    Logout
    

    is equivalent to

    Logout
    

    and wrapping parts of this expression again in ${...} seems to confuse the GSP compiler.

    Hope this helps.

提交回复
热议问题