mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Fixing edit where id wouldn't be set
WW-2186 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@628279 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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/");
|
||||
|
||||
Reference in New Issue
Block a user