| 在Struts和Hibernate之间搭起桥梁 |
|
|
|
|
| 来源:www.matrix.org.cn 作者:Ted He;alilo 添加日期:2006-5-1 12:08:08 点击次数: |
|
// Evaluate property, create new instance if it is null if ( propValue == null ) { PropertyUtils.setProperty( this, propName, propClass.newInstance() ); } } } }
public void cleanupEmptyObjects() { // Get PropertyDescriptor of all bean properties PropertyDescriptor descriptors[] = PropertyUtils.getPropertyDescriptors( this );
for ( int i = 0; i < descriptors.length; i++ ) { Class propClass = descriptors[i].getPropertyType(); ClassMetadata classMetadata = HibernateUtil.getSessionFactory() .getClassMetadata( propClass );
if ( classMetadata != null ) { // This is a Hibernate object Serializable id = classMetadata.getIdentifier( this, EntityMode.POJO );
// If the object id has not been set, release the object. // Define application specific rules of not-set id here, // e.g. id == null, id == 0, etc. if ( id == null ) { String propName = descriptors[i].getName(); PropertyUtils.setProperty( this, propName, null ); }
} } } }
为了让代码可读,我们省略了Exception的处理代码。
我们的新AbstractForm类从Struts的ActionForm类继承,并且提供了通用行为:reset和cleanup多对一关联对象。当这个关系是相反的话(也就是一对多关系),那么每个例子将会有所不同,类似在Abstract类里实现是比较好的办法。
总结
Struts和Hibernate是非常流行和强大的框架,他们可以有效地相互合作,并且弥补domain模型和MVC视图(view)之间的差别。这篇文章讨论一个解决Struts/Hibernate Project的通用的方案,并且不需要大量修改已经有的代码。
感谢Jason Stell和Barbara Warren-Sica对于本文的审查和修订。
本新闻共 6页,当前在第 6页 1 2 3 4 5 6 |
|
| |