mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Adding ability to add custom action parameter prefixes
WW-1815 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@556506 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -235,6 +235,17 @@ public class DefaultActionMapper implements ActionMapper {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 @@ public class DefaultActionMapper implements ActionMapper {
|
||||
return allowSlashesInActionNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines a parameter action prefix
|
||||
*/
|
||||
interface ParameterAction {
|
||||
void execute(String key, ActionMapping mapping);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
+20
-1
@@ -302,7 +302,7 @@ public class DefaultActionMapperTest extends StrutsTestCase {
|
||||
|
||||
assertEquals(actionMapping.getName(), "myAction");
|
||||
}
|
||||
|
||||
|
||||
public void testActionPrefix_fromImageButton() throws Exception {
|
||||
Map parameterMap = new HashMap();
|
||||
parameterMap.put(DefaultActionMapper.ACTION_PREFIX + "myAction", "");
|
||||
@@ -383,6 +383,25 @@ public class DefaultActionMapperTest extends StrutsTestCase {
|
||||
// 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 {
|
||||
DefaultActionMapper mapper = new DefaultActionMapper();
|
||||
String name = mapper.dropExtension("foo.action");
|
||||
|
||||
Reference in New Issue
Block a user