From 62e78be60f604b153a38506bbc823ed02ead067f Mon Sep 17 00:00:00 2001 From: John Lindal Date: Mon, 31 Jan 2011 20:16:38 +0000 Subject: [PATCH] WW-3498 Inject values into top of stack (model or action) instead of always using action. This does not break backward compatibility because nobody could use a model before this patch. git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1065743 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/struts2/json/JSONInterceptor.java | 10 +++++++--- .../org/apache/struts2/json/JSONInterceptorTest.java | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java index aa713d840..2a6272d69 100644 --- a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java +++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java @@ -83,7 +83,7 @@ public class JSONInterceptor extends AbstractInterceptor { contentType = contentType.substring(0, iSemicolonIdx); } - Object rootObject; + Object rootObject = null; if (this.root != null) { ValueStack stack = invocation.getStack(); rootObject = stack.findValue(this.root); @@ -91,8 +91,6 @@ public class JSONInterceptor extends AbstractInterceptor { if (rootObject == null) { throw new RuntimeException("Invalid root expression: '" + this.root + "'."); } - } else { - rootObject = invocation.getAction(); } if ((contentType != null) && contentType.equalsIgnoreCase("application/json")) { @@ -106,6 +104,9 @@ public class JSONInterceptor extends AbstractInterceptor { if (dataCleaner != null) dataCleaner.clean("", json); + if (rootObject == null) // model overrides action + rootObject = invocation.getStack().peek(); + // populate fields populator.populateObject(rootObject, json); } else { @@ -121,6 +122,9 @@ public class JSONInterceptor extends AbstractInterceptor { if (obj instanceof Map) { Map smd = (Map) obj; + if (rootObject == null) // model makes no sense when using RPC + rootObject = invocation.getAction(); + // invoke method try { result = this.invoke(rootObject, smd); diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java index 86557ba53..5eee747a8 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java @@ -322,6 +322,7 @@ public class JSONInterceptorTest extends StrutsTestCase { TestAction action = new TestAction(); this.invocation.setAction(action); + this.invocation.getStack().push(action); interceptor.intercept(this.invocation);