Author: mrdon Date: Sat Feb 16 04:27:07 2008 New Revision: 628279 URL: http://svn.apache.org/viewvc?rev=628279&view=rev Log: Fixing edit where id wouldn't be set WW-2186
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java?rev=628279&r1=628278&r2=628279&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java Sat Feb 16 04:27:07 2008 @@ -121,9 +121,16 @@ 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 @@ } } 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,17 +164,18 @@ mapping.setMethod("update"); } - if (idParameterName != null) { - if (mapping.getParams() == null) { - mapping.setParams(new HashMap()); - } - mapping.getParams().put(idParameterName, id); - } } if (idParameterName != null && lastSlashPos > -1) { actionName = actionName.substring(0, lastSlashPos); } + } + + 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 Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java?rev=628279&r1=628278&r2=628279&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java Sat Feb 16 04:27:07 2008 @@ -74,6 +74,38 @@ 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/");