How to define multiple distinct controllers in Grails 2 filter?

让人想犯罪 __ 提交于 2019-11-28 02:38:00

问题


Is it possible to define multiple distinct controller in a Grails 2 web application filter? For example, something like:

def filters = {
    someFilterMethod(controller: 'controller1 controller2 ...', action: '*') {
        ...
    }
}

Otherwise, is there a way to specify to not include the main index.gsp in the filter?


回答1:


Use the pipe symbol:

def filters = {
   someFilterMethod(controller: 'controller1|controller2|...', action: '*') {
      ...
   }
}



回答2:


If you can define a rule that matches index.gsp, then you can define a rule that matches everything but index.gsp by adding invert: true. I guess something like this should do it:

def filters = {
    someFilterMethod(uri: '/', invert: 'true') {

    }
}

It seems like the following should also work:

def filters = {
    someFilterMethod(uriExclude: '/') {

    }
}

You can provide a regex instead of a literal path, so if you also need to exclude '/index' as well, then you just need to replace '/' with a regex that matches '/' and '/index'. My regex skills are rusty, but something like this should do it:

def filters = {
    someFilterMethod(uriExclude: '/(index)?', regex: true) {

    }
}

Warning

I haven't tested any of the code above, so caveat emptor!




回答3:


You could include logic within the filter like

if (controllerName == "controller1" || controllerName == "controller2") {
 ...
}


来源:https://stackoverflow.com/questions/8941249/how-to-define-multiple-distinct-controllers-in-grails-2-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!