Grails:重定向操作 [英] Grails: Redirecting actions

查看:169
本文介绍了Grails:重定向操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对较小的应用程序.我的控制器中有2个动作,分别为action1和action2.我想要的是,如果在美国访问我的应用程序,则控制器将调用action1,而在英国访问该控制器,则将调用action2.我怎样才能做到这一点?有人可以告诉我如何在URLMappings或控制器中执行此操作吗?还是他们这样做的另一种方式?

I have a relatively small app. I have 2 actions in my controller, action1 and action2. What I want is that, if my app is accessed in US, the controller will call action1, and if it is accessed in UK, action2 is called. How can I do this? Can someone show me how to do it in URLMappings or in the controller? Or is their another way to do this?

谢谢.

一个像我这样的新手的简单示例将不胜感激:)

A simple example for a newbie like me would be greatly appreciated :)

推荐答案

您可以做的是创建一个过滤器,该过滤器将根据当前语言环境重定向到适当的控制器.

What you can do is to create a filter that will redirect to the appropriate controller depending on the current locale.

有关过滤器的更多信息.

执行以下操作:

class LocaleFilters {
    def filters = {
        checkLocale(controller: '*', action: '*') {
            before = {
                if (org.springframework.web.servlet.support.RequestContextUtils.getLocale(request) == Locale.US ) {
                    redirect(action: 'action1')
                    return false
                } else {
                    redirect(action: 'action2')
                    return false
                }
            }
        }
    }
}

这篇关于Grails:重定向操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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