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.
Struts DynaActionForm example的更多相关文章
- struts-config.xml的配置
1.<struts-config> 元素 <struts-cofnig> 元素是 Struts 配置文件的根元素.<struts-config> 元素有 8 个子 ...
- 【Struts 动态表单】DynaActionForm
RegisterAction package k.action; import org.apache.struts.action.ActionForm; import org.apache.strut ...
- Struts核心技术简介
Struts核心技术简介 1.Struts内部机制 Struts是一种基于MVC经典设计模式的开发源代码的应用框架,它通过把Servlet.JSP.JavaBean.自定义标签和信息资源整合到一个 ...
- Struts 笔记 内部资料 请勿转载 谢谢合作
Struts 概述 随着MVC 模式的广泛使用,催生了MVC 框架的产生.在所有的MVC 框架中,出现最早,应用最广的就是Struts 框架. Struts 的起源 Struts 是Apache 软件 ...
- [转载]深入了解 Struts 1.1
转载自:http://www.ibm.com/developerworks/cn/java/l-struts1-1/ 摘要:作为基于 MVC 模式的 Web 应用最经典框架,Struts 已经正式推出 ...
- Struts框架——(三)动态ActionForm
一.DynaActionForm的引入意义 使用ActionForm把表单数据单独封装起来,而且提供了自动的数据验证,简化了代码的编写,给我们带来了极大的方便. 但是,ActionForm也存在一些明 ...
- AjaxAnywhere+struts用法
AjaxAnywhere的用法 1,简介 AjaxAnywhere被设计成能够把任何一套现存的JSP组件转换成AJAX感知组件而不需要复杂的JavaScript编码.它利用标签把Web页面简单地划分成 ...
- 菜鸟学习Struts——总结
一.原理 客户端请求到ActionSeverlet,ActionSeverlet负责截URL进行分发分发到每一个Action上,Action负责和Model打交道然后把相关信息返回到ActionSev ...
- Java Web编程的主要组件技术——Struts的高级功能
参考书籍:<J2EE开源编程精要15讲> Struts对国际化的支持 "国际化"(I18N)指一个应用程序在运行时能根据客户端请求所来的国家/地区.语言的不同显示不同的 ...
随机推荐
- shell 中的操作符
1.算术操作符 2.关系操作符 3.布尔操作符 4.字符串操作符 5.文件相关操作符 算术操作符 bash shell 没有提供任何机制来执行简单的算术运算,不过我们可以借助于一些其他程序,如 exp ...
- window10+Anaconda3-4.2+python3.5+Pycharm+清华镜像源安装
window下对python3.5适用性比较好,Anaconda4.2里面包含了python3.5. https://mirrors.tuna.tsinghua.edu.cn/anaconda/arc ...
- 【转载】14个你可能不知道的 JavaScript 调试技巧
了解你的工具可以极大的帮助你完成任务.尽管 JavaScript 的调试非常麻烦,但在掌握了技巧 (tricks) 的情况下,你依然可以用尽量少的的时间解决这些错误 (errors) 和问题 (bug ...
- gitlab的备份与恢复与迁移
一.gitlab的备份1.1 创建备份目录,并授权 1 2 3 4 [root@linux-node1 ~]# mkdir /data/backups/gitlab -p [root@linux-no ...
- 科学计算三维可视化---Mlab基础(基于Numpy数组的绘图函数)
Mlab了解 Mlab是Mayavi提供的面向脚本的api,他可以实现快速的三维可视化,Mayavi可以通过Mlab的绘图函数对Numpy数组建立可视化. 过程为: .建立数据源 .使用Filter( ...
- JavaScript 生成n位随机数
function RndNum(n){ var rnd=""; for(var i=0;i<n;i++) rnd+=Math.floor(Math.random()*10); ...
- APScheduler定时执行外加supervisor管理后台运行
最近写的天气爬虫想要让它在后台每天定时执行,一开始用的celery,但不知道为什么明明设置cron在某个时间运行,但任务却不间断的运行.无奈转用apscheduler,但是不管怎么设置都不能使得当调用 ...
- Richard Stallman:让我们关注和尊敬自由软件教父
1953年,Richard Stallman生于美国纽约曼哈顿区.在度过了并不快乐的童年之后,他在哈佛大学找到了自己的家.在MIT人工智能实验室工作期间,展露出了自己的计算 机天赋.对他来说,开发操作 ...
- 【专题】计数问题(排列组合,容斥原理,Prufer序列)
[容斥原理] 对于统计指定排列方案数的问题,一个方案是空间中的一个元素. 定义集合x是满足排列中第x个数的限定条件的方案集合,设排列长度为S,则一共S个集合. 容斥原理的本质是考虑[集合交 或 集合交 ...
- 【Loadrunner】LR参数化:利用mysql数据库里面的数据进行参数化
很多同学都在自学loadrunner去做压力测试,但是如果要利用LR做压力测试,或者是其他工具,其中有一个环节是我们避开不了的,比如说:参数化 今天华华就给大家简要的介绍下,如果你要做的参数化的数据来 ...