Author: mrdon Date: Fri Oct 27 23:10:40 2006 New Revision: 468632 URL: http://svn.apache.org/viewvc?view=rev&rev=468632 Log: Added a setting to allow slashes in the action name to restore WW compatibility WW-1383
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.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/StrutsConstants.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java?view=diff&rev=468632&r1=468631&r2=468632 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java Fri Oct 27 23:10:40 2006 @@ -130,4 +130,8 @@ /** A list of configuration files automatically loaded by Struts */ public static final String STRUTS_CONFIGURATION_FILES = "struts.configuration.files"; + + /** Whether slashes in action names are allowed or not */ + public static final String STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES = "struts.enable.SlashesInActionNames"; + } 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=468632&r1=468631&r2=468632 ============================================================================== --- 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 Fri Oct 27 23:10:40 2006 @@ -163,12 +163,23 @@ static final String REDIRECT_ACTION_PREFIX = "redirect-action:"; - private static boolean allowDynamicMethodCalls = "true".equals(Settings - .get(StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION)); + private boolean allowDynamicMethodCalls = true; + + private boolean allowSlashesInActionNames = false; private PrefixTrie prefixTrie = null; public DefaultActionMapper() { + if (Settings.isSet(StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION)) { + allowDynamicMethodCalls = "true".equals(Settings + .get(StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION)); + } + + if (Settings.isSet(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES)) { + allowSlashesInActionNames = "true".equals(Settings + .get(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES)); + } + prefixTrie = new PrefixTrie() { { put(METHOD_PREFIX, new ParameterAction() { @@ -318,6 +329,14 @@ name = uri.substring(namespace.length() + 1); } + + if (!allowSlashesInActionNames && name != null) { + int pos = name.lastIndexOf('/'); + if (pos > -1 && pos < name.length() - 1) { + name = name.substring(pos + 1); + } + } + mapping.setNamespace(namespace); mapping.setName(name); } Modified: struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties?view=diff&rev=468632&r1=468631&r2=468632 ============================================================================== --- struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties (original) +++ struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties Fri Oct 27 23:10:40 2006 @@ -78,6 +78,15 @@ ### mappings, such as <action name="*/*" method="{2}" class="actions.{1}"> struts.enable.DynamicMethodInvocation = true +### Set this to true if you wish to allow slashes in your action names. If false, +### Actions names cannot have slashes, and will be accessible via any directory +### prefix. This is the traditional behavior expected of WebWork applications. +### Setting to true is useful when you want to use wildcards and store values +### in the URL, to be extracted by wildcard patterns, such as +### <action name="*/*" method="{2}" class="actions.{1}"> to match "/foo/edit" or +### "/foo/save". +struts.enable.SlashesInActionNames = false + ### use alternative syntax that requires %{} in most places ### to evaluate expressions for String attributes for tags struts.tag.altSyntax=true Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.java?view=diff&rev=468632&r1=468631&r2=468632 ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/config/SettingsTest.java Fri Oct 27 23:10:40 2006 @@ -45,7 +45,7 @@ assertEquals("de", locale.getLanguage()); int count = getKeyCount(); - assertEquals(31, count); + assertEquals(32, count); } public void testDefaultResourceBundlesLoaded() { 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=468632&r1=468631&r2=468632 ============================================================================== --- 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 Fri Oct 27 23:10:40 2006 @@ -22,6 +22,7 @@ import org.apache.struts2.StrutsConstants; import org.apache.struts2.StrutsTestCase; +import org.apache.struts2.config.Settings; import org.apache.struts2.dispatcher.ServletRedirectResult; import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest; @@ -91,18 +92,25 @@ } public void testGetMappingWithSlashedName() throws Exception { - setUp(); - req.setupGetRequestURI("/my/foo/actionName.action"); - req.setupGetServletPath("/my/foo/actionName.action"); - req.setupGetAttribute(null); - req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + + String old = Settings.get(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES); + Settings.set(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES, "true"); + try { + req.setupGetRequestURI("/my/foo/actionName.action"); + req.setupGetServletPath("/my/foo/actionName.action"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); - DefaultActionMapper mapper = new DefaultActionMapper(); - ActionMapping mapping = mapper.getMapping(req, configManager); + DefaultActionMapper mapper = new DefaultActionMapper(); + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my", mapping.getNamespace()); + assertEquals("foo/actionName", mapping.getName()); + assertNull(mapping.getMethod()); } + finally { + Settings.set(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES, old); + } - assertEquals("/my", mapping.getNamespace()); - assertEquals("foo/actionName", mapping.getName()); - assertNull(mapping.getMethod()); } public void testGetMappingWithUnknownNamespace() throws Exception { @@ -116,7 +124,7 @@ ActionMapping mapping = mapper.getMapping(req, configManager); assertEquals("", mapping.getNamespace()); - assertEquals("bo/foo/actionName", mapping.getName()); + assertEquals("actionName", mapping.getName()); assertNull(mapping.getMethod()); } @@ -199,6 +207,40 @@ assertEquals(actionMapping.getName(), "someAction"); assertEquals(actionMapping.getNamespace(), "/my"); + } + + public void testParseNameAndNamespace_NoSlashes() throws Exception { + String old = Settings.get(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES); + Settings.set(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES, "false"); + try { + ActionMapping actionMapping = new ActionMapping(); + + DefaultActionMapper defaultActionMapper = new DefaultActionMapper(); + defaultActionMapper.parseNameAndNamespace("/foo/someAction", actionMapping, config); + + assertEquals(actionMapping.getName(), "someAction"); + assertEquals(actionMapping.getNamespace(), ""); + } + finally { + Settings.set(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES, old); + } + } + + public void testParseNameAndNamespace_AllowSlashes() throws Exception { + String old = Settings.get(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES); + Settings.set(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES, "true"); + try { + ActionMapping actionMapping = new ActionMapping(); + + DefaultActionMapper defaultActionMapper = new DefaultActionMapper(); + defaultActionMapper.parseNameAndNamespace("/foo/someAction", actionMapping, config); + + assertEquals(actionMapping.getName(), "foo/someAction"); + assertEquals(actionMapping.getNamespace(), ""); + } + finally { + Settings.set(StrutsConstants.STRUTS_ENABLE_SLASHES_IN_ACTION_NAMES, old); + } }