struts ActionForm设计的缺陷

假设我有一个功能是增加联系人记录,这时我会在Module层实现一个:
public class Module{
public void AddContact(Contact c)throws Exception
{
......
}
}
public class Contact{
private String name;
private String address;
......
}
在View层做个页面addContact.htm用来提交ContactForm表单
此时,还要做个Form类来包装表单数据:
public class ContactForm extern ActionForm{
private String name;
private String address;
......
}

这时发现,ContactForm和Contact里面的代码几乎一样,如果能让ContactForm来继承Contact应该是比较理想的设计,但问题是ContactForm又要扩展ActionForm,由于java的类单继承特征,使它不能再继承Contact.还有,在Action调用Module时,又要把ContactForm转换成Contact.
如果ContactForm可以继承Contact或直接把Contact来作为Form类的话,在设计上和代码编写上就会精简很多.
欢迎大家谈谈自己的看法.

我觉得到不是问题.
formbean只是jsp中form的一个数据映射,它负责完成数据从jsp到后台的获取.和你后台处理持久的类毫无关系.如果没有formbean,你一样要写一堆属性和getParameter...
我觉得他们是不同层面的对象,不能说那个是多余的

这两者是类似,我认为有优点也有缺点。

优点是MVC模式,ActionForm属于V,而Contact等属于Model,这样分层清楚,很多设计都要依赖Model,这样,可以摆脱对界面的依赖。

缺点是麻烦,自己做个框架或自动工具来实现吧。

在这里,有一个比较好的办法

就是让Form持有一个该model的实体----但仅是普通的Object而已
再 把 model中要在页面中用到的属性,在Form中再声明一次


public class Contact{
private String name;
//setter
//getter
}
public class ContactForm extends ActionForm{
private Contact contact;

//contact setter
//contact getter
public String getName(){
return contact.getName();
}
public void setName(String name){
this.contact.setName(name);
}
}

如果你的开发工具支持 ContactForm 中的那些getter setter可以自动生成。

Struts的作者怎么会把这样的问题给拉下呢,试试
org.apache.commons.beanutils的工具
BeanUtils.copyProperties()

可以把一个Object的属性复制到另外一个Object,并能实现自动类型转换


//这是一个bean
public class DemoBean {
private String userName; //String in Bean
private int userAge; //String in Form
private boolean checked; //boolean 0,1 in Form
private String[] selectedItems; //String[] in Form
private long score; //String in Form
.
.
}



//这是一个ActionForm
public class DemoForm extends ActionForm {
private String userName; //String in Bean
private String userAge; //int in Bean
private boolean checked; //boolean 0,1 in Bean
private String[] selectedItems; //String[] in Bean
private String score; //long in Bean
.
.
}



//这是一个演示
public class TestBeanFormConvert extends TestCase {
public void testB2F() throws IllegalAccessException, InvocationTargetException{
DemoBean bean = getBean();
DemoForm form = new DemoForm();

BeanUtils.copyProperties(form,bean);
System.out.print("ok");
}

public void testF2B() throws IllegalAccessException, InvocationTargetException{
DemoForm form = getForm();
DemoBean bean= new DemoBean();
BeanUtils.copyProperties(bean,form);
System.out.print("ok");
}


/**
* initiate a bean
* @author:hefeng
* 2004-10-11 11:32:52
* @return DemoBean
*/
private DemoBean getBean(){
DemoBean bean = new DemoBean();
bean.setChecked(true);
bean.setScore(198293);

String[] selectedItems = getSelectedItems();
bean.setSelectedItems(selectedItems);

bean.setUserAge(22);
bean.setUserName(
"Hefeng");

return bean;
}

//Initiate an Array of String
private String[] getSelectedItems() {
String[] selectedItems = new String[4];
selectedItems[0] =
"55";
selectedItems[1] =
"66";
selectedItems[2] =
"77";
selectedItems[3] =
"88";
return selectedItems;
}

/**
* Initiate an ActionForm
* @author:hefeng
* 2004-10-11 11:35:09
* @return
*/

private DemoForm getForm(){
DemoForm form = new DemoForm();
form.setChecked(true);
form.setScore(
"198293");
form.setSelectedItems(getSelectedItems());
form.setUserAge(
"22");
form.setUserName(
"Hefeng");

return form;
}

}

这只是个粗略的演示,使用copyProperties()的时候有很多需要注意的,具体去查看Api吧。
BeanUtils很强大,能给我们节省很多工作。最近想写一篇Struts ActionForm 的使用技巧说明 ....

这不出问题,也不是struts缺陷。

直接在form里设置一个dto对象属性就可以,

private Module module ;

public set....
public get...

你的jsp里这样写就可以:

<html:text property="module.name" />


接着



<html:text property="module.name"/>

>


> BeanUtils很强大,能给我们节省很多工作。最近想写一篇Str
> ts ActionForm 的使用技巧说明 ....

Well, this is a simple sample with transferring datas between ActionForm and VOs by commons-beanutils.

However, why not *ONLY* use the DynaActionForm, even when you use struts 1.2 , you *CAN* use the LazyDynaActionForm !

The Original post means why not avoid too many duplicate codes in your system, yap, we don't wanna write and maintain more same codes in system. Just Try dynaform.

http://blog.csdn.net/jakarta99/archive/2004/09/09/99143