Grails过滤器控制器封装 [英] Grails filter controller in package

查看:61
本文介绍了Grails过滤器控制器封装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的项目目前使用各种过滤器。过滤器本身效果很好。我遇到的问题是,当指定哪个控制器应该执行过滤器时,我会得到一个非常大的列表。从功能上看,这种方式很好,但最终会变得丑陋而且有些笨拙。

  def filters = 
{
filterSomething(controller:'one | two | three | ... | xyz',action:'*')
{
//在过滤之前,不重要。






$ b

有没有办法指定过滤器是唯一的适用于给定包或者包列表中的控制器?

如果没有任何东西可用,我正在考虑将某些东西绑定到引导程序并设置我的列表你可以在所有的控制器上都有一个过滤器,然后检查他们的软件包,然后决定你想要做什么。

  class MyFilters {
def grailsApplication


def filters = {
all(controller:'*',action:'*'){
before = {
if(checkController (['com.package.name'],controllerName)){
}
}
}
}


def searchInList list(packageName)){
for(关键字在列表中){
if(packageName.contains(keyword))return true
}
return false
}

布尔检查控制器(def includePackageList,cname){
def dlist = grailsApplication.getArtefacts(Controller)
def filteredList = dlist.findAll {searchInList(includePackageList,it.getPackageName()) }
return filteredList.contains(cname)
}
}


The project I'm working on currently uses filters for various pieces. The filters themselves work great. The problem I'm running into is that when specifying which controllers the filter should be executed on, I end up with a very large list. Functionally this works fine but it ends up being ugly and somewhat unwieldy.

def filters =
{
        filterSomething(controller:'one|two|three|...|xyz', action:'*')
        {
            //before filter here, not important.
        }
    }

Is there a way to specify that the filter is only applicable for controllers in a given package or list of packages?

If there is nothing out of the box, I was thinking about tying something into the bootstrap and setting my lists that way.

解决方案

You could have a filter on all controllers and then check their package and then decide what you want to do. There are better and more elegant way coding this, but just to give you an idea.

 class MyFilters {
def grailsApplication


def filters = {
    all(controller:'*', action:'*') {
        before = {
            if (checkController(['com.package.name'],controllerName)){
            }
        }
    }
}


def searchInList(list,packageName){
    for (keyword in list) {
        if (packageName.contains(keyword)) return true
    }
    return false
}

Boolean checkController(def includePackageList,cname) {
    def dlist = grailsApplication.getArtefacts("Controller")
    def filteredList= dlist.findAll{ searchInList(includePackageList,it.getPackageName()) }
    return filteredList.contains(cname)
}
}

这篇关于Grails过滤器控制器封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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