Struts DynaActionForm example
2015-08-28 15:34
489 查看
The Struts
For example, a
The different between “
In
DynaActionFormclass 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
DynaActionFormcontains 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), but
ActionFormdoes.
In
DynaActionForm, form validation is implement in
Actionclass, while
ActionFormis 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 forDynaActionForm? This feature can save you a lot time to create
ActionFormclass, but, it has limitation and sometime you have to use a real
ActionFormto 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
ActionFormis not so hard to create after all.
相关文章推荐
- java问题诊断
- java实现ping ip地址
- Spring定时任务
- java json
- eclipse中出现的错误
- Java笔记学习总结(四)
- Java的缓存机制
- mac下 Struts2 第一个程序的详细步骤(附带源码链接)
- Spring MVC 教程,快速入门,深入分析
- Java -verbose:gc命令
- axia2+spring配置实例
- java list add的弊病
- java 中类为啥要序列化
- 关于文本输入框获取光标位置以及指定位置插入内容
- 必须知道的八大种排序算法【java实现】(三) 归并排序算法、堆排序算法详解
- Java中的可变参数以及foreach语句
- javaMail-入门
- java源码查看工具之Source Insight
- java中session对象及其常用方法
- md5 加密java代码