`

Spring结合JSF使用时的组件命名支持

阅读更多
之前和打倒小日本做项目的时候,用seam,曾经定过一条规则,Service/Manager/DAO这个层面上由于组件数量不是特别多,所以组件命名不需要空间前缀,但是对于action层面的组件,由于可能很多,所以按功能模块划分命名空间,例如:

@Compoenent("treasure.selector"),@Component("store.main")等等,在Seam的支持下,在页面上写表达式语言的时候,没有任何问题,例如#{store.main.entity.name}

后来在某环境下,不用seam,用Spring了,发现如下的代码会有问题:

@Component("auth.main")//Spring的Annotation
public class MainAction{........}

在页面上用#{auth.main.xx}这样的表达式的时候,会出现找不到对象的错误。跟代码发现Spring提供的SpringBeanFacesELResolver里面解析的时候,先解析"auth",找不到,于是后面就解析不下去了。

问题的关键点在于这个EL解析器。Seam的ELResolver能够正确解析带命名空间的组件,而Spring的这个不行。

于是,扩展之。

首先第一步是要让系统知道有哪些命名空间,这里用了Spring的一个接口,叫做BeanFactoryPostProcesser,看名字应该能看的出来,和我们常用的BeanPostProcess道理一样,只不过实现了BeanFactoryPostProcesser接口的组件会在整个beanFactory初始化之前和之后调用。

看代码:

@Component
public class BeanNamespacePostProcesser implements BeanFactoryPostProcessor {

	@Override
	public void postProcessBeanFactory(
			ConfigurableListableBeanFactory beanFactory) throws BeansException {
		String[] beanNames = beanFactory.getBeanDefinitionNames();
		for(String beanName : beanNames){
			if(beanName.indexOf('.') == -1)
				continue;
			else{
				String[] terms = beanName.split("\\.");
				Namespace space = Namespace.rootNamespace;
				for(int i = 0;i<terms.length - 1; i++){
					space = space.getOrCreateChild(terms[i]);
				}
			}	
		}
	}

}



这样,在系统启动的时候分析所有的bean的名字,把命名空间记录下来。

然后,创建一个ELResolver,让他能够解析命名空间:
public class JSFELResolver extends SpringBeanFacesELResolver {

	@Override
	public Object getValue(ELContext elContext, Object base, Object property) throws ELException {
        if (base == null) {
            return resolveBase(elContext, property);
        } else if (base instanceof Namespace) {
            return resolveInNamespace(elContext, (Namespace) base, property);
        }else {
            return null;
        }
	}

	private Object resolveInNamespace(ELContext elContext, Namespace base,
			Object property) {
		Object result = base.get((String) property);
		elContext.setPropertyResolved(true);
        return result;
	}

	private Object resolveBase(ELContext elContext, Object property) {
		if(this.getWebApplicationContext(elContext).containsBean(property.toString())){
			elContext.setPropertyResolved(true);
			return this.getWebApplicationContext(elContext).getBean(property.toString());
		}else{
			Object result = Namespace.rootNamespace.get(property.toString());
			if(result != null)
				elContext.setPropertyResolved(true);
			
			return result;
		}
	}
}



在JSF的配置文件中启用这个ELResolver:
<el-resolver>el.JSFELResolver</el-resolver>

这样,就可以在页面上用表达式引用带命名空间的组件了。
分享到:
评论

相关推荐

    JSF2.0快速入门的基本教程.ppt

    JSF将是J2EE5.0中所包含的web开发框架,这应该是第一个成为jcp标准,并且随j2eesdk一起发布的web框架,...在myeclipse 4.0GA中新建一个web项目,命名为hello,为项目增加对JSTL的支持: 在JSTL的版本中选择1.1。

    jboss as 7 support jsf2.2.1 modules

    虽然只是小版本的升级,但对于希望在JSF应用中使用HTML5技术的开发人员而言,JSF 2.2带来的更新很重要,尤其是pass through能力,它允许在JSF组件不知情的情况下传递HTML属性。 HTML5中增加了很多新特性,其中有些...

    Spring中文帮助文档

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    Spring API

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    第二部分详细讲解了jsf ri、jta、jndi、rmi、jms、javamail、ejb 3的session bean、message driven bean、jpa、jax-ws 2、jaas等java ee知识,这部分知识以jsf+ejb 3+jpa整合开发为重点,通过使用netbeans ide工具...

    Spring攻略(第二版 中文高清版).part1

    3.10 Spring中的AspectJ加载时织入aspect 140 3.10.1 问题 140 3.10.2 解决方案 141 3.10.3 工作原理 141 3.11 在Spring中配置AspectJ aspect 146 3.11.1 问题 146 3.11.2 解决方案 146 3.11.3 工作...

    Spring攻略(第二版 中文高清版).part2

    3.10 Spring中的AspectJ加载时织入aspect 140 3.10.1 问题 140 3.10.2 解决方案 141 3.10.3 工作原理 141 3.11 在Spring中配置AspectJ aspect 146 3.11.1 问题 146 3.11.2 解决方案 146 3.11.3 工作...

    JBoss Seam 工作原理、seam和hibernate的范例、RESTFul的seam、seam-gen起步、seam组件、配置组件、jsf,jboss、标签、PDF、注解等等

    Seam - 语境相关的组件[满江红20071230]............................................................................................................................ 1 Java EE 框架...........................

    J2EE应用开发详解

    259 15.3.2 Setter注入 261 15.3.3 Method注入 263 15.4 Spring AOP技术 266 15.4.1 装备(advices) 267 15.4.2 Spring AOP的传统用法 275 15.4.3 基于@AspectJ注释的AOP 277 15.4.4 基于aop命名空间的AOP 279 ...

    从Java走向Java+EE+.rar

    4.1.3 XML命名空间 31 4.2 XML能用来干什么 32 4.3 用DTD验证XML文档 33 4.4 用Schema验证XML文档 35 4.4.1 使用XML Schema 35 4.4.2 Schema的语法 37 4.5 用JAXP读写XML 44 4.6 Java EE中的JAXB 55...

Global site tag (gtag.js) - Google Analytics