KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > ctl > FormBeanUser


1 /*
2  * $Id: FormBeanUser.java,v 1.7 2003/10/27 11:00:38 thusted Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/ctl/FormBeanUser.java,v $
4  */

5
6 package org.infohazard.maverick.ctl;
7
8 import org.infohazard.maverick.flow.*;
9 import org.apache.commons.beanutils.BeanUtils;
10 import org.jdom.Element;
11 import javax.servlet.*;
12
13 /**
14  * FormBeanUser is a base class for singleton controllers which use
15  * external FormBeans rather than populating themselves. Since
16  * only one of these will exist for each command definition,
17  * it must be thread-safe. This Controller pattern is very similar
18  * to Struts Actions.
19  */

20 public abstract class FormBeanUser implements ControllerSingleton
21 {
22     /**
23      * Common name for the typical "success" view.
24      */

25     public static final String JavaDoc SUCCESS = "success";
26
27     /**
28      * Common name for the typical "error" view.
29      */

30     public static final String JavaDoc ERROR = "error";
31
32     /**
33      * If you want any custom behavior based on the content of the
34      * configuration file, this will be called once before the
35      * FormBeanUser is ever used.
36      *
37      * @see ControllerSingleton#init
38      */

39     public void init(Element controllerNode) throws ConfigException
40     {
41         // Defaults to nothing.
42
}
43
44     /**
45      * Executes this controller. Override one of the other perform()
46      * methods to provide application logic.
47      *
48      * @see ControllerSingleton#go
49      */

50     public final String JavaDoc go(ControllerContext cctx) throws ServletException
51     {
52         try
53         {
54             Object JavaDoc formBean = this.makeFormBean(cctx);
55             
56             BeanUtils.populate(formBean, cctx.getRequest().getParameterMap());
57             BeanUtils.populate(formBean, cctx.getControllerParams());
58             
59             cctx.setModel(formBean);
60
61             return this.perform(formBean, cctx);
62         }
63         catch (ServletException ex)
64         {
65             throw ex;
66         }
67         catch (Exception JavaDoc ex)
68         {
69             throw new ServletException(ex);
70         }
71     }
72
73     /**
74      * This method can be overriden to perform application logic.
75      *
76      * Override this method if you want the model to be something
77      * other than the formBean itself.
78      *
79      * @param formBean will be a bean created by makeFormBean(),
80      * which has been populated with the http request parameters.
81      */

82     protected String JavaDoc perform(Object JavaDoc formBean, ControllerContext cctx) throws Exception JavaDoc
83     {
84         return SUCCESS;
85     }
86                                                 
87     /**
88      * This method will be called to produce a simple bean whose properties
89      * will be populated with the http request parameters. The parameters
90      * are useful for doing things like persisting beans across requests.
91      */

92     protected abstract Object JavaDoc makeFormBean(ControllerContext cctx);
93 }
Popular Tags