You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by he...@apache.org on 2003/07/10 14:33:24 UTC

cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/util/velocity VelocityActionEvent.java

henning     2003/07/10 05:33:24

  Modified:    conf     TurbineResources.properties
               conf/master TurbineResources.master
               xdocs/howto action-event-howto.xml
               src/java/org/apache/turbine TurbineConstants.java
               src/java/org/apache/turbine/modules ActionEvent.java
               src/java/org/apache/turbine/util/velocity
                        VelocityActionEvent.java
  Log:
  This modification allows ActionEvent and VelocityActionEvent to "ignore"
  eventSubmit_<xxx> events if they contain an empty or a zero value. This is
  useful if you have multiple event fields in a form and only one should be
  selected (e.g. by a client side java script application).
  
  This behaviour is controlled by a property which defaults to "off" which
  means no change from the current behaviour.
  
  Revision  Changes    Path
  1.45      +9 -1      jakarta-turbine-2/conf/TurbineResources.properties
  
  Index: TurbineResources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/conf/TurbineResources.properties,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- TurbineResources.properties	23 Jun 2003 14:59:30 -0000	1.44
  +++ TurbineResources.properties	10 Jul 2003 12:33:24 -0000	1.45
  @@ -200,6 +200,14 @@
   
   action.accesscontroller=AccessController
   
  +#
  +# Set this value to true if you want to be able to submit multiple 
  +# events to an Action by doing eventSubmit_do<action> and consider
  +# only events that have a non-empty, non-zero value. This is useful
  +# if you submit your form with JavaScript
  +# 
  +action.eventsubmit.needsvalue = false
  +
   # -------------------------------------------------------------------
   #
   #  J N D I  C O N T E X T S
  
  
  
  1.44      +9 -1      jakarta-turbine-2/conf/master/TurbineResources.master
  
  Index: TurbineResources.master
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/conf/master/TurbineResources.master,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- TurbineResources.master	23 Jun 2003 14:59:30 -0000	1.43
  +++ TurbineResources.master	10 Jul 2003 12:33:24 -0000	1.44
  @@ -200,6 +200,14 @@
   
   action.accesscontroller=@ACTION_ACCESS_CONTROLLER@
   
  +#
  +# Set this value to true if you want to be able to submit multiple 
  +# events to an Action by doing eventSubmit_do<action> and consider
  +# only events that have a non-empty, non-zero value. This is useful
  +# if you submit your form with JavaScript
  +# 
  +action.eventsubmit.needsvalue = false
  +
   # -------------------------------------------------------------------
   #
   #  J N D I  C O N T E X T S
  
  
  
  1.2       +8 -0      jakarta-turbine-2/xdocs/howto/action-event-howto.xml
  
  Index: action-event-howto.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/xdocs/howto/action-event-howto.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- action-event-howto.xml	16 Aug 2001 05:10:10 -0000	1.1
  +++ action-event-howto.xml	10 Jul 2003 12:33:24 -0000	1.2
  @@ -5,6 +5,7 @@
    <properties>
     <title>Action Events Howto</title>
     <author email="jon@latchkey.com">Jon S. Stevens</author>
  +  <author email="hps@intermeta.de">Henning P. Schmiedehausen</author>
    </properties>
   
   <body>
  @@ -101,6 +102,13 @@
   <p>
   If you follow these conventions, then you should be ok with your method
   naming in your Action class.
  +</p>
  +
  +<p>
  +There is a property in the TurbineResources.properties file, called "action.eventsubmit.needsvalue".
  +If you set this to "true", only the events that contain a non-empty, non-zero value will be
  +executed. This is useful if you have a form with multiple, different events, that need to be executed
  +and the form is submitted by client-side code, e.g. JavaScript.
   </p>
   
   </section>
  
  
  
  1.24      +7 -1      jakarta-turbine-2/src/java/org/apache/turbine/TurbineConstants.java
  
  Index: TurbineConstants.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/TurbineConstants.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- TurbineConstants.java	11 Mar 2003 17:52:47 -0000	1.23
  +++ TurbineConstants.java	10 Jul 2003 12:33:24 -0000	1.24
  @@ -199,6 +199,12 @@
       /** Default value for the access controller. (org.apache.modules.actions.AccessController) */
       String ACTION_ACCESS_CONTROLLER_DEFAULT = "AccessController";
   
  +    /** Select whether an Action Event must have a non-zero value */
  +    String ACTION_EVENTSUBMIT_NEEDSVALUE_KEY = "action.eventsubmit.needsvalue";
  +
  +    /** Default value for action.eventsubmit.needsvalue */
  +    boolean ACTION_EVENTSUBMIT_NEEDSVALUE_DEFAULT = false;
  +
       /** Default layout. */
       String LAYOUT_DEFAULT = "layout.default";
   
  
  
  
  1.12      +88 -4     jakarta-turbine-2/src/java/org/apache/turbine/modules/ActionEvent.java
  
  Index: ActionEvent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/modules/ActionEvent.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ActionEvent.java	11 Apr 2003 13:15:09 -0000	1.11
  +++ ActionEvent.java	10 Jul 2003 12:33:24 -0000	1.12
  @@ -59,6 +59,11 @@
   
   import org.apache.commons.lang.StringUtils;
   
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
  +
  +import org.apache.turbine.Turbine;
  +import org.apache.turbine.TurbineConstants;
   import org.apache.turbine.util.RunData;
   import org.apache.turbine.util.parser.ParameterParser;
   import org.apache.turbine.util.parser.ParserUtils;
  @@ -109,6 +114,9 @@
    */
   public abstract class ActionEvent extends Action
   {
  +    /** Logging */
  +    protected Log log = LogFactory.getLog(this.getClass());
  +
       /**
        * You need to implement this in your classes that extend this class.
        *
  @@ -129,6 +137,26 @@
       /** The length of the button to look for. */
       protected static final int LENGTH = BUTTON.length();
   
  +    /** 
  +     * If true, the eventSubmit_do<xxx> variable must contain
  +     * a not null value to be executed.
  +     */
  +    private boolean submitValueKey = false;
  +    
  +    /**
  +     * C'tor
  +     */
  +    public ActionEvent()
  +    {
  +        super();
  +        
  +        submitValueKey = Turbine.getConfiguration()
  +                .getBoolean(TurbineConstants.ACTION_EVENTSUBMIT_NEEDSVALUE_KEY,
  +                        TurbineConstants.ACTION_EVENTSUBMIT_NEEDSVALUE_DEFAULT);
  +
  +        log.debug("Need Submit Value: " + submitValueKey);
  +    }
  +    
       /**
        * This overrides the default Action.perform() to execute the
        * doEvent() method. If that fails, then it will execute the
  @@ -172,8 +200,11 @@
               String key = (String) it.next();
               if (key.startsWith(button))
               {
  -                theButton = formatString(key);
  -                break;
  +                if (considerKey(key, pp))
  +                {
  +                    theButton = formatString(key);
  +                    break;
  +                }
               }
           }
   
  @@ -223,6 +254,59 @@
           }
           return tmp;
       }
  -}
   
  +    /**
  +     * Checks whether the selected key really is a valid event.
  +     *
  +     * @param key The selected key
  +     * @param pp The parameter parser to look for the key value
  +     *
  +     * @return true if this key is really an ActionEvent Key
  +     */
  +    protected boolean considerKey(String key, ParameterParser pp)
  +    {
  +        if (!submitValueKey)
  +        {
  +            log.debug("No Value required, accepting " + key);
  +            return true;
  +        }
  +        else
  +        {
  +            // If the action.eventsubmit.needsvalue key is true, 
  +            // events with a "0" or empty value are ignored.
  +            // This can be used if you have multiple eventSubmit_do<xxx>
  +            // fields in your form which are selected by client side code, 
  +            // e.g. JavaScript.
  +            //
  +            // If this key is unset or missing, nothing changes for the
  +            // current behaviour.
  +            //
  +            String keyValue = pp.getString(key);
  +            log.debug("Key Value is " + keyValue);
  +            if (StringUtils.isEmpty(keyValue))
  +            {
  +                log.debug("Key is empty, rejecting " + key);
  +                return false;
  +            }
   
  +            try
  +            {
  +                if (Integer.parseInt(keyValue) != 0)
  +                {
  +                    log.debug("Integer != 0, accepting " + key);
  +                    return true;
  +                }
  +            }
  +            catch (NumberFormatException nfe)
  +            {
  +                // Not a number. So it might be a
  +                // normal Key like "continue" or "exit". Accept
  +                // it.
  +                log.debug("Not a number, accepting " + key);
  +                return true;
  +            }
  +        }
  +        log.debug("Rejecting " + key);
  +        return false;
  +    }
  +}
  
  
  
  1.12      +8 -9      jakarta-turbine-2/src/java/org/apache/turbine/util/velocity/VelocityActionEvent.java
  
  Index: VelocityActionEvent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/util/velocity/VelocityActionEvent.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- VelocityActionEvent.java	22 Jun 2003 19:07:00 -0000	1.11
  +++ VelocityActionEvent.java	10 Jul 2003 12:33:24 -0000	1.12
  @@ -59,7 +59,6 @@
   import java.util.Iterator;
   
   import org.apache.commons.logging.Log;
  -import org.apache.commons.logging.LogFactory;
   
   import org.apache.turbine.modules.ActionEvent;
   import org.apache.turbine.services.velocity.TurbineVelocity;
  @@ -83,14 +82,11 @@
    *
    * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
  + * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    * @version $Id$
    */
   public abstract class VelocityActionEvent extends ActionEvent
   {
  -
  -    /** Use this to get the right class even when extended */
  -    private Log log = LogFactory.getLog(this.getClass());
  -    
       /**
        * You need to implement this in your classes that extend this
        * class.
  @@ -145,8 +141,11 @@
               String key = (String) it.next();
               if (key.startsWith(button))
               {
  -                theButton = formatString(key);
  -                break;
  +                if (considerKey(key, pp))
  +                {
  +                    theButton = formatString(key);
  +                    break;
  +                }
               }
           }
   
  @@ -177,7 +176,7 @@
           catch (NoSuchMethodException nsme)
           {
               // Attempt to execute things the old way..
  -            log.debug("Couldn't locate the Event, running executeEvents() in "
  +            log.debug("Couldn't locate the Event ( " + theButton + "), running executeEvents() in "
                       + super.getClass().getName());
               super.executeEvents(data);
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org