DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. Struts DynaActionForm Tutorial

Struts DynaActionForm Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · Tutorial
Like (0)
Save
Tweet
Share
54.22K Views

Join the DZone community and get the full member experience.

Join For Free

DynaActionForm Beans are the extension of Form Beans that allows you to specify the form properties inside the struts configuration file instead of creating a seperate concreate class. It will become tedious to create a seperate form bean for each action class. Using DynaActionForm we can easily create Form Bean in struts-config.xml file. The struts-config.xml file entry for the DyanActionForm Bean is shown below.

<form-beans>
    <form-bean name="LoginForm" type="org.apache.struts.action.DynaActionForm">
        <form-property name="userName" type="java.lang.String" />
        <form-property name="password" type="java.lang.String" />
    </form-bean>
</form-beans>

The type attribute points to org.apache.struts.action.DynaActionForm and the <form-property> tag is used to define all the form variables. The <form-property> tag has the following three attributes.

  • name - The unique name of the property.
  • initial - The default value of the property.
  • type - Defines the Java type of the property. The available types are
    • java.math.BigDecimal
    • java.math.BigInteger
    • boolean and java.lang.Boolean
    • byte and java.lang.Byte
    • char and java.lang.Character
    • java.lang.Class
    • double and java.lang.Double
    • float and java.lang.Float
    • int and java.lang.Integer
    • long and java.lang.Long
    • short and java.lang.Short
    • java.lang.String
    • java.sql.Date
    • java.sql.Time
    • java.sql.Timestamp

Now we will see how to access the DyanActionForm in the action class.

public class LoginAction extends org.apache.struts.action.Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {
    DynaActionForm loginForm = (DynaActionForm) form;
    String userName = loginForm.get("userName").toString();
    String password = loginForm.get("password").toString();
    if(userName.equals(password))
    {
    	return mapping.findForward("success");
    }
    else
    {
    	return mapping.findForward("failure");
    }
}
}

We need to typecast the form object to DynaActionForm object in the execute method of the action class. After that we can access the Form Bean properties. We will consider a simple login application for our example. Here we will check the user name and password, if they are equal then we will forward the user to the success page, else we will forward the user to the failue page.

Now we will run the Login application. Lets enter the user name as "Eswar" and the password as "Eswar" and click the Login button.


You can download the source code of the DynaActionForm example by clicking on the Download link below.

Source: Download

Source + Lib: Download

 

Form (document) Bean (software) Spring Framework Property (programming) Data Types Download application Attribute (computing) Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Solving the Kubernetes Security Puzzle
  • Comparing Map.of() and New HashMap() in Java
  • The Path From APIs to Containers
  • How To Build a Spring Boot GraalVM Image

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: