grails提供了两种对请求的拦截机制,一是在Controller中定义beforeInterceptor和afterInterceptor两个方法,分别在执行action之前和之后调用,具体实现代码是在SimpleGrailsController和SimpleGrailsControllerHelper中,不再赘述。
还有一个是Filter,即在grails-app/conf目录下定义以Filters结尾的groovy文件既可,提供了三个拦截方法before,after,afterView,分别对应Spring MVC的HandlerInterceptor接口的preHandle(在处理请求前调用),postHandle(在处理请求后生成View之前调用,提供了修改Model的机会),afterCompletion(在请求处理完毕生成View之后调用)。
看看源码即可发现其实grails的MVC即是在Spring MVC框架上包装一层而成的,所以看来暂时不可能把核心框架修改为struts的可能。
下面看看grails是如何把Filters结尾的groovy文件变成Spring的HandlerInterceptor。
DefaultGrailsPlugin加载所有的plugin,当然也包含FiltersGrailsPlugin
DefaultGrailsPlugin.groovy
//加载plugin,调用plugin的doWithApplicationContext方法
public void doWithRuntimeConfiguration(
RuntimeSpringConfiguration springConfig) {
if(this.pluginBean.isReadableProperty(DO_WITH_SPRING)) {
if(LOG.isDebugEnabled()) {
LOG.debug("Plugin " + this + " is participating in Spring configuration...");
}
//调用plugin的doWithSpring方法,加载plugin到spring的ApplicationContext,使用Spring DSL方式的BeanBuilder
Closure c = (Closure)this.plugin.getProperty(DO_WITH_SPRING);
BeanBuilder bb = new BeanBuilder(getParentCtx(), application.getClassLoader());
bb.setSpringConfig(springConfig);
Binding b = new Binding();
b.setVariable("application", application);
b.setVariable("manager", getManager());
b.setVariable("plugin", this);
b.setVariable("parentCtx", getParentCtx());
b.setVariable("resolver", getResolver());
bb.setBinding(b);
c.setDelegate(bb);
bb.invokeMethod("beans", new Object[]{c});
}
}
//动态配置plugin,调用plugin的doWithApplicationContext方法
public void doWithApplicationContext(ApplicationContext applicationContext)
FiltersGrailsPlugin.groovy
def watchedResources = "file:./grails-app/conf/**/*Filters.groovy"
//Spring bean DSL
static final BEANS = { filter ->
"${filter.fullName}Class"(MethodInvokingFactoryBean) {
targetObject = ref("grailsApplication", true)
targetMethod = "getArtefact"
arguments = [TYPE, filter.fullName]
}
"${filter.fullName}"(filter.clazz) { bean ->
bean.singleton = true
bean.autowire = "byName"
}
}
//生成了实现spring HandlerInterceptor接口的filterInterceptor
def doWithSpring = {
filterInterceptor(org.codehaus.groovy.grails.plugins.web.filters.CompositeInterceptor) {
}
for(filter in application.getArtefacts(TYPE)) {
def callable = BEANS.curry(filter)
callable.delegate = delegate
callable.call()
}
}
def doWithApplicationContext = { applicationContext ->
reloadFilters(application, applicationContext)
}
// 把Filters groovy类变成CompositeInterceptor的handlers成员
// 用FilterToHandlerAdapter适配器把preHandle==before,postHandle==after,afterCompletion==afterView对应起来
private reloadFilters(GrailsApplication application, applicationContext) {
log.info "reloadFilters"
def filterConfigs = application.getArtefacts(TYPE)
def handlers = []
for(c in filterConfigs) {
def filterClass = applicationContext.getBean("${c.fullName}Class")
def bean = applicationContext.getBean(c.fullName)
for(filterConfig in filterClass.getConfigs(bean)) {
handlers << new FilterToHandlerAdapter(filterConfig:filterConfig, configClass:bean)
}
}
if (log.isDebugEnabled()) log.debug("resulting handlers: ${handlers}")
applicationContext.getBean('filterInterceptor').handlers = handlers
}
分享到:
相关推荐
Grails入门指南书籍和源码----下载不扣分,回帖加1分,欢迎下载,童叟无欺 getting started with grails chinese Grails入门指南书籍和源码----下载不扣分,回帖加1分,欢迎下载,童叟无欺 getting started with ...
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part2 共两个包,解压后需要将扩展名.zip.bak改名为.zip重新解压。 http://dist.springsource.com/release/STS/3.8.1.RELEASE/dist/ e4.6/spring-tool-...
grails-1.0 中文参考文档 网上收集而来,对 e 文不太熟悉的朋友可以参考一下,有些翻译的不好理解,最好对照着官方的e 文的参考文档
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part1 共两个压缩包,解压后将扩展名.zip.bak改为.zip再次解压。
漏洞修补好象是传asp等后缀名的时候,生成的是nba的后缀。上传原来很简单在后缀加个空格就可以了
Grails-2.4.4-用户手册
grails 中文文档+grails-fckeditor-0.9.5.zip插件
grails-docs-2.0.0
prestige.zip,android的mvc模式,android的代码生成驱动的mvc模式。
grails-doc-1.0-beta1-中文.rar。Grails 1.0 学习资料。
grails-quartz-0.4.1 插件,直接安装
grails-3.0.5-ga (2/2), for somebody who can not download
Spring Websocket Grails插件 ... implementation "org.grails.plugins:grails-spring-websocket:2.5.0.RC1" 该插件已发布到bintray,并链接到grails/plugins以及jcenter 。 用法 该插件还可以在Grai
grails-docs-1.0,api文档。html格式
Grails开源框架 - 使用指南,版本1.0,中文文档。 Grails开源框架 - 使用指南 作者: Graeme Rocher, Marc Palmer 版本: 1.0 Copies of this document may be made for your own use and for distribution to ...
grails-reference-documentation-1.1.x.pdf
Grails Getting Started-II
grails参考文档 The Grails Framework - Reference Documentation Authors: Graeme Rocher, Marc Palmer Version: 1.0.3