如何重定向到Grails应用程序中最后访问的页面? [英] How to redirect to the last visited page in Grails app?

查看:90
本文介绍了如何重定向到Grails应用程序中最后访问的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Grails的新手,我正在为很多简单的问题苦苦挣扎。

例如,我还没有设法找到回到正确的方式最后一次访问页面,当我登录/注销显示在页面顶部布局的模板视图。



我最后一次尝试解决这个问题是为了保存$ {params.controller}和$ {params.action}中的参数发送到注销操作并在其后重定向。那么......即使这个失败了。
这里是gsp片段:

 < g:link controller =useraction =logoutparams =[currentController:$ {params.controller},currentAction:$ {params.action}]>注销< / g:link> 

最后一行代码会抛出以下异常:

 错误errors.GrailsExceptionResolver  - 评估表达式的错误[[currentController:$ {params.controller},currentAction:$ {params.action}]] 
$ b $ p如何重新加载上次访问过的页面

2 - 为什么我的代码中有一个异常?



谢谢你



编辑:
关于问题#2,似乎下面的代码正在工作:

 < g:link controller =useraction =logoutparams =[currentController:params.controller,currentAction:params.action]>注销< / g:链路> 

但我不明白原因......



EDIT2:
我也找到了一个重定向到上次访问页面的解决方案:

 重定向(url:request.header('referer'))

但不幸的是, ,我的页面中呈现的内容将被复制。任何想法或任何其他安全的解决方案?

解决方案

重定向到最后一页的最简单方法是直接使用URI:

 < g:link controller =useraction =logoutparams =[targetUri :( request.forwardURI  - request.contextPath)]>退出< /克:链接> 

request.forwardURI 是完整的网址在浏览器中显示,而 request.contextPath 是表示应用上下文的URL部分,例如 http:// localhost:8080 / yourApp - 因此,从前向URI移除上下文路径的结果是应用相对URI,例如/ mycontroller / myaction )



在您的注销操作中,您只需重定向到此URI:

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

AFAIK,使用引用不是完全安全的,因为这依赖于用户代理(浏览器)追加引用HTTP头(可能已被禁用)。



关于第二个问题: Grails自动将GSP中的列表或映射属性值解释为Groovy表达式。因此,这

 < g:link controller =useraction =logoutparams =[currentController:params。控制器,currentAction:params.action]>注销< / g:link> 

相当于

 < g:link controller =useraction =logoutparams =$ {[currentController:params.controller,currentAction:params.action]}>注销< / g:link> 

并将该表达式的一部分再次包装在 $ {...}中似乎混淆了GSP编译器。



希望这有助于。


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 login/logout from a template view that is displayed on the top layout of the page.

My last attempt at solving this problem was to save the ${params.controller} and ${params.action} in the parameters sent to the logout action and redirect thereafter. Well...even this failed. Here is the gsp snippet:

<g:link controller="user" action="logout" params="[currentController: ${params.controller}, currentAction: ${params.action}]">Logout</g:link>

This last code line throws the following exception:

ERROR errors.GrailsExceptionResolver  - Error evaluating expression [[currentController: ${params.controller}, currentAction: ${params.action}]]

So my questions are:

1 - How can I reload the last visited page after a login/logout action ?

2 - Why do I have an exception from my code above?

Thank You

EDIT : Concerning question #2, it seems that the following code is working:

<g:link controller="user" action="logout" params="[currentController: params.controller, currentAction: params.action]">Logout</g:link>

But I don't really understand the reason...

EDIT2 : I have also found out a solution for redirecting to the last visited page:

redirect(url: request.header('referer'))

But unfortunately when doing this after login, the contents rendered in my page are duplicated. Any idea or any other safe solution?

解决方案

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

<g:link controller="user" action="logout" params="[targetUri: (request.forwardURI - request.contextPath)]">Logout</g:link>

(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

<g:link controller="user" action="logout" params="[currentController: params.controller, currentAction: params.action]">Logout</g:link>

is equivalent to

<g:link controller="user" action="logout" params="${[currentController: params.controller, currentAction: params.action]}">Logout</g:link>

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

Hope this helps.

这篇关于如何重定向到Grails应用程序中最后访问的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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