2013-01-31 43 views
0

我有一个运行的tomcat在localost设置没有控制器的过滤器

我希望编写一个grails过滤器,以便当用户转到localhost/filter/时拦截该调用并进行一些处理。我创建了一个过滤器是conf文件夹

class TestFilters { 

    def filters = { 
     filter(uri:'/filter') { 
      before = { 
      } 
      after = {      
      } 
      afterView = {     
      } 
     } 
    }   
} 

内设置此功能后,当我去localhost/filter/我只得到404错误。

我在哪里犯错?

在此先感谢

回答

1

如果你没有FilterController网址localhost/filter没有的ressource显示 - 让您得到一个404错误。您必须修改您的UrlMappings,以便localhost/filter是有效的应用程序网址。

以下内容添加到UrlMappings.groovy

"/filter" (controller: "yourController", action:"yourAction") 

现在 - URL localhost/filteryourActionyourController和过滤器应适用。

+0

没有其他办法可以在不创建控制器的情况下做到这一点?有没有办法通过更改'web.xml'文件来做到这一点? – sriram

+0

如果你真的想忽略整个MVC模式,你可以将url映射直接链接到视图。看看[这个SO答案](http://stackoverflow.com/a/9890039/1230366)我发现... – aiolos

+0

感谢您的答案。但是有什么办法可以将过滤器路由放入'UrlMapping'中? – sriram