Struts DynaActionForm working example tutorial

This tutorial explains the Struts form bean DynaActionForm using a small example application.

Generals

Author:

Sascha Wolski

Sebastian Hennebrueder

http://www.laliluna.de/tutorials.html

Date: February, 8th 2005


Source code:


http://www.laliluna.de/download/struts-dynaactionform-source.zip

PDF Version des Tutorials:

http://www.laliluna.de/download/struts-dynaactionform-tutorial-en.pdf

Development Tools

Eclipse 3.x


Application Server

Jboss 3.2.5

You can also use Tomcat.

DynaActionForm class

A form bean of type Typ DynaActionForm is defined in the struts config file. The actual class is created by Struts dynamically. You define all attributes of the class in the Struts config file.

Example for a DynaActionForm definition in the Struts config file:

<form-beans >
   <form-bean name="exampleForm" type="org.apache.struts.action.DynaActionForm">
   	<form-property name="age" type="java.lang.Integer" />
      <form-property name="name" type="java.lang.String" />
   </form-bean>
</form-beans>



The form bean can be used in an Struts action. Below there is an example of an ActionMapping using our form bean.

Example:

<action attribute="exampleForm"
        name="exampleForm"
        path="/example"
        scope="request"
        type="my.package.ExampleAction" />



Validation

If you want to validate any fields of the DynaActionForm, you can do this in the action class. It is not possible to do the Validation in the form itself. The attributes of the form are accessed with get(..) and set(..) methods as shown below.

Example:

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

//Cast the DynaActionForm
DynaActionForm exampleForm = (DynaActionForm) form;

//validate the properties of the DynaActionForm
if(exampleForm.get("name").toString().length() < 3){
//..error..
}

if(Integer.parseInt(exampleForm.get("name").toString()) < 1){
//..error..
}

return mapping.findForward("showExample");
}



Initialization of properties

You can specify a default value for each property using the initial attribute in the <form-property> tag.

Example:

<form-beans >
   <form-bean name="exampleForm" type="org.apache.struts.action.DynaActionForm">
   	<form-property name="age" type="java.lang.Integer" initial="23" />
      <form-property name="name" type="java.lang.String" initial="Adam" />
   </form-bean>
</form-beans>



Using DynaActionForm Beans

We will show you now an example application with a DynaActionForm Bean.

Create the form bean (struts-config.xml)

Open the struts-config.xml and add a new form bean tag to the form beans area. Add two properties, name of type String and age of type Integer.

Specify default values:

Below you can see the example code.

<form-beans >
   <form-bean name="exampleForm" type="org.apache.struts.action.DynaActionForm">
   	<form-property name="age" type="java.lang.Integer" initial="23" />
      <form-property name="name" type="java.lang.String" initial="Adam Weisshaupt" />
   </form-bean>
</form-beans>



Create the Action class

Create the class ExampleAction in the package de.laliluna.tutorial.dynaaction.action.

The class extends the class Action.

Implement the method execute(..).

Output the name and the age to the log.

The complete source code is shown below.

public class ExampleAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

//Cast DynaActionForm
DynaActionForm exampleForm = (DynaActionForm) form;

//Access the properties of the DynaActionForm
System.out.println(exampleForm.get("name"));
System.out.println(exampleForm.get("age"));

return mapping.findForward("showExample");
}

}


Create a JSP file

Create a JSP example.jsp in the directory ../WebRoot/form/ .

Below you can see the source code of the JSP file.

<%@ page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

<html> 
	<head>
		<title>JSP for exampleForm</title>
	</head>
	<body>
		<html:form action="/example">
 <html:errors />
			Name: <html:text property="name" /> <br>
			Age: <html:text property="age" /> <br>			
			<html:submit value="Send"/>
		</html:form>
	</body>
</html>



Configure the Action (struts-config.xml)

Add an action mapping in the struts-config.xml. Add the form bean exampleForm to the action and create a forward to the example.jsp.

name specifies the action of the form bean.

Type is the path to our action class, ExampleAction.

<forward ...> is the forward to our example.jsp.

<action-mappings>
      <action
         attribute="exampleForm"
         name="exampleForm"
         path="/example"
         scope="request"
         type="de.laliluna.tutorial.dynaactionform.action.ExampleAction">

		<forward name="showExample" path="/form/example.jsp" />

	</action>
</action-mappings>



Validate the properties in the action class

We will validate if the name is longer than three characters and if the age is greater than 0. As explained before, the validation is only possible in the action class.

Below you can see the source code of the execute(..) method in the action class ExampleAction.

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

//Cast DynaActionForm
DynaActionForm exampleForm = (DynaActionForm) form;

//Access the properties of the DynaActionForm
System.out.println(exampleForm.get("name"));
System.out.println(exampleForm.get("age"));

//Create a new instance of ActionErrors
ActionErrors actionErrors = new ActionErrors();

//validate the properties of the DynaActionForm
if(exampleForm.get("name").toString().length() < 3){
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.name"));
}

if(Integer.parseInt(exampleForm.get("age").toString()) < 1){
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.age"));
}

//save the actionErrors in the request
saveErrors(request, actionErrors);

return mapping.findForward("showExample");
}



Create a Message Resource file

The Message Resource file is needed for the output of the error messages, we used in the execute method.

Create a new file named ApplicationResources.properties in the package de.laliluna.tutorial.dynaactionform.

You can find more information about message resource files in our Message Resource tutorial.
http://www.laliluna.de/articles/posts/struts-message-resources-tutorial.html



Add the following to the file.

errors.suffix=<br>
error.name=Name must have minimum 3 characters
error.age=Age must be greater then 0



Open the struts-config.xml and add the following lines.

<message-resources parameter="de.laliluna.tutorial.dynaactionform.ApplicationResources" />



Test your example

We have finished our example application. Test the example by calling

http://localhost:8080/DynaActionForm/example.do

(We expect a standardinstallation of JBOSS or Tomcat)