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. Linux运维七:网络基础

    1:网线 2:交换机,路由器 交换机(Switch)意为“开关”是一种用于电(光)信号转发的网络设备.它可以为接入交换机的任意两个网络节点提供独享的电信号通路.最常见的交换机是以太网交换机.其他常见的 ...

  2. 数据科学的完整学习路径—Python版(转载)

    时间 2015-01-29 14:14:11  数盟原文  http://dataunion.org/?p=9805 译者: Allen 从Python菜鸟到Python Kaggler的旅程(译注: ...

  3. google analysis教程

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  4. Java 守护线程概述

    原文出处: 朱小厮 Java的线程分为两种:User Thread(用户线程).DaemonThread(守护线程). 只要当前JVM实例中尚存任何一个非守护线程没有结束,守护线程就全部工作:只有当最 ...

  5. Windows下配置Nginx+php7

    第一部分:准备工作 第二部分:安装nginx 第三部分:安装php(这里主要讲nginx配置启动php,以cgi运行php)nginx配置文件是conf文件夹里的nginx.conf 在这里,我简单说 ...

  6. 一、Kafka初认识

    一.kafka使用背景 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候,是否会遇到这样的一些问题: 我们想分析下用户行为(pageviews),以便我们设计出更好的广告位 我 ...

  7. 2016-2017-20155329 《Java程序设计》第5周学习总结

    学号 2016-2017-20155329 <Java程序设计>第5周学习总结 教材学习内容总结 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生时显示友好的错误 ...

  8. 【转】 jquery easyui Tab 引入页面的问题

    原地址:http://blog.csdn.net/superdog007/article/details/8225518 jQuery Easyui 的tabs插件有两种方式加载某个tab(标签页)上 ...

  9. HDU 3449 Consumer (背包问题之有依赖背包)

    题目链接 Problem Description FJ is going to do some shopping, and before that, he needs some boxes to ca ...

  10. backup服务器之rsync服务

    backup服务器之rsync服务   rsync是开源的.快速的.多功能的可实现全量及增量的本地或远程数据同步备份的优秀工具.它拥有scp.cp的全量复制功能,同时比scp.cp命令更优秀.更强大. ...