Grails过滤器控制器封装

编程入门 行业动态 更新时间:2024-10-08 12:42:00
本文介绍了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过滤器控制器封装

本文发布于:2023-11-16 11:01:51,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   控制器   Grails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!