Struts DynaActionForm

In this tutorial you will learn how to create Struts DynaActionForm.

Ads

Tutorials   
Struts 2.5.10.1 General Availability - released Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 version 2.3.16.3 released Struts 2 version 2.3.15.3 released Struts 2 Hello World Annotation Example Struts Roseindia Struts 2 Tutorial: Easy steps to learn Struts 2 Difference between Struts and Spring Apache Struts 2.3.15.2 launched to fix vulnerable Feature Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts 2.3.15.1 Maven - How to use Maven dependency of Struts 2.3.15.1? Download Struts 2.3.15.1 How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts 2.3.15.1 Struts 2 Tutorials - Struts version 2.3.15.1 Struts 1 Tutorial and example programs Struts Tutorial What is Struts Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts Video Tutorials Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 In Struts What is Model? What is Struts? Download Struts What is Struts Framework? Struts Framework Latest Version of Struts Framework Downloading Struts Official example Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Hibernate4 Struts Integration Struts2 Tiles Example
Ads

1Struts DynaActionForm

     

In this tutorial you will learn how to create Struts DynaActionForm. We will recreate our address form with Struts DynaActionForm. DynaActionForm is specialized subclass of ActionForm that allows the creation of form beans with dynamic sets of properties, without requiring the developer to create a Java class for each type of form bean. DynaActionForm eliminates the need of FormBean class and now the form bean definition can be written into the struts-config.xml file. So, it makes the FormBean declarative and this helps the programmer to reduce the development time.

In this tutorial we will recreate the add form with the help of DynaActionForm. It also shows you how you can validate use input in the action class. 

Adding DynaActionForm Entry in struts-config.xml

First we will add the necessary entry in the struts-config.xml file.  Add the following entry in the struts-config.xml file. The form bean is of org.apache.struts.action.DynaActionForm type. The <form-property/> tag is used to define the property for the form bean. We have defined three properties for our dynamic form bean.

<form-bean name="DynaAddressForm" 
   type="org.apache.struts.action.DynaActionForm">
   <form-property name="name" type="java.lang.String"/>
   <form-property name="address" type="java.lang.String"/>
   <form-property name="email" type="java.lang.String" />
</form-bean>

Adding action mapping

Add the following action mapping in the struts-config.xml file:

<action path="/DynaAddress" type="roseindia.net.AddressDynaAction"
   name="DynaAddressForm"
   scope="request"
   validate="true"
   input="/pages/DynaAddress.jsp">

  <forward name="success" path="/pages/success.jsp"/>
  <forward name="invalid" path="/pages/DynaAddress.jsp" />

</action>

Creating Action Class

Code for action class is as follows:

 package roseindia.net;                        
/**
@author Deepak Kumar
* @Web http://www.roseindia.net
* @Email [email protected]
*/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;
public class AddressDynaAction extends Action
{
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse responsethrows Exception{
  DynaActionForm addressForm = (DynaActionForm)form;
 //Create object of ActionMesssages
  ActionMessages errors = new ActionMessages();
  //Check and collect errors
  if(((String)addressForm.get("name")).equals("")) {
 errors.add("name",new ActionMessage("error.name.required"));
  }
  if(((String)addressForm.get("address")).equals("")) {
 errors.add("address",new ActionMessage("error.address.required"));
  }
  if(((String)addressForm.get("email")).equals("")) {
 errors.add("email",new ActionMessage("error.emailaddress.required"));
  }
  //Saves the error
  saveErrors(request,errors);
  //Forward the page
  if(errors.isEmpty()){
  return mapping.findForward("success");
  }else{
  return mapping.findForward("invalid");
  }
  }

Creating the JSP file

We will use the Dyna Form DynaAddressForm created above in the jsp file. Here is the code of the jsp(DynaAddress.jsp) file.

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

<html:html locale="true">
<head>
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/DynaAddress" method="post">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</tr>

<tr>
<td align="left" colspan="2">
<font color="red"><html:errors/></font>
</tr>



<tr>
<td align="right">
Name
</td>
<td align="left">
<html:text property="name" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
Address
</td>
<td align="left">
<html:text property="address" size="30" maxlength="30"/>
</td>
</tr>

<tr>
<td align="right">
E-mail address
</td>
<td align="left">
<html:text property="email" size="30" maxlength="30"/>
</td>
</tr>

<tr>
<td align="right">
<html:submit>Save</html:submit>
</td>
<td align="left">
<html:cancel>Cancel</html:cancel>
</td>
</tr>
</table>


</html:form>
</body>
</html:html>

Add the following line in the index.jsp to call the form.

<li>
<html:link page="/pages/DynaAddress.jsp">Dyna Action Form Example</html:link>
<br>
Example shows you how to use DynaActionForm.
</li>

Building Example and Testing

To build and deploy the application go to Struts\strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the DynaAddress.jsp page. Without entering anything in the form and submitting the submit button, your browser should show the following out put.

Advertisements

Share on Google+Share on Google+

Struts DynaActionForm

Posted on: February 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Struts DynaActionForm   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
laxminarsaiah
September 20, 2011
Am not able to do validations with DynaActionForm

please help me in doing validations using DynaActionForm. thank u sir

Ads

 

Ads