Grails UrlMapping重定向以保持DRY [英] Grails UrlMapping Redirect to keep DRY

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

问题描述

我正在使用 Grails 2.1.1 ,并希望添加一些映射到控制器操作的自定义URL。

我可以这样做,但原始映射仍然有效。



例如,我在中创建了一个映射 add-property-to-directory UrlMappings 如下:

  class UrlMappings {

static mappings = {
/ add-property-to-directory(controller:property,action:create)
/ $ controller / $ action?/ $ id?{
constraints {
//在这里应用约束
}
}

/(view:/ index)
500(view:' / error')
}
}

现在,我可以点击 / mysite / add-property-to-directory ,它会执行 PropertyController.create ,如我所料。然而,我仍然可以打到 / mysite / property / create ,它会执行相同的 PropertyController.create 方法。

本着DRY的精神,我想做一个301重定向从 / mysite / property / create / mysite的/附加属性到目录



我找不到在 UrlMappings.groovy 中执行此操作的方法。有没有人知道我可以在Grails中实现这一点的方法?



非常感谢!

更新



以下是我根据Tom的回答实施的解决方案:



UrlMappings.groovy

  class UrlMappings {

static mappings = {

/ add-property-to目录(控制器:属性,操作:创建)
/ property / create{
controller =redirect
destination =/ add-property-to-目录
}


/ $ controller / $ action?/ $ id?{
constraints {
// apply constraints here


$ b/(view:/ index)
500(view:'/ error')
}
}

RedirectController.groovy

  class RedirectController {

def index(){
redirect(url:params.de stination,permanent:true)
}
}


解决方案可以实现这一点:

 / $ controller / $ action?/ $ id? (
controller:'myRedirectControlller',action:'myRedirectAction',params:[controller:$ controller,action:$ action,id:$ id]


/ user / list(controller:'user',action:'list')

你可以在params中得到normallny的值:

  log.trace'myRedirectController.myRedirectAction:'+ params.controller +','+ params.action +','+ params.id 


I am working with Grails 2.1.1 and would like to add a handful of customized URLs that map to Controller Actions.

I can do that, but the original mapping still works.

For example, I created a mapping add-property-to-directory in my UrlMappings as follows:

class UrlMappings {

    static mappings = {
        "/add-property-to-directory"(controller: "property", action: "create")
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "500"(view:'/error')
    }
}

Now, I can hit /mysite/add-property-to-directory and it will execute PropertyController.create, as I would expect.

However, I can still hit /mysite/property/create, and it will execute the same PropertyController.create method.

In the spirit of DRY, I would like to do a 301 Redirect from /mysite/property/create to /mysite/add-property-to-directory.

I could not find a way to do this in UrlMappings.groovy. Does anyone know of a way I can accomplish this in Grails?

Thank you very much!

UPDATE

Here is the solution that I implemented, based on Tom's answer:

UrlMappings.groovy

class UrlMappings {

    static mappings = {

        "/add-property-to-directory"(controller: "property", action: "create")
        "/property/create" {
            controller = "redirect"
            destination = "/add-property-to-directory"
        }


        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "500"(view:'/error')
    }
}

RedirectController.groovy

class RedirectController {

    def index() {
        redirect(url: params.destination, permanent: true)
    }
}

解决方案

It is possible to achieve this:

"/$controller/$action?/$id?" (
    controller: 'myRedirectControlller', action: 'myRedirectAction', params:[ controller: $controller, action: $action, id: $id ]
)

"/user/list" ( controller:'user', action:'list' )

and in the action you get the values normallny in params:

log.trace 'myRedirectController.myRedirectAction: ' + params.controller + ', ' + params.action + ', ' + params.id

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

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