Skip to content

Commit c0cacfc

Browse files
committed
Add servletRelativeAction form tag attribute
A recent change in FormTag to prepend the context and servlet paths if not present, causes issues when used in portlet applications. This change introduces a servletRelativeAction form tag attribute that must be used for the context and servlet paths to be prepended. Issue: SPR-10382
1 parent aaded7e commit c0cacfc

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/FormTag.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.servlet.tags.form;
1818

1919
import java.util.Map;
20+
2021
import javax.servlet.ServletRequest;
2122
import javax.servlet.ServletResponse;
2223
import javax.servlet.http.HttpServletRequest;
@@ -104,6 +105,8 @@ public class FormTag extends AbstractHtmlElementTag {
104105

105106
private String action;
106107

108+
private String servletRelativeAction;
109+
107110
private String method = DEFAULT_METHOD;
108111

109112
private String target;
@@ -189,6 +192,21 @@ protected String getAction() {
189192
return this.action;
190193
}
191194

195+
/**
196+
* Set the value of the '{@code action}' attribute.
197+
* <p>May be a runtime expression.
198+
*/
199+
public void setServletRelativeAction(String servletRelativeaction) {
200+
this.servletRelativeAction = (servletRelativeaction != null ? servletRelativeaction : "");
201+
}
202+
203+
/**
204+
* Get the value of the '{@code action}' attribute.
205+
*/
206+
protected String getServletRelativeAction() {
207+
return this.servletRelativeAction;
208+
}
209+
192210
/**
193211
* Set the value of the '{@code method}' attribute.
194212
* <p>May be a runtime expression.
@@ -395,22 +413,30 @@ protected String resolveModelAttribute() throws JspException {
395413

396414
/**
397415
* Resolve the value of the '{@code action}' attribute.
398-
* <p>If the user configured an '{@code action}' value then
399-
* the result of evaluating this value is used. Otherwise, the
400-
* {@link org.springframework.web.servlet.support.RequestContext#getRequestUri() originating URI}
401-
* is used.
416+
* <p>If the user configured an '{@code action}' value then the result of
417+
* evaluating this value is used. If the user configured an
418+
* '{@code servletRelativeAction}' value then the value is prepended
419+
* with the context and servlet paths, and the result is used. Otherwise, the
420+
* {@link org.springframework.web.servlet.support.RequestContext#getRequestUri()
421+
* originating URI} is used.
422+
*
402423
* @return the value that is to be used for the '{@code action}' attribute
403424
*/
404425
protected String resolveAction() throws JspException {
405426
String action = getAction();
427+
String servletRelativeAction = getServletRelativeAction();
406428
if (StringUtils.hasText(action)) {
407-
String pathToServlet = getRequestContext().getPathToServlet();
408-
if (action.startsWith("/") && !action.startsWith(getRequestContext().getContextPath())) {
409-
action = pathToServlet + action;
410-
}
411429
action = getDisplayString(evaluate(ACTION_ATTRIBUTE, action));
412430
return processAction(action);
413431
}
432+
else if (StringUtils.hasText(servletRelativeAction)) {
433+
String pathToServlet = getRequestContext().getPathToServlet();
434+
if (servletRelativeAction.startsWith("/") && !servletRelativeAction.startsWith(getRequestContext().getContextPath())) {
435+
servletRelativeAction = pathToServlet + servletRelativeAction;
436+
}
437+
servletRelativeAction = getDisplayString(evaluate(ACTION_ATTRIBUTE, servletRelativeAction));
438+
return processAction(servletRelativeAction);
439+
}
414440
else {
415441
String requestUri = getRequestContext().getRequestUri();
416442
ServletResponse response = this.pageContext.getResponse();

spring-webmvc/src/main/resources/META-INF/spring-form.tld

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@
142142
<required>false</required>
143143
<rtexprvalue>true</rtexprvalue>
144144
</attribute>
145+
<attribute>
146+
<description>HTML Required Attribute</description>
147+
<name>servletRelativeAction</name>
148+
<required>false</required>
149+
<rtexprvalue>true</rtexprvalue>
150+
</attribute>
145151
<attribute>
146152
<description>HTML Optional Attribute</description>
147153
<name>method</name>

spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void testPrependServletPath() throws Exception {
179179
String onreset = "onreset";
180180

181181
this.tag.setCommandName(commandName);
182-
this.tag.setAction(action);
182+
this.tag.setServletRelativeAction(action);
183183
this.tag.setMethod(method);
184184
this.tag.setEnctype(enctype);
185185
this.tag.setOnsubmit(onsubmit);

0 commit comments

Comments
 (0)