Introducing Dynamic FormBeans

As we have discussed about the various benefits of form beans and learnt how to worte and use them in the struts application for exchanging the data between the view and ccontroller. But writing a separate form bean class for each form increases the number of classes to be managed in the application even though if we resuse the form beans as explained. It is applicable only if the forms have same number of fields and of same type including field names. Therefore, as the number of distinct forms in the application increases the number of form bean classes also increases and this results to demand for more memory i.e this increases the actice memory reqirement for more memory this increases the actie memory requirement. Struts 1.1 introduced Dynamic Form Beans to solve this problem, which allows us to configure a single form bean class to represent different forms in support of this feature, org.apache.struts.action.DynaActionForm class is included in struts 1.1 API. In this case, we do not required to wite a class with the properties defined in java bean style as explained in the above section instead we need to configure directly DynaActinForm class in the form bean declaration, the following section explains the dynamic form bean configuration.

Configuring Dynamic FormBeans

To configure the dynamic form beans use the <form-bean> element nested with <form-property> tags in struts configuration file, the <form-bean> tags child elements are described .The following snippet shows the dynamic form bean declaration for representing login details equivalent to the UserForm of the preceding example.

Snippet: dynamic form bean declaration with two fields of type java.lang.String

<form-beans>
<form-bean name=”userForm” type=”org.apache.struts.action.DynaActionForm”>
<form-property name=”uname” type=”java.lang.String”/>
<form-property name=”pass” type=”java.lang.String”/>
</form-bean>
</form-beans>
As described above the DynaAction Form class can be used to configure multiple form beans like if we want to configure two form beans one to represent employee details and other to represent department details use the <form-bean> definition for two times, the following snippet shows two dynamic form bean declarations
<form-beans>
<form-bean name =”empForm” type=”org.apche.struts.action.DynaActionForm”>
<form-property name=”empno” type = “java.lang.Integer”/>
<form-property name=”ename” type=”java.lang.String”/>
<form-property name-“sal” type=”java.lang.Double”/>
<form-property name=”deptno” type=”java.lang.Integer”/>
</form-bean>
<form-bean name=”deptForm” type=”org.apche.struts.DynaActionForm”>
<form-property name=”deptno” type = “java.lang.Integer”/>
<form-property name=”dname” type=”java.lang.String”/>
<form-property name=”loc” type=”java.lang.String”/>
</form-bean>
</form-beans>

Observe in the above snippet while declaring the empForm’s empno property the type is defined as java.lang.Integer instead of int, when we want to configure a property with basic type like int, double,float,Boolean etc then we need to configure the respective wrapper type instead of primitive type. After the form bean is declared associating it with the action is same as described with the normal form beans ie use name attribute of <action> tag similar to the normal form beans dynamic form beans can be also assigned to multiple actions.

As we have learn how to configure the dynamic form beans , now we need to learn how to retrieve ad se the data from the dynamic form. To retrieve and set the data into dynamic form beans we can use the following methods defined in org.apache.struts.ation.DynaActionForm class.

Object get(String name)

The method takes the property name and returns the value associated to it if the property value is not set then returns null for non-primitive types and the respective default value in case of primitive types. If the property is not found then throws IllegalArgumentException.

Void  Set(String name,Object value)

The method sets the given value to the specified property name. If there is no property with the specified name then this throws an IllegalArgumentException. Similarly we have remove method to remove the value of property with the specified name The following code snippet shows how to get the values of the form properties in dynic form bean.

Public class AddEmpAction extends Action{
Public ActionForward execute(ActionMapping am,ActionForm af,HttpServletRequest request, HttpServletResponse response)throws Exception{
DynaActionForm daf=(DynaActionForm)af;
Int empno=(Interger)daf.get(“empno”).intValue();
String ename=(String)dat.get(“ename”);
Double sal=(Double)daf.get(“sal”).doubleValue();
…..//rest of the request handling code goes here
}//execute
}//class



Post a Comment

 
Top