[cvs]
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller
DefaultForm.java
JCorporate Ltd
jcorp at jcorp2.servlets.net
Mon Jul 19 16:39:30 PDT 2004
Update of /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller
In directory jcorp2.servlets.net:/tmp/cvs-serv28787
Modified Files:
DefaultForm.java
Log Message:
added struts validator integration
Index: DefaultForm.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller/DefaultForm.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** DefaultForm.java 26 Nov 2003 10:42:05 -0000 1.10
--- DefaultForm.java 19 Jul 2004 23:39:28 -0000 1.11
***************
*** 76,79 ****
--- 76,94 ----
import java.util.Iterator;
+ /** @todo imports required for Struts Validator *RD* Mon Jul 27 2004 */
+ import java.net.URLDecoder;
+ import java.util.Locale;
+ import java.util.StringTokenizer;
+ import javax.servlet.http.HttpServletRequest;
+ import javax.servlet.ServletContext;
+ import org.apache.commons.validator.Validator;
+ import org.apache.commons.validator.ValidatorResults;
+ import org.apache.commons.validator.ValidatorException;
+ import org.apache.commons.validator.ValidatorResources;
+ import org.apache.struts.validator.Resources;
+ import org.apache.commons.logging.Log;
+ import org.apache.commons.logging.LogFactory;
+
+
/**
* * <p> This class is a bridge between Expresso's Controller system and the Struts
***************
*** 86,90 ****
public class DefaultForm
extends ActionForm
! implements Serializable {
private Hashtable formData = new Hashtable();
private Hashtable formAttributes = null;
--- 101,105 ----
public class DefaultForm
extends ActionForm
! implements Serializable, StateForm {
private Hashtable formData = new Hashtable();
private Hashtable formAttributes = null;
***************
*** 183,185 ****
return (String) formData.get(fieldName);
}
! }
\ No newline at end of file
--- 198,453 ----
return (String) formData.get(fieldName);
}
!
! /**
! * Validate the properties that have been set from this HTTP request,
! * and return an <code>ActionErrors</code> object that encapsulates any
! * validation errors that have been found. If no errors are found, return
! * <code>null</code> or an <code>ActionErrors</code> object with no
! * recorded error messages.
! *
! * @param mapping The mapping used to select this instance.
! * @param request The servlet request we are processing.
! * @return ActionErrors containing validation errors.
! */
! /** @todo add this method so validation can be called from an external State *RD* Mon Jul 27 2004 */
! public ActionErrors validate(ActionMapping mapping,
! HttpServletRequest request) {
!
! ServletContext application = getServlet().getServletContext();
! ActionErrors errors = new ErrorCollection();
!
! try {
! Validator validator =
! initValidator(getState(request), this,
! application, request,
! errors, page);
!
! validatorResults = validator.validate();
! }
! catch (ControllerException ex) {
! }
! catch (ValidatorException e) {
! log.error(e.getMessage(), e);
! }
!
! return errors;
! }
!
! /**
! * Validate the properties that have been set from this HTTP request,
! * and return an <code>ActionErrors</code> object that encapsulates any
! * validation errors that have been found. If no errors are found, return
! * <code>null</code> or an <code>ActionErrors</code> object with no
! * recorded error messages.
! *
! * @param mapping The mapping used to select this instance.
! * @param request The servlet request we are processing.
! * @return ActionErrors containing validation errors.
! */
! /** @todo add this method so the form can be validated with Struts Validator *RD* Mon Jul 27 2004 */
! public ErrorCollection validate(ControllerRequest request) {
! if (request instanceof ServletControllerRequest) {
! ServletControllerRequest scr = (ServletControllerRequest) request;
! return (ErrorCollection) this.validate(scr.getMapping(), (HttpServletRequest) scr.getServletRequest());
! } else
! return new ErrorCollection();
! }
!
!
! /**
! * Get the state requested by the transition button in the form
! * @param params the hashtable of parameters to parse
! * @return String
! * @throws ControllerException
! */
! /** @todo add this method so Struts Validator can find the formset containing the rules *RD* Mon Jul 27 2004 */
! /** @todo I'm sure there must be a more elegant way for doing this *RD* Mon Jul 27 2004 */
! private static String getState(HttpServletRequest request) throws
! ControllerException {
!
! log.debug("Adding button parameters");
!
! String oneParamName = null;
! Object oneParamValue = null;
! boolean gotControllerFromButton = false;
!
! for (Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {
! oneParamName = (String) e.nextElement();
! oneParamValue = StringUtil.notNull(request.getParameter(oneParamName));
!
! if (oneParamName.startsWith("button_")) {
!
! //We add a special parameter to the collection, which
! //is simply "button"="name_ofButton"
! String buttonName = oneParamName.substring(oneParamName.indexOf(
! "_") + 1);
!
! /**
! * button names come in from the browser like
!
! button_promptLogin.y
! button_promptLogin.x
!
! cut off those .x, .y trailers
! */
! if (buttonName.endsWith(".x")) {
! buttonName = buttonName.substring(0, buttonName.length() - 2);
! }
! if (buttonName.endsWith(".y")) {
! buttonName = buttonName.substring(0, buttonName.length() - 2);
! }
!
! if (log.isDebugEnabled()) {
! log.debug("There is a button parameter called '" +
! buttonName + "'");
! }
!
! /* pick up the corresponding parameter string, if any */
! String paramString = (String) request.getParameter(buttonName +
! "_params");
!
! if (paramString != null) {
! log.debug("Button parameters:");
!
! //We first check to see if the button parameters string is encoded. If it is, we decode it
! //before parsing it.
! String encodeType = (String) request.getParameter(buttonName +
! "_encoding");
!
! if ("u".equals(encodeType)) {
!
! //this param string has been URL encoded
! try {
! paramString = java.net.URLDecoder.decode(
! paramString);
! }
! catch (Exception ex) {
! log.error("Could not URLDecode: " + paramString,
! ex);
! }
! }
! /* if the param string for the button was encoded */
!
! /* parse it into a hashtable */
! StringTokenizer stkpm = null;
! stkpm = new StringTokenizer(paramString, "&");
!
! while (stkpm.hasMoreTokens()) {
! String pairValue = stkpm.nextToken();
!
! //This decode is paired up with the getParamString() method's encode() in
! //the Transition class.
! try {
! pairValue = URLDecoder.decode(pairValue);
! }
! catch (Exception ex) {
! log.error("Could not URLDecode: " + pairValue, ex);
! }
!
! String paramName = pairValue;
! String paramValue = "";
! int position = pairValue.indexOf("=");
! if (position != -1) {
! paramName = pairValue.substring(0, position);
! position++; //ignore the '='
! if (position < pairValue.length()) {
! paramValue = pairValue.substring(position);
! }
! }
!
! if (paramName.equals("controller")) {
! gotControllerFromButton = true;
! }
! if (log.isDebugEnabled()) {
! log.debug("Parameter '" + paramName + "', value '"
! + paramValue + "'");
! }
! if (paramName.equals("state")) {
! return paramValue;
! }
!
! }
! /* while more parameters in the button param string */
!
! log.debug("End button parameters");
! }
! else { /* if the button param string exists */
! throw new ControllerException("Button '" + buttonName +
! "' was clicked, but no " +
! "button parameters field called '" +
! buttonName +
! "_params' was found.");
! }
! }
! /* if this param is a buitton */
!
! }
! /* for each of the parameters */
!
! return "";
! }
!
! /**
! * Initialize the <code>Validator</code> to perform validation.
! *
! * @param key The key that the validation rules are under (the form elements
! * name attribute).
! * @param bean The bean validation is being performed on.
! * @param application servlet context
! * @param request The current request object.
! * @param errors The object any errors will be stored in.
! * @param page This in conjunction with the page property of a
! * <code>Field<code> can control the processing of fields. If the field's
! * page is less than or equal to this page value, it will be processed.
! */
! /** @todo add this method for Struts Validator to work *RD* Mon Jul 27 2004 */
! public Validator initValidator(
! String key,
! Object bean,
! ServletContext application,
! HttpServletRequest request,
! ActionErrors errors,
! int page) {
!
! ValidatorResources resources = Resources.getValidatorResources(application,
! request);
!
! Locale locale = Resources.getLocale(request);
!
! Validator validator = new Validator(resources, key);
! validator.setUseContextClassLoader(true);
!
! validator.setPage(page);
!
! validator.addResource(Resources.
! SERVLET_CONTEXT_KEY, application);
! validator.addResource(Resources.
! HTTP_SERVLET_REQUEST_KEY, request);
! validator.addResource(Validator.LOCALE_KEY, locale);
! validator.addResource(Resources.
! ACTION_ERRORS_KEY, errors);
! validator.addResource(Validator.BEAN_KEY, bean);
!
! return validator;
! }
!
! /**
! * Used to indicate the current page of a multi-page form.
! */
! /** @todo Struts Validator requires this variable to exist *RD* Mon Jul 27 2004 */
! protected int page = 0;
!
! /**
! * The results returned from the validation performed
! * by the <code>Validator</code>.
! */
! /** @todo Struts Validator requires this variable to exist *RD* Mon Jul 27 2004 */
! protected ValidatorResults validatorResults = null;
!
! /**
! * Commons Logging instance.
! */
! /** @todo Struts Validator requires this variable to exist *RD* Mon Jul 27 2004 */
! private static Log log = LogFactory.getLog(DefaultForm.class);
!
! }
More information about the cvs
mailing list