diff --git a/core/src/main/java/org/apache/struts2/dispatcher/HttpParameters.java b/core/src/main/java/org/apache/struts2/dispatcher/HttpParameters.java index 4b3570576a..a85c9c762f 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/HttpParameters.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/HttpParameters.java @@ -57,10 +57,10 @@ public HttpParameters clone(Map newParams) { return HttpParameters.createEmpty().withParent(this).withExtraParams(newParams).build(); } - public Map toMap() { - Map result = new HashMap<>(parameters.size()); + public Map toMap() { + Map result = new HashMap<>(parameters.size()); for (Map.Entry entry : parameters.entrySet()) { - result.put(entry.getKey(), entry.getValue().getObject()); + result.put(entry.getKey(), entry.getValue().getMultipleValues()); } return result; } diff --git a/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java b/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java new file mode 100644 index 0000000000..854f85b64e --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java @@ -0,0 +1,47 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.interceptor; + +import org.apache.struts2.dispatcher.HttpParameters; + + +/** + *

+ * This interface gives actions an alternative way of receiving input parameters. The parameters will + * contain all input parameters as implementation of {@link org.apache.struts2.dispatcher.Parameter}. + * Actions that need this should simply implement it. + *

+ * + *

+ * One common use for this is to have the action propagate parameters to internally instantiated data + * objects. + *

+ */ +public interface HttpParametersAware { + + /** + * Sets the HTTP parameters in the implementing class. + * + * @param parameters an instance of {@link HttpParameters}. + */ + void setParameters(HttpParameters parameters); +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java b/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java index b37c0c6015..755efbb74f 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java @@ -21,11 +21,8 @@ package org.apache.struts2.interceptor; -import org.apache.struts2.dispatcher.HttpParameters; - import java.util.Map; - /** *

* This interface gives actions an alternative way of receiving input parameters. The map will @@ -41,7 +38,10 @@ * Note that all parameter values for a given name will be returned, so the type of the objects in * the map is java.lang.String[]. *

+ * + * @deprecated please use {@link HttpParametersAware} instead */ +@Deprecated public interface ParameterAware { /** @@ -49,5 +49,5 @@ public interface ParameterAware { * * @param parameters a Map of parameters (name/value Strings). */ - public void setParameters(HttpParameters parameters); + public void setParameters(Map parameters); } diff --git a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java index 4542d437c8..c92d4828e3 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java @@ -57,6 +57,8 @@ * *
  • {@link ParameterAware}
  • * + *
  • {@link HttpParametersAware}
  • + * *
  • {@link RequestAware}
  • * *
  • {@link SessionAware}
  • @@ -135,7 +137,11 @@ public String intercept(ActionInvocation invocation) throws Exception { } if (action instanceof ParameterAware) { - ((ParameterAware) action).setParameters(context.getParameters()); + ((ParameterAware) action).setParameters(context.getParameters().toMap()); + } + + if (action instanceof HttpParametersAware) { + ((HttpParametersAware) action).setParameters(context.getParameters()); } if (action instanceof ApplicationAware) { diff --git a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java index 244d5f3fce..1320b6043b 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java @@ -84,7 +84,23 @@ public void testServletResponseAware() throws Exception { } public void testParameterAware() throws Exception { - ParameterAware mock = (ParameterAware) createMock(ParameterAware.class); + ParameterAware mock = createMock(ParameterAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + HttpParameters param = HttpParameters.createEmpty().build(); + mai.getInvocationContext().setParameters(param); + + mock.setParameters(param.toMap()); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + + public void testHttpParametersAware() throws Exception { + HttpParametersAware mock = createMock(HttpParametersAware.class); MockActionInvocation mai = createActionInvocation(mock);