You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by mr...@apache.org on 2007/07/16 07:30:38 UTC

svn commit: r556506 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/dispatcher/mapper/ test/java/org/apache/struts2/dispatcher/mapper/

Author: mrdon
Date: Sun Jul 15 22:30:37 2007
New Revision: 556506

URL: http://svn.apache.org/viewvc?view=rev&rev=556506
Log:
Adding ability to add custom action parameter prefixes
WW-1815

Added:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/ParameterAction.java
Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java?view=diff&rev=556506&r1=556505&r2=556506
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java Sun Jul 15 22:30:37 2007
@@ -235,6 +235,17 @@
             }
         };
     }
+
+    /**
+     * Adds a parameter action.  Should only be called during initialization
+     *
+     * @param prefix The string prefix to trigger the action
+     * @param parameterAction The parameter action to execute
+     * @since 2.1.0
+    */
+    protected void addParameterAction(String prefix, ParameterAction parameterAction) {
+        prefixTrie.put(prefix, parameterAction);
+    }
     
     @Inject(StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION)
     public void setAllowDynamicMethodCalls(String allow) {
@@ -491,11 +502,4 @@
 		return allowSlashesInActionNames;
 	}
 	
-	/**
-     * Defines a parameter action prefix
-     */
-    interface ParameterAction {
-        void execute(String key, ActionMapping mapping);
-    }
-
 }

Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/ParameterAction.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/ParameterAction.java?view=auto&rev=556506
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/ParameterAction.java (added)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/ParameterAction.java Sun Jul 15 22:30:37 2007
@@ -0,0 +1,13 @@
+package org.apache.struts2.dispatcher.mapper;
+
+/**
+ * Defines a parameter action prefix.  This is executed when the configured prefix key is matched in a parameter
+ * name, allowing the implementation to manipulate the action mapping accordingly.  For example, if the "action:foo"
+ * parameter name was found, and a ParameterAction implementation was registered to handle the "action" prefix, the
+ * execute method would be called, allowing the implementation to set the "method" value on the ActionMapping.
+ * 
+ * @since 2.1.0
+ */
+public interface ParameterAction {
+    void execute(String key, ActionMapping mapping);
+}

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java?view=diff&rev=556506&r1=556505&r2=556506
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java (original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java Sun Jul 15 22:30:37 2007
@@ -302,7 +302,7 @@
 
         assertEquals(actionMapping.getName(), "myAction");
     }
-    
+
     public void testActionPrefix_fromImageButton() throws Exception {
         Map parameterMap = new HashMap();
         parameterMap.put(DefaultActionMapper.ACTION_PREFIX + "myAction", "");
@@ -381,6 +381,25 @@
         assertTrue(result instanceof ServletRedirectResult);
 
         // TODO: need to test location but there's noaccess to the property/method, unless we use reflection
+    }
+
+    public void testCustomActionPrefix() throws Exception {
+        Map parameterMap = new HashMap();
+        parameterMap.put("foo:myAction", "");
+
+        StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
+        request.setParameterMap(parameterMap);
+        request.setupGetServletPath("/someServletPath.action");
+
+        DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
+        defaultActionMapper.addParameterAction("foo", new ParameterAction() {
+            public void execute(String key, ActionMapping mapping) {
+                mapping.setName("myAction");
+            }
+        });
+        ActionMapping actionMapping = defaultActionMapper.getMapping(request, configManager);
+
+        assertEquals(actionMapping.getName(), "myAction");
     }
 
     public void testDropExtension() throws Exception {