I did this recently.  I simply wrote a base action class which extends
the struts Action class.  Here's the code snippet:

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    throws Exception {
      if(form!=null) {
        trimFormFields(form);
      return(executeAction(mapping,form,request,response));
    }
    
    abstract ActionForward executeAction(ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response)
    throws Exception;
    
    protected void trimFormFields(ActionForm form)
    throws Exception {
      Map properties = PropertyUtils.describe(form);
      Iterator it = properties.keySet().iterator();
      String property; Object value;
      while(it.hasNext()) {
        property = ((String)it.next());
        value = PropertyUtils.getSimpleProperty(form,property);
        if(value!=null && value instanceof java.lang.String) { 
        
PropertyUtils.setSimpleProperty(form,property,((String)value).trim());
        }
      }
    }
    
So whatever common action logic I want all my actions to perform, I
placed inside of BaseAction's execute() method.  Then I extend the
BaseAction by creating an executeAction body in all subclasses.  Then
when the action is invoked, BaseAction takes care of trimming all form
properties which were defined as string types.  This has been working
quite well for us for sometime now.

Hope this helps,
Chris

     
-----Original Message-----
From: K.C. Baltz [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, March 01, 2005 12:53 PM
To: Struts Users Mailing List
Subject: Is there a way to auto-trim String properties in DynaForms?


I'm reposting this in the hopes that there is an answer out there.

> It would be really nice if I could programmatically indicate that
> certain fields in a DynaForm should be automatically trimmed of 
> whitespace.  Is there a way to do this?
> Alternatively, is there a way to get the behavior of the "required" 
> validation when using validwhen?  "required" doesn't accept values 
> that are nothing but whitespace whereas there's no way to test for 
> this when using validwhen.

K.C. Baltz

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to