Struts DynaActionForm example
The Struts DynaActionForm
class is an interesting feature to let you create a form bean dynamically and declaratively. It enables you to create a “virtual” form bean in Struts configuration file instead of create a real Java form bean class. It can avoid you to create many simple but tedious form bean classes.
For example, a DynaActionForm
contains a “username
” property.
<form-bean name="dynaUserForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="username" type="java.lang.String"/>
</form-bean>
The different between “DynaActionForm
” and “ActionForm
”
DynaActionForm
is not required to create a real Java class (just declare in Struts config file), butActionForm
does.- In
DynaActionForm
, form validation is implement inAction
class, whileActionForm
is implement inside its own class.
DynaActionForm example
The Struts <html:text>
textbox example will be refactor to use the “DynaActionForm
” instead of normal “ActionForm
”.
1. struts-config.xml
Declare the “DynaActionForm
” in Struts configuration file and link it to the Action class like normal.
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<!--<form-bean
name="userForm"
type="com.mkyong.common.form.UserForm"/>
-->
<form-bean name="dynaUserForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="username" type="java.lang.String"/>
</form-bean>
</form-beans>
<action-mappings>
<action
path="/LoginPage"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/login.jsp"/>
<action
path="/Login"
type="com.mkyong.common.action.UserAction"
name="dynaUserForm"
>
<forward name="success" path="/pages/welcome.jsp"/>
<forward name="failed" path="/pages/login.jsp"/>
</action>
</action-mappings>
<message-resources
parameter="com.mkyong.common.properties.Common" />
</struts-config>
2. Action
Move all the form validation method to Action class, and get the “DynaActionForm
” property via the “get()
” method.
UserAction.java
package com.mkyong.common.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 UserAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
DynaActionForm userForm = (DynaActionForm)form;
ActionMessages errors = new ActionMessages();
//do the form validation in action class
if( userForm.get("username") == null ||
("".equals(userForm.get("username")))) {
errors.add("common.name.err",
new ActionMessage("error.common.name.required"));
}
saveErrors(request,errors);
if(errors.isEmpty()){
return mapping.findForward("success");
}else{
return mapping.findForward("failed");
}
}
}
Conclusion
Should you go for DynaActionForm
? This feature 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. In large project environment, maintenance always is the 1st priority to consider, you have to create a “Form standard” to follow, it’s not practical to mix use of both, unless you have a very solid reason to support. Personally, i seldom use the DynaActionForm
, with Eclipse IDE, the ActionForm
is not so hard to create after all.