【问题标题】:How to append to the URL Path Grails如何附加到 URL 路径 Grails
【发布时间】:2014-04-25 20:17:53
【问题描述】:

我正在使用 Grails 1.3.7,并且正在尝试弄清楚如何在我的 URL 路径中附加州(地区)缩写(NM、AZ、UT)。所以它看起来像这样:

localhost/NM/action/

localhost/UT/action2/3/

必须对网站上的所有网址执行此操作。我也不想更改 URLMappings.groovy 文件,过滤器对此有帮助吗?

我创建了这个过滤器定义:

def filters = {
    ...
    regionAppender(uri:'/**') {
        before = {
            if (request.region) {
                if (!request.forwardURI.contains(request.region)) {
                    String[] split = s.split("/");
                    String redirectUri = split[0] + "/" + request.region
                    if (split.length > 1) {
                        for (int i = 1; i < split.length; i++){
                            redirectUri = redirectUri + "/" + split[i]
                        }
                    }
                    redirect(uri: redirectUri)
                    return false
                }
                return true
            }
            return true
            }
    }
}

但它似乎不起作用。有没有办法做到这一点,还是我已经更改了我所有的 URLMappings?

【问题讨论】:

  • 如何从请求中获得 NM、UT 等?请求的区域设置在哪里?
  • 对不起,响应迟了,request.region 是由另一个过滤器定义设置的,它放在 regionAppender 之前。

标签: url grails servlet-filters url-mapping


【解决方案1】:

我没有对此进行测试,但可能与您的请求声明有关...尝试 params.region 代替

Take a look here

def filters = {
    ....
            before = {
                if(params?.id) {

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多