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”
DynaActionFormis not required to create a real Java class (just declare in Struts config file), butActionFormdoes.- In
DynaActionForm, form validation is implement inActionclass, whileActionFormis 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)指一个应用程序在运行时能根据客户端请求所来的国家/地区.语言的不同显示不同的 ...
随机推荐
- 为什么Spring Boot推荐使用logback-spring.xml来替代logback.xml来配置logback日志的问题分析
最根本的原因: 即,logback.xml加载早于application.properties,所以如果你在logback.xml使用了变量时,而恰好这个变量是写在application.proper ...
- Keepalived LVS-DR单网络双活双主配置模式
Keepalived LVS-DR单网络双活双主配置模式 Linux就该这么学 今天 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.LV ...
- golang 中的 time 包的 Ticker
真实的应用场景是:在测试收包的顺序的时候,加了个 tick 就发现丢包了 那么来看一个应用例子: package main import ( "fmt" "runtime ...
- linux下sudo命令
[userld@redhat2 root]$ sudo ls We trust you have received the usual lecture from the local System Ad ...
- 数据结构编程实验——chapter9-应用二叉树的基本概念编程
二叉树是树结构中的重要概念,一些特殊的二叉树如满二叉树和完全二叉树由于节点序号的特殊关系,在一些算法中十分常见. 这篇文章将从三个方面介绍有关二叉树的知识点: (1) 普通有序树转化为二叉树. ( ...
- Mockserver -MOCO的使用
转自: http://blog.csdn.net/shensky711/article/details/52770686
- python核心编程笔记——Chapter6
Chapter 6.序列:字符串,列表和元组 这章内容比较多啊,看得比较久,而且题目又难很多. 6.1鉴定一个字符串是否是另外一个字符串的字串,这道题目不是很确定,好像没有直接的判定吧. 直接是否内建 ...
- 51nod1312 最大异或和
题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 320 有一个正整数数组S,S中有N个元素,这些元素分别是S[0],S[1],S[2]...,S[N-1]. ...
- HDU 1263 水果 结构体排序
解题报告:一个结构体排序的题,用了一个运算符重载,要注意的是不同的地方可能会产相同的水果,一开始没注意. #include<cstdio> #include<cstring> ...
- php 创建验证码方法
php创建验证码方法: <?php function getVerify($length=4,$sessName='verify'){ //验证码 //获取字符串 去除01ol等较难辨认字符 $ ...