Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1188333
  • 博文数量: 89
  • 博客积分: 10546
  • 博客等级: 上将
  • 技术积分: 1510
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-16 01:24
文章分类

全部博文(89)

文章存档

2012年(7)

2011年(4)

2010年(5)

2009年(52)

2008年(21)

分类: Java

2009-04-25 11:19:47

几年前写一篇 Struts设计复杂的ActionForm, 将ActionForm 中使用select , checkbox 的各种方法进行了一个简单的总结。
有些朋友仍然报怨,不知道如何为表单设置一个初始值,这篇正是为这些人而写。
我对原来的程序作了简单的修改,请从这里下载源代码,它仍然是一个Netbeans项目,你可以用最新的 NetBeans 6.5.1 打开进行编辑。
文件:survey.tar.gz
大小:37KB
下载:下载
另外添加一个简单的 EditSurveyAction 类,其中方法 initForm 专门用于设置初始值。

private void initForm(ActionForm form) {
        SurveyForm f = (SurveyForm) form;
        f.setGender("Male");
        f.setOccupation("developer");
        f.setPurpose(new String[]{"interest", "work"});

        f.setLinuxdist("debian", "Debian/Debian-based Linux");
        f.setLinuxdist("redhat", "Redhat/Fedora Linux");

        //this does work when init a checkbox group
        f.setPerferredDesktop(new String[]{"Gnome"});
}
这里可以设置初始并没有什么特别,只是使用ActionForm 的set 方法设置表单属性值。
其中,对于checkbox,有多种方法,使用Collection,Map,使用Indexed Properties等。
注意当页面使用使用一组
表单初始化成为了一个方法,移到了 BaseSurveyAction中。

 protected void prepareForm(ActionForm form, HttpServletRequest request) {
        SurveyForm f = (SurveyForm) form;

        //prepare the age selector list.
        //or put the list  in request scope.
        List occupationList = new ArrayList();
        occupationList.add(new LabelValueBean("Teacher", "teacher"));
        occupationList.add(new LabelValueBean("Student", "student"));
        occupationList.add(new LabelValueBean("Developer", "developer"));
        occupationList.add(new LabelValueBean("Others", "others"));
        f.setOccupationList(occupationList);

        List frequencyList = new ArrayList();
        frequencyList.add(new LabelValueBean("Every Day", "everyday"));
        frequencyList.add(new LabelValueBean("2-3 days per week", "2dperweek"));
        frequencyList.add(new LabelValueBean("1 day per week", "1dperweek"));
        frequencyList.add(new LabelValueBean("Every little", "little"));
        frequencyList.add(new LabelValueBean("Not use until now", "notuse"));

        request.setAttribute("frequencyList", frequencyList);

        List purposeList = new ArrayList();
        purposeList.add(new LabelValueBean("Work", "work"));
        purposeList.add(new LabelValueBean("Interest", "interest"));
        purposeList.add(new LabelValueBean("Others", "others"));
        request.setAttribute("purposeList", purposeList);


        List desktopList = new ArrayList();
        desktopList.add(new LabelValueBean("Gnome", "Gnome"));
        desktopList.add(new LabelValueBean("KDE", "KDE"));
        desktopList.add(new LabelValueBean("XFCE", "XFCE"));
        desktopList.add(new LabelValueBean("Other window manager", "Other window manager"));
        request.setAttribute("desktopList", desktopList);
        //set some default value

        List ops=new ArrayList();
        ops.add(new EditorCheckboxOption("Vi", false));
        ops.add(new EditorCheckboxOption("Emacs", false));
        ops.add(new EditorCheckboxOption("Gedit", false));
        ops.add(new EditorCheckboxOption("Kate", false));
        ops.add(new EditorCheckboxOption("Others", false));

        f.setEditors(ops);
    }

运行项目。

阅读(2760) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~