Struts was first encountered on the Java Web development road. It elegantly implemented MVC and matched her own labels, so that people could not ask for anything for a while. As people develop their skills and get involved in the development of "large" systems, people complain about struts. Action class, actionform class more and more, struts-config.xml more and more big, demand changes and system adjustment will frequently change forward and JSP file ing, developers complain about changing the actionform class and attributes in the JSP form. For debugging purposes, the server restarts frequently. Most often mentioned by developers is the issue of formbean data binding. The database table structure has changed and the actionform class has to change. To this end, the dynaactionform class is added to struts1.1, which enables dynamic data binding by configuring an XML file. However, changes in the database table structure and XML files will inevitably lead to a restart of the server, and modification of XML files is still troublesome. Can you find a way to dynamically bind data without modifying the configuration file? The answer is yes.
Struts was first encountered on the Java Web development road. It elegantly implemented MVC and matched her own labels, so that people could not ask for anything for a while.
As people develop their skills and get involved in the development of "large" systems, people complain about struts. Action class, actionform class more and more, struts-config.xml more and more big, demand changes and system adjustment will frequently change forward and JSP file ing, developers complain about changing the actionform class and attributes in the JSP form. For debugging purposes, the server restarts frequently.
Most often mentioned by developers is the issue of formbean data binding. The database table structure has changed and the actionform class has to change. To this end, the dynaactionform class is added to struts1.1, which enables dynamic data binding by configuring an XML file. However, changes in the database table structure and XML files will inevitably lead to a restart of the server, and modification of XML files is still troublesome.
Can you find a way to dynamically bind data without modifying the configuration file? The answer is yes.
Let's first look at the definition of dynaactionform:
Public class dynaactionform extends actionform implements org. Apache. commons. beanutils. dynabean {...}
The implemented interface org. Apache. commons. beanutils. dynabean has a public dynaclass getdynaclass (). This method returns dynaactionformclass,
Dynaactionformclass is defined as follows:
Public class dynaactionformclass implements org. Apache. commons. beanutils. dynaclass, serializable {...}
The Org. Apache. commons. beanutils. dynaclass interface has a method: Public dynabean newinstance (). The method implemented by dynaactionformclass returns
Dynaactionform creates an instance.
You can use struts labels such as <HTML: text/> to display the data in formbean on a static page. This process must call the org. Apache. commons. beanutils. dynaclass interface.
Public dynaproperty getdynaproperty (string name) method, where the public dynaproperty getdynaproperty (string name) method of dynaactionformclass is called,
This method is implemented as follows:
Public dynaproperty getdynaproperty (string name ){
If (name = NULL ){
Throw new illegalargumentexception
("No property name specified ");
}
Return (dynaproperty) propertiesmap. Get (name ));
}
In the method, propertiesmap is of the hashmap type and contains the attribute information defined in the XML Attribute file of the dynaactionform class. If the property name represented by name has not been defined in the XML property file, a blank value is returned. The real binding issue is not about binding, but about data extraction. Finally, an exception is thrown. <HTML: Form/> In the tag, if the formbean attribute name represented by the <HTML: text/> tag property value is not defined in the XML Attribute file of formbean, an exception is thrown.
However, this problem can be solved.
We define two classes to implement the org. Apache. commons. beanutils. dynaclass and org. Apache. commons. beanutils. dynabean interfaces respectively, as shown below:
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) propertiesmap. Get (name );
If (dynaproperty = NULL ){
// If the attribute named by name does not exist, it is instantiated.
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 );
}
......
}
In this way, in the struts action configuration, maydynaactionform is used as the formbean class name to dynamically bind data, and Struts labels such as <HTML: text/> can be fully utilized.
If the property value of a tag exists in formbean, its data is displayed. If the property value does not exist, it is displayed as a null value.
"Attributes exist in formbean" indicates that the attributes exist in the array of formbean. getdynaclass (). getdynaproperties () attributes. How is this array generated,
Developers can design according to the role of formbean.
Newxy technology the dynamic formbean class implements the org. Apache. commons. beanutils. dynabean interface and the attribute array obtained by formbean. getdynaclass (). getdynaproperties ().
It consists of the result field name and its type information obtained from the query database.
Newxy Technology Net. newxy. Bean. beandynaclass class implements the org. Apache. commons. beanutils. dynaclass interface,
The public dynaproperty getdynaproperty (string name) method is as follows:
Public dynaproperty getdynaproperty (string name ){
If (name = NULL ){
Throw new illegalargumentexception ("property name is missing .");
}
Dynaproperty = (dynaproperty) propertiesmap. Get (name );
If (dynaproperty = NULL ){
// If the attribute named by name does not exist, it is instantiated.
Dynaproperty = new dynaproperty (name );
}
Return dynaproperty;
}
Newxy technology's DAO class net. newxy. DBM. basedao has a method object findbysql (string SQL) throws exception, the returned is
Net. newxy. struts_faces.dynaformbean type, which stores the queried records in the _ Coll attribute. The object findbysql (string SQL) method is finally called.
Org. Apache. commons. beanutils. rowsetdynaclass introspect (resultset) and copy (resultset) methods. These two Methods Extract the query result field type and place the query result in
Net. newxy. struts_faces.dynaformbean in the _ Coll attribute.
Newxy technology allows you to dynamically bind data without using XML Attribute files. This process is inseparable from the methods in the requestutils class of struts runtime.
Newxy technology can directly use its own <nhtml: Action1/> label to insert, delete, update, and query data without configuring struts actions, however, you can also disable specific action classes when configuring actions,
The net. newxy. struts_faces.dispatchaction class inherits the org. Apache. Struts. Actions. dispatchaction of Struts. It contains update, find, remove, edit, and other methods,
Data can be inserted, deleted, updated, and queried.
When newxy technology is used, whether newxy's <nhtml: Form/> or struts's <HTML: Form/> are used on the JSP page, and the backend uses the action class, or use the newxy label class,
After data is submitted, several methods of the struts tool class requestutils are called to bind data to formbean.
It can be seen that although newxy technology can achieve dynamic data binding without XML Attribute configuration files, it is inseparable from struts.
Newxy technology site: http://www.newxy.net
Newxy Technical Documentation: http://www.newxy.net/doc.jsp
Newxy technical documentation and Development Kit download: http://www.newxy.net/zh_cn/download/index.jsp
Newxy Technology example: http://www.newxy.net/zh_cn/samples/index.jsp