diff --git a/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java b/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java index 99f394f7a..00f8d31f9 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java @@ -121,9 +121,16 @@ public class Restful2ActionMapper extends DefaultActionMapper { String actionName = mapping.getName(); + int lastSlashPos = actionName.lastIndexOf('/'); + String id = null; + if (lastSlashPos > -1 && actionName != null) { + id = actionName.substring(lastSlashPos+1); + } + + // Only try something if the action name is specified if (actionName != null && actionName.length() > 0) { - int lastSlashPos = actionName.lastIndexOf('/'); + // If a method hasn't been explicitly named, try to guess using ReST-style patterns if (mapping.getMethod() == null) { @@ -140,8 +147,6 @@ public class Restful2ActionMapper extends DefaultActionMapper { } } else if (lastSlashPos > -1) { - String id = actionName.substring(lastSlashPos+1); - // Viewing the form to create a new item e.g. foo/new if (isGet(request) && "new".equals(id)) { mapping.setMethod("editNew"); @@ -159,12 +164,6 @@ public class Restful2ActionMapper extends DefaultActionMapper { mapping.setMethod("update"); } - if (idParameterName != null) { - if (mapping.getParams() == null) { - mapping.setParams(new HashMap()); - } - mapping.getParams().put(idParameterName, id); - } } if (idParameterName != null && lastSlashPos > -1) { @@ -172,6 +171,13 @@ public class Restful2ActionMapper extends DefaultActionMapper { } } + if (idParameterName != null && id != null) { + if (mapping.getParams() == null) { + mapping.setParams(new HashMap()); + } + mapping.getParams().put(idParameterName, id); + } + // Try to determine parameters from the url before the action name int actionSlashPos = actionName.lastIndexOf('/', lastSlashPos - 1); if (actionSlashPos > 0 && actionSlashPos < lastSlashPos) { diff --git a/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java b/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java index 206a5dcea..a805c7884 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java @@ -74,6 +74,38 @@ public class Restful2ActionMapperTest extends StrutsTestCase { assertEquals("index", mapping.getMethod()); } + public void testGetId() throws Exception { + mapper.setIdParameterName("id"); + req.setupGetRequestURI("/my/namespace/foo/3"); + req.setupGetServletPath("/my/namespace/foo/3"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("GET"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/3", mapping.getName()); + assertEquals("view", mapping.getMethod()); + assertEquals("3", mapping.getParams().get("id")); + } + + public void testGetEdit() throws Exception { + mapper.setIdParameterName("id"); + req.setupGetRequestURI("/my/namespace/foo/3!edit"); + req.setupGetServletPath("/my/namespace/foo/3!edit"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("GET"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/3", mapping.getName()); + assertEquals("edit", mapping.getMethod()); + assertEquals("3", mapping.getParams().get("id")); + } + public void testGetIndexWithParams() throws Exception { req.setupGetRequestURI("/my/namespace/bar/1/foo/"); req.setupGetServletPath("/my/namespace/bar/1/foo/");