在java web開發道路上較早遇到的是struts,它很優雅地實現了MVC,搭配她自有的標籤,會使人們在一段時間裏不再有所他求。
隨着人們水平的提高,涉足“大型”系統的開發,人們又對struts抱怨起來。Action類,ActionForm類越來越多,struts-config.xml越來越大,需求的變化和系統的調整會頻繁地改變forward與jsp文件的影射,改變ActionForm類、改變jsp面上表單內屬性名,爲了調試,服務器頻繁重啓,開發者叫苦不迭。
最常被開發者提起的是formBean數據的綁定問題,數據庫表結構變化了,ActionForm類也要改變。爲此struts1.1增加了DynaActionForm類,通過配置xml文件實現數據的動態綁定。但是,數據庫表結構變化,xml文件也要變化,不免重起服務器,而且修改xml文件仍然是效麻煩的事。
那麼能不能找到一種方法,不需修改配置文件,就可動態綁定數據?答案是完全可以的。
先看DynaActionForm的定義:
public class DynaActionForm extends ActionForm implements org.apache.commons.beanutils.DynaBean {...}
所實現接口org.apache.commons.beanutils.DynaBean 有個方法 public DynaClass getDynaClass(),這個方法返回的是 DynaActionFormClass,
DynaActionFormClass定義如下:
public class DynaActionFormClass implements org.apache.commons.beanutils.DynaClass, Serializable {...}
org.apache.commons.beanutils.DynaClass接口有個方法:public DynaBean newInstance(),DynaActionFormClass實現的這個方法返回的就是
DynaActionForm新建實例。
通過struts <html:text/>等標籤可以將formBean中的數據顯示在靜態頁面上,這過程必須調用 org.apache.commons.beanutils.DynaClass接口裏
public DynaProperty getDynaProperty(String name)方法,在這裏就是調用DynaActionFormClass的public DynaProperty getDynaProperty(String name)方法,
這個方法實現如下:
public DynaProperty getDynaProperty(String name) {
if (name == null) {
throw new IllegalArgumentException
("No property name specified");
}
return ((DynaProperty) propertiesMap.get(name));
}
方法裏propertiesMap是HashMap類型,包含DynaActionForm類xml屬性文件裏定義的屬性信息。如果name值代表的屬性名未曾在xml屬性文件中定義過,就返回空。真正的綁定問題不是綁定問題,而是在提取數據上。最後拋出異常。<html:form />標籤內,<html:text/>等標籤property值代表的formBean屬性名如果不曾在formBean的xml屬性文件中定義,就會拋出異常。
但這問題是可以解決的。
我們自己定義兩個類,分別實現org.apache.commons.beanutils.DynaClass和org.apache.commons.beanutils.DynaBean接口,如下:
public class MyDynaActionFormClass implements org.apache.commons.beanutils.DynaClass, Serializable {
......
public DynaProperty getDynaProperty(String name) {
if (name == null) {
throw new IllegalArgumentException("Property name is missing.");
}
DynaProperty dynaProperty = (DynaProperty)propertiesMap.get(name);
if (dynaProperty == null) {
//如果以name值爲名的屬性不存在就實例化一個。
dynaProperty = new DynaProperty(name);
}
return dynaProperty;
}
........
}
public class MyDynaActionForm extends ActionForm implements org.apache.commons.beanutils.DynaBean {
......
protected MyDynaActionFormClass dynaClass = null;
......
public DynaClass getDynaClass() {
return (this.dynaClass);
}
......
}
這樣struts的action配置中以MayDynaActionForm爲formBean類名實現數據的動態綁定,可以充分利用struts <html:text />等標籤。
如果標籤property值代表的屬性存在於formBean中,就顯示其數據,如果不存在,就可以顯示爲空值。
“屬性存在於formBean”中是指屬性存在於formBean.getDynaClass().getDynaProperties()所得屬性數組中。這個數組是如何產生的,
開發者可根據formBean的作用來設計。
newxy技術動態formBean類實現了org.apache.commons.beanutils.DynaBean接口,formBean.getDynaClass().getDynaProperties()所得屬性數組
由查詢數據庫所得結果字段名及其類型信息組成。
newxy技術net.newxy.bean.BeanDynaClass類實現了org.apache.commons.beanutils.DynaClass接口,
其實現的public DynaProperty getDynaProperty(String name)方法是:
public DynaProperty getDynaProperty(String name) {
if (name == null) {
throw new IllegalArgumentException("Property name is missing.");
}
DynaProperty dynaProperty = (DynaProperty)propertiesMap.get(name);
if (dynaProperty == null) {
//如果以name值爲名的屬性不存在就實例化一個。
dynaProperty = new DynaProperty(name);
}
return dynaProperty;
}
newxy技術的DAO類net.newxy.dbm.BaseDAO有個方法Object findBySql(String sql) throws Exception,返回的是
net.newxy.struts_faces.DynaFormBean類型,它將查詢所得記錄集存放在_coll屬性中。Object findBySql(String sql)方法最終調用了
org.apache.commons.beanutils.RowSetDynaClass的introspect(resultSet),copy(resultSet)方法,這兩個方法分別提取查詢結果字段類型和將查詢結果置於
net.newxy.struts_faces.DynaFormBean的_coll屬性中。
newxy技術實現了不用xml屬性文件動態綁定數據的功能,這過程離不開struts實用類RequestUtils裏的方法。
newxy技術可以不配置struts action,直接用自身的<nhtml:action1/>標籤實現數據的插入、刪除、更新、查詢等操作,但也可以在配置action時不寫特定的Action類,
而用net.newxy.struts_faces.DispatchAction類,它繼承了struts的org.apache.struts.actions.DispatchAction,它包含update、find、remove、edit等方法,
可以實現數據的插入、刪除、更新、查詢等操作。
在利用newxy技術時,在jsp頁面上不管是用newxy的<nhtml:form/>還是用struts的<html:form/>,而後臺不管是利用Action類,還是用newxy的標籤類,
數據提交後都會調用struts的工具類RequestUtils的幾個方法來實現數據與formBean綁定。
可見,newxy技術雖然可以免xml屬性配置文件實現數據動態綁定,但她離不開struts。
newxy技術網站:http://www.newxy.net
newxy技術文檔:http://www.newxy.net/doc.jsp
newxy技術文檔及開發包下載:http://www.newxy.net/zh_cn/download/index.jsp
newxy技術範例:http://www.newxy.net/zh_cn/samples/index.jsp