Struts DynaActionForm

The DynaActionForm (org.apache.struts.action.DynaActionForm) is designed so that you can specify simple JavaBean properties through the Struts configuration file. It allows the creation of form beans, without creating a Java class for each type of form bean.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. Create DynaActionForm

First we create an entry for DynaActionForm in struts-config.xml

    <form-bean name="LoginForm"  
         <form-property name="userName" type="java.lang.String"/>
         <form-property name="password" type="java.lang.String"/>

Now can use this form in any of your action using normal tag.

2. Action Class (Controller)

Let us create an Action class that will handle the request. Here Action Class contains the business logic.

package com.kruders.action;
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.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
public class LoginAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            DynaActionForm  loginForm = (DynaActionForm ) form;
            ActionMessages errors = new ActionMessages();
            if( loginForm.get("userName") == null || loginForm.get("userName").equals("")) {
                errors.add("userName",new ActionMessage("error.userName.required"));
            if( loginForm.get("password") == null || loginForm.get("password").equals("")) {
                errors.add("password",new ActionMessage("error.password.required"));
            if(errors.isEmpty() && (!loginForm.get("userName").equals("test") || !loginForm.get("password").equals("test"))){
                errors.add("invalid",new ActionMessage("error.invalidDetails"));
            if(errors.isEmpty()) {
                return mapping.findForward("success");
            } else {
                return mapping.findForward("fail");

3. View

Create following JSP files, login.jsp and success.jsp with the following content in your WebContent folder.


<%@ taglib uri="" prefix="html"%>
<title>Login Page</title>
    <html:form action="/login" >
        User Name :<html:text name="LoginForm" property="userName" />  <html:errors property="userName" />
        Password  :<html:password name="LoginForm" property="password" /> <html:errors property="password" />
        <html:errors property="invalid" />
        <html:submit value="Login" />

The view login.jsp is to provide user to enter login details and submitting for Login. and tags are used to display the error messages to the user.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="" prefix="bean"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Success Page</title>
        Login Success. Welcome <bean:write name="LoginForm" property="userName"></bean:write>

The view success.jsp is to show success message.

4. Struts Config

Add the following code in struts-config.xml folder.

        <action input="/login.jsp" name="LoginForm" path="/login"  validate="true" scope="request" type="com.kruders.action.LoginAction">
            <forward name="success" path="/success.jsp" />
            <forward name="fail" path="/login.jsp" />
<message-resources parameter="ApplicationResources" />

5. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file


The folder structure of the example is shown below in Figure 3.1

Figure 3.1 Figure 3.1

You can download the source code of this example here.

No comments yet.

Leave a Reply