Prepend context/servlet path to FormTag action

The Form tag now fills in the context and servlet path if not present
in the specified action.

Issue: SPR-8684
This commit is contained in:
Rossen Stoyanchev
2013-01-14 18:19:44 -05:00
parent ad91fa63fa
commit ad025b59c5
3 changed files with 56 additions and 6 deletions

View File

@@ -36,6 +36,7 @@ import org.springframework.ui.context.ThemeSource;
import org.springframework.ui.context.support.ResourceBundleThemeSource;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
@@ -422,14 +423,14 @@ public class RequestContext {
* context path and the servlet path of the original request. This is useful
* for building links to other resources within the application where a
* servlet mapping of the style {@code "/main/*"} is used.
* <p>Delegates to the UrlPathHelper for decoding the context path.
* @see javax.servlet.http.HttpServletRequest#getContextPath
* @see javax.servlet.http.HttpServletRequest#getServletPath()
* @see #getUrlPathHelper
* Delegates to the UrlPathHelper to determine the context and servlet path.
*/
public String getPathToServlet() {
return this.urlPathHelper.getOriginatingContextPath(this.request)
+ this.urlPathHelper.getOriginatingServletPath(this.request);
String path = this.urlPathHelper.getOriginatingContextPath(this.request);
if (StringUtils.hasText(this.urlPathHelper.getPathWithinServletMapping(this.request))) {
path += this.urlPathHelper.getOriginatingServletPath(this.request);
}
return path;
}
/**

View File

@@ -32,6 +32,7 @@ import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.support.RequestDataValueProcessor;
import org.springframework.web.util.HtmlUtils;
import org.springframework.web.util.UrlPathHelper;
/**
* Databinding-aware JSP tag for rendering an HTML '{@code form}' whose
@@ -411,6 +412,10 @@ public class FormTag extends AbstractHtmlElementTag {
protected String resolveAction() throws JspException {
String action = getAction();
if (StringUtils.hasText(action)) {
String pathToServlet = getRequestContext().getPathToServlet();
if (action.startsWith("/") && !action.startsWith(getRequestContext().getContextPath())) {
action = pathToServlet + action;
}
action = getDisplayString(evaluate(ACTION_ATTRIBUTE, action));
return processAction(action);
}