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), but ActionForm does.
  • In DynaActionForm, form validation is implement in Action class, while ActionForm 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.

Struts DynaActionForm example的更多相关文章

  1. struts-config.xml的配置

    1.<struts-config>  元素 <struts-cofnig> 元素是 Struts 配置文件的根元素.<struts-config> 元素有 8 个子 ...

  2. 【Struts 动态表单】DynaActionForm

    RegisterAction package k.action; import org.apache.struts.action.ActionForm; import org.apache.strut ...

  3. Struts核心技术简介

    Struts核心技术简介 1.Struts内部机制   Struts是一种基于MVC经典设计模式的开发源代码的应用框架,它通过把Servlet.JSP.JavaBean.自定义标签和信息资源整合到一个 ...

  4. Struts 笔记 内部资料 请勿转载 谢谢合作

    Struts 概述 随着MVC 模式的广泛使用,催生了MVC 框架的产生.在所有的MVC 框架中,出现最早,应用最广的就是Struts 框架. Struts 的起源 Struts 是Apache 软件 ...

  5. [转载]深入了解 Struts 1.1

    转载自:http://www.ibm.com/developerworks/cn/java/l-struts1-1/ 摘要:作为基于 MVC 模式的 Web 应用最经典框架,Struts 已经正式推出 ...

  6. Struts框架——(三)动态ActionForm

    一.DynaActionForm的引入意义 使用ActionForm把表单数据单独封装起来,而且提供了自动的数据验证,简化了代码的编写,给我们带来了极大的方便. 但是,ActionForm也存在一些明 ...

  7. AjaxAnywhere+struts用法

    AjaxAnywhere的用法 1,简介 AjaxAnywhere被设计成能够把任何一套现存的JSP组件转换成AJAX感知组件而不需要复杂的JavaScript编码.它利用标签把Web页面简单地划分成 ...

  8. 菜鸟学习Struts——总结

    一.原理 客户端请求到ActionSeverlet,ActionSeverlet负责截URL进行分发分发到每一个Action上,Action负责和Model打交道然后把相关信息返回到ActionSev ...

  9. Java Web编程的主要组件技术——Struts的高级功能

    参考书籍:<J2EE开源编程精要15讲> Struts对国际化的支持 "国际化"(I18N)指一个应用程序在运行时能根据客户端请求所来的国家/地区.语言的不同显示不同的 ...

随机推荐

  1. BZOJ 4827 循环卷积

    题意:求两个手环任意旋转对应位置的差值+c的平方最小 设b旋转到k最小,那么先将b扩张一倍构成一圈,那么答案式子就是 将这个式子展开一下,事情就变得有趣了起来 这个式子将a[ ]翻转可以化成卷积形式 ...

  2. DNS系统的解析原理

    根据网络通讯原理,对于Router设备是通过IP地址进行路径的Forward:当通过域名(主机名)访问远程主机时,必须将相应的主机名解析为IP地址,DNS服务器就充当了这个角色. DNS的工作原理: ...

  3. 《剑指offer》 面试题43 n个骰子的点数 (java)

    引言:写这篇文章的初衷只是想做个笔记,因为这道题代码量有点大,有点抽象,而书上并没有详细的注释.为了加深印象和便于下次复习,做个记录. 原题:把n个骰子扔到地上,所有骰子朝上一面的点数之后为s. 输入 ...

  4. spring中的控制反转

    为什么使用spring框架,控制反转是它的重要优势之一. 传统的程序设计中,某个对象需要被调用时(比如mvc模式中controller调用service),需要调用者自己创建被调用对象. 而在spri ...

  5. Java并发编程原理与实战三十六:阻塞队列&消息队列

    一.阻塞队列 1.阻塞队列BlockingQueue ---->可以理解成生产者消费者的模式---->消费者要等待到生产者生产出来产品.---->而非阻塞队列ConcurrentLi ...

  6. [转]大整数算法[11] Karatsuba乘法

    ★ 引子         前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) ...

  7. 排序算法的java实现

    冒泡.选择就不写了.很常见 一:插入排序: /** * 插入排序 */ public class P4_3 { static void insertSort(int[] a){ int j,t; /* ...

  8. 谈谈Linux内核驱动的coding style【转】

    转自:http://www.cnblogs.com/wwang/archive/2011/02/24/1960283.html 最近在向Linux内核提交一些驱动程序,在提交的过程中,发现自己的代码离 ...

  9. 25 个常用的 Linux iptables 规则【转】

    转自 25 个常用的 Linux iptables 规则 - 文章 - 伯乐在线http://blog.jobbole.com/108468/ # 1. 删除所有现有规则 iptables -F # ...

  10. bash脚本里su命令执行

    俩种方法 1.可以使用 <<EOF 参数实现. 脚本内容:cat test.sh代码如下: #!/bin/bashsu - test <<EOFpwd;exit;EOF 2.当 ...