Working with ActionForms and DynaActionForms
Previous | Home | Next |
ActionForms and DynaActionForms both are java bean class. ActionForm will provide properties to hold the state of the form with getters and setters to access whereas DynaActionForms ,There do not have to write setters and getters. No bean class is required for the DynaActionForm and we will declare the form beans as DynaActionForm type in struts-confing.xml.We can declare the properties and their type in the struts-config.xml.
The DynaActionForm is no compile time checking for the form fields because This is not strongly typed as the ActionForm. ActionForm were designed to act as a Firewall between HTTP and the Action classes, i.e. isolate and encapsulate the HTTP request parameters from direct use in Actions. DunaActionForm can save you a lot time to create ActionForm class, but, it has limitation and sometime you have to use a real ActionForm to do certain tasks.Directory Structure of DynaActionFormExample in Struts 1.3 Using MyEclipse IDE
index.jsp<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <title>Struts DynaActionForm example</title> </head> <body> <h3>Struts DynaActionForm example</h3> <html:form action="/myaction"> <bean:message key="label.name"/>: <html:text property="name"/><br/> <bean:message key="label.password"/>: <html:password property="password"/><br/> <br/> <html:submit><bean:message key="label.submit"/></html:submit> <html:reset><bean:message key="label.reset"/></html:reset> </html:form> </body> </html>web.xml<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>struts-config.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="name" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> </form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="dynaForm" input="/index.jsp" name="dynaForm" path="/myaction" scope="request" type="org.r4r.struts.DynaAction" cancellable="true" > <forward name="success" path="/success.jsp"></forward> </action> </action-mappings> <message-resources parameter="org.r4r.struts.ApplicationResources" /> </struts-config>DynaAction.javapackage org.r4r.struts; 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; public class DynaAction extends Action { public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{ return mapping.findForward("success"); } }ApplicationResources.properties#label message label.name = User Name label.password = Password label.submit = Submit label.reset = Resetsuccess.jsp<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <title>Struts DynaActionForm example</title> </head> <body> <h3>Struts DynaActionForm Example</h3> <h4>Welcome , <bean:write name="dynaForm" property="name" /></h4> <h4>Your password , <bean:write name="dynaForm" property="password" /></h4> </body> </html>Output
Previous | Home | Next |