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