I've been playing with several different web application frameworks, and decided to spend some time writing up a real simple little threaded discussion board in Maverick. I also managed to write a fairly complete base controller which integrates with FormProc to give you semi-automated form validation. I thought I'd send it to the list to see what people think. I've attached the maverick.xml and the SuperController.java files for people to look at. I also threw in one of the derived controllers to show how it is used. The entire threaded discussion forum is much to large to put in an e-mail, but I may throw it up on a website somewhere if people are interested in it as a learning project.
Anyway, Maverick is really cool. Thanks for the good work. Have you guys thought of hooking up some XDoclet stuff like struts and webwork have done?
package com.zedshaw.mavforum.control; import com.zedshaw.mavforum.data.*; import cirrus.hibernate.*; import org.infohazard.maverick.ctl.*; import org.infohazard.maverick.flow.*; import java.util.*; import org.formproc.*; import org.formproc.servlet.*; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log;
/**
* Description of the Class
*
*@author zedshaw
*@created December 16, 2002
*/
public class ForumList extends SuperController {
/**
* Description of the Method
*
*@param controllerNode Description of the Parameter
*@exception ConfigException Description of the Exception
*/
public void init(org.jdom.Element controllerNode) throws ConfigException {
// we only do this in the ForumController so that it is done once
super.init(controllerNode);
HibernateManager.init();
}
/**
* A logger for printing out messages.
*/
private static Log log = LogFactory.getLog(ForumList.class);
/**
* Constructor for the ForumController object
*/
public ForumList() {
super();
}
/**
* Description of the Method
*
*@param ctx Description of the Parameter
*@param formResult Description of the Parameter
*@param formModel Description of the Parameter
*@return Description of the Return Value
*@exception Exception Description of the Exception
*/
public String perform(ControllerContext ctx, FormResult formResult, Map
formModel) throws Exception {
// get the forums and set them up as the model
Session sess = HibernateManager.getSession(ctx);
boolean hasErrorr = false;
// clear out the currentForum
ctx.getRequest().getSession().setAttribute("currentForum", null);
try {
List forums = sess.find("from forums in class Forum");
ctx.setModel(forums);
} catch (Exception e) {
ctx.setParam("errorReason", "Problem listing all the forums.");
e.printStackTrace();
hasErrorr = true;
} finally {
sess.disconnect();
}
return hasErrorr ? ERROR : SUCCESS;
}
}
package com.zedshaw.mavforum.control;
import org.infohazard.maverick.ctl.*;
import org.infohazard.maverick.flow.*;
import java.util.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.MethodUtils;
import javax.servlet.*;
import javax.servlet.http.*;
import org.formproc.*;
import org.formproc.servlet.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.jdom.*;
/**
* Description of the Class
*
*@author zedshaw
*@created December 16, 2002
*/
public class SuperController implements ControllerSingleton {
/**
* Common name for the typical "success" view.
*/
public final static String SUCCESS = "success";
/**
* Common name for the typical "error" view.
*/
public final static String ERROR = "error";
/**
* Indicates that there is an error in the form submitted.
*/
public final static String FORM_ERROR = "form_error";
/**
* This is the prefix for the methods, which the ACTION_KEY is appended to
*/
public final static String PERFORM_PREFIX = "perform";
/**
* A logger for printing out messages.
*/
private static Log log = LogFactory.getLog(ControllerSingleton.class);
/**
* The form manager used by this controller to verify forms.
*/
protected FormManager formManager = null;
/**
* The name of the form to use when processing the input.
*/
protected String formName = null;
/**
* Determines whether to use an ExtendedHttpForm or not.
*/
protected boolean isFormExtended = false;
/**
* Determines whether the form errors should be passed through to the
* controller or if an error in the form should be rejected with FORM_ERROR. A
* value of true means to pass the errors on to the controlle, and false means
* to reject.
*/
protected boolean isFormPassed = false;
/**
* Description of the Field
*/
protected boolean hasForm = false;
/**
* Initializes the singleton with the configuration node.
*
*@param controllerNode Description of the Parameter
*@exception ConfigException Description of the Exception
*/
public void init(org.jdom.Element controllerNode) throws ConfigException {
try {
formManager = new FormManager();
} catch (Exception e) {
log.error("Could not start the FormManager to process forms:"
+ e);
throw new ConfigException(e);
}
// process the <form> tags inside our controller statement
List children = controllerNode.getChildren("form");
Iterator i = children.iterator();
// there should only be one form tag, so we just do one hasNext() check
if (i.hasNext()) {
Element el = (Element) i.next();
log.info("Element: " + el + " " + el.getAttribute("name") + "
" + el.getAttribute("extended"));
try {
formName = el.getAttribute("name").getValue();
} catch (Exception e) {
log.error("Could not read name attribute of form");
}
try {
isFormExtended =
el.getAttribute("extended").getBooleanValue();
} catch (Exception e) {
// this means that the extended attribute was not set,
so we default to isFormExtended=false
}
try {
isFormPassed =
el.getAttribute("pass").getBooleanValue();
} catch (Exception e) {
// this means that the attribute was not set, so
default to false
}
hasForm = true;
} else {
log.debug("Controller does not have a form, results will be
raw request input.");
hasForm = false;
}
}
/**
* Sets up the servlet parameters and calls through to the parameterless
* rawPerform() method. Does not result in bean population.
*
*@param cctx Description of the Parameter
*@return Description of the Return Value
*@exception ServletException Description of the Exception
*@see Controller#perform
*/
public final String go(ControllerContext cctx) throws ServletException {
Map processedModel = null;
FormResult result = null;
String performResult = null;
// figure out if this controller has a form
if (hasForm) {
processedModel = new HashMap();
try {
if (isFormExtended) {
ExtendedHttpForm form = new
ExtendedHttpForm(formName, processedModel);
formManager.configure(form);
result = form.process(cctx.getRequest());
} else {
HttpForm form = new HttpForm(formName,
processedModel);
formManager.configure(form);
result = form.process(cctx.getRequest());
}
} catch (Exception e) {
log.error("Problem processing form " + formName + "
because of: " + e);
return FORM_ERROR;
}
// reject the submission to the FORM_ERROR view if the form is
not requested to be passed and
// it is invalid. Otherwise, the goods are just passed on to
the perform method.
if (!isFormPassed && !result.isValid()) {
// looks like there were problems
cctx.setModel(result);
return FORM_ERROR;
}
} else {
// since they don't have a form, then we just set the
processed model to the parameter map
log.warn("no form specified");
processedModel = cctx.getRequest().getParameterMap();
}
try {
performResult = perform(cctx, result, processedModel);
} catch (Exception e) {
// TODO: add exception handling
e.printStackTrace();
throw new ServletException(e);
}
return performResult;
}
/**
* This is the base perform method which does nothing but return success.
*
*@param ctx Description of the Parameter
*@param formResult Description of the Parameter
*@param formModel Description of the Parameter
*@return Description of the Return Value
*@exception Exception Description of the Exception
*/
public String perform(ControllerContext ctx, FormResult formResult, Map
formModel) throws Exception {
log.error("Perform method called in SuperController, which means it
wasn't properly overridden.");
return SUCCESS;
}
}
maverick.xml
Description: application/text
----- Zed A. Shaw http://www.zedshaw.com/
