Understanding Struts DynaActionForm in Java

In this article we will understand about Struts DynaActionForm in Java

Struts DynaActionForm

Struts DynaActionForm is a feature that enables the creation of ActionForm beans, dynamically, through XML configuration. In these types of applications, Form beans are virtual instead of hard coded java ActionForm classes. This removes the tedious process of creating and managing ActionForm classes within a project. Configuring a DynaActionForm is pretty simple. Here is a simple DynaActionForm configuration snippet from the struts-config.xml

The DynaActionForm is configured under the form-beans section just as with the ActionForm configurations. The form bean becomes a DynaActionForm by having the type as org.apache.struts.action.DynaActionForm. In the above example the name of the DynaActionForm bean is dynaUserInformationForm that has two properties - username and password - both of which are of type java.lang.String.

The struts-config.xml is also configured to have action mappings like the application in the previous tutorial.

The above code shows how to use DynaActionForm. With a normal ActionForm, getters and setters will be used to access data from the Form bean. However, in the case of DynaActionForm, the bean’s get() and set() methods will have to be used. Since we do not a hard coded Form bean class, all the Form class validation will be performed in the Action class.

Benefits of using DynaActionForm:

DynaActionForms are simple to create and manage.

Declarative nature enables changes to be carried out easily.

Number of classes written can be reduced significantly in a project that requires lots of ActionForm classes.

Centralized control of ActionForm beans.

Enables clear role separation within a development team. FormBeans can be created by a page author instead of a java developer, thereby reducing delays.

Disadvantages of using DynaActionForm:

The struts-config.xml becomes unmanageable if the number of DynaActionForms created is huge.

DynaActionForm is not strongly typed as ActionForms. It means,the compilation of DynaActionForm is not done until runtime, thereby requiring multiple deployments and run cycles. This may increase the overall development time.

The time saved by not coding the ActionForms is negligible, especially with the availability of latest and improved integrated development environments.

ActionForms can be neatly organized as packages whereas DynaActionForms are flat structured, making maintenance painful at times.

DynaActionForm construction during runtime requires lots of Java Reflection, which can be avoided by using ActionForms.

Struts DispatchAction

Struts DispatchAction comes in handy when there is more than one action to be performed through a single jsp. Struts DispatchAction enables grouping of multiple functions in a single action class. This results in better maintenance and improved coding flexibility. With a normal Struts action class, all the logic needs to go into the single execute() method. No matter how many buttons the JSP has, all the actions are directed to the single execute() method. Having just one execute() method means that there will be increased conditional checks to incorporate multiple logic. Having one huge execute() method can become rather tedious. This is where DispatchAction comes to help. DispatchAction allows for multiple methods to be created within the same Action class. The distinguished methods can be invoked by means of a parameter that carries the method name.

In this tutorial, the Login Application will be extended to include DispatchAction instead of the regular Action class. In this example, the login.jsp will have two submit buttons - one for login and one for reset. Both buttons should access the Action class to perform the corresponding action.

LoginDispatchAction.java

For ease of creating and comparing, a new Action class is created with the name LoginDispatchAction.java. The UserAction.java class from the previous tutorial is left untouched.

The first major difference between the Action class for DispatchAction and regular Action is the class they inherit from. DispatchAction extends from org.apache.struts.actions.DispatchAction, while a regular Action extends from org.apache.struts.actions.Action.The LoginDispatchAction includes two methods - loginUser() and resetForm(). Both methods have the same method signature, taking 4 parameters - ActionMapping, ActionForm, HttpServletRequest, and HttpServletResponse

The method loginUser() performs the normal username and password validations. The method resetForm() clears the field values and removes the errorList from the session.

The two buttons in the login.jsp invoke two different methods in the LoginDispatchAction class based on the parameter value for the parameter “method”.

<html:submit property="method" value="resetForm"></html:submit>

Here, the resetForm button invokes the resetForm() method in the LoginDispatchAction class. The property attribute signifies the query parameter value as defined in the struts-config.xml. The only drawback here is the value attribute must be equal to method name in the DispatchAction class. The value attribute is case and space sensitive.

This drawback can be overcome by using javascript to set the parameter value. The login button uses javascript to set the parameter value.

Note here that because javascript is used to set the parameter value, the value attribute in the html:submit button can be anything and doesn’t have to match the java method name in the DispatchAction class.

My main area of specialization is Java and J2EE.
I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

To have full access to this post (or download the associated files) you must have MrBool Credits.

See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video.Subscribe for this package!