Struts DynaActionForm example
来源:互联网 发布:危机公关 知乎 编辑:程序博客网 时间:2024/05/16 18:17
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 in Action 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 的DynaActionForm
- struts DynaActionForm 使用心得
- struts DynaActionForm讲解
- Struts中的DynaActionForm
- Struts中使用 DynaActionForm
- struts中的动态form----DynaActionForm
- Struts的动态ActionForm(DynaActionForm)
- struts中DynaActionForm的使用
- struts,DynaActionForm,数组,文件上传
- DynaActionForm
- struts中的DynaActionForm类和ActionForm类
- struts之DynaActionform动态表单创建
- struts中有关DynaActionForm的注意事项
- struts的基本知识点—DynaActionForm
- struts高级,指令牌,上传组件,forwardaction,DispatchAction,DynaActionForm
- Struts 1.x | 通过DynaActionForm获取页面上的值
- Struts Hello World Example
- hdu1166-敌兵布阵(线段树)
- hdu5047 Sawtooth(推导+java大数)
- Spring MVC入门案例(2)
- 宽度决定高度
- Spring事务配置的五种方式
- Struts DynaActionForm example
- LeetCode题解:Climbing Stairs
- 对select()参数fdset的完整理解 http://blog.csdn.net/maray/article/details/8285775
- Redis管道(Pipelining)操作
- ZOJ 1649 Rescue
- android handler 内存泄露
- Session_Flow
- jquery使用cdn加载问题
- iOS多线程之NSoperation和GCD的比较