From 83d3cf2df4295aeaf4140613829ba6b6b6074e45 Mon Sep 17 00:00:00 2001 From: Musachy Barroso Date: Mon, 2 Nov 2009 20:24:12 +0000 Subject: [PATCH] WW-3312 Add "includeContext" to Form tag, like the Url tag does (default to true) git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@832087 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/struts2/components/Form.java | 8 ++++- .../components/ServletUrlRenderer.java | 4 +-- .../apache/struts2/views/jsp/ui/FormTag.java | 6 ++++ core/src/site/resources/tags/form.html | 8 +++++ .../struts2/views/jsp/ui/FormTagTest.java | 29 +++++++++++++++++++ .../struts2/views/jsp/ui/Formtag-13.txt | 4 +++ .../struts2/views/jsp/ui/Formtag-14.txt | 4 +++ 7 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-13.txt create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-14.txt diff --git a/core/src/main/java/org/apache/struts2/components/Form.java b/core/src/main/java/org/apache/struts2/components/Form.java index a935c0e95..252fcebb8 100644 --- a/core/src/main/java/org/apache/struts2/components/Form.java +++ b/core/src/main/java/org/apache/struts2/components/Form.java @@ -104,8 +104,9 @@ public class Form extends ClosingUIBean { protected String portletMode; protected String windowState; protected String acceptcharset; - protected String focusElement; + protected boolean includeContext = true; + protected String focusElement; protected Configuration configuration; protected ObjectFactory objectFactory; protected UrlRenderer urlRenderer; @@ -346,4 +347,9 @@ public class Form extends ClosingUIBean { public void setFocusElement(String focusElement) { this.focusElement = focusElement; } + + @StrutsTagAttribute(description="Whether actual context should be included in URL", type="Boolean", defaultValue="true") + public void setIncludeContext(boolean includeContext) { + this.includeContext = includeContext; + } } diff --git a/core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java b/core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java index f672cbafc..1c906db6d 100644 --- a/core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java +++ b/core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java @@ -151,7 +151,7 @@ public class ServletUrlRenderer implements UrlRenderer { ActionMapping mapping = new ActionMapping(actionName, namespace, actionMethod, formComponent.parameters); String result = UrlHelper.buildUrl(formComponent.actionMapper.getUriFromActionMapping(mapping), - formComponent.request, formComponent.response, null); + formComponent.request, formComponent.response, null, null, formComponent.includeContext, true); formComponent.addParameter("action", result); // let's try to get the actual action class and name @@ -186,7 +186,7 @@ public class ServletUrlRenderer implements UrlRenderer { LOG.warn("No configuration found for the specified action: '" + actionName + "' in namespace: '" + namespace + "'. Form action defaulting to 'action' attribute's literal value."); } - String result = UrlHelper.buildUrl(action, formComponent.request, formComponent.response, null); + String result = UrlHelper.buildUrl(action, formComponent.request, formComponent.response, null, null, formComponent.includeContext, true); formComponent.addParameter("action", result); // namespace: cut out anything between the start and the last / diff --git a/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java b/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java index 8e54630ce..0a82ae43f 100644 --- a/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java +++ b/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java @@ -49,6 +49,7 @@ public class FormTag extends AbstractClosingTag { protected String windowState; protected String acceptcharset; protected String focusElement; + protected boolean includeContext = true; public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { return new Form(stack, req, res); @@ -68,6 +69,7 @@ public class FormTag extends AbstractClosingTag { form.setWindowState(windowState); form.setAcceptcharset(acceptcharset); form.setFocusElement(focusElement); + form.setIncludeContext(includeContext); } @@ -118,4 +120,8 @@ public class FormTag extends AbstractClosingTag { public void setFocusElement(String focusElement) { this.focusElement = focusElement; } + + public void setIncludeContext(boolean includeContext) { + this.includeContext = includeContext; + } } diff --git a/core/src/site/resources/tags/form.html b/core/src/site/resources/tags/form.html index 046cc0df8..803e3faa4 100644 --- a/core/src/site/resources/tags/form.html +++ b/core/src/site/resources/tags/form.html @@ -121,6 +121,14 @@ Please do not edit it directly. String HTML id attribute + + includeContext + false + true + false + Boolean + Whether actual context should be included in URL + javascriptTooltip false diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java index 523c0bd28..11f595e61 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java @@ -65,6 +65,35 @@ public class FormTagTest extends AbstractUITagTest { verify(FormTag.class.getResource("Formtag-9.txt")); } + + public void testFormWithoutContext() throws Exception { + request.setupGetContext("somecontext"); + + FormTag tag = new FormTag(); + tag.setTheme("xhtml"); + tag.setPageContext(pageContext); + tag.setAction("testAction"); + tag.setIncludeContext(false); + tag.doStartTag(); + tag.doEndTag(); + + + verify(FormTag.class.getResource("Formtag-14.txt")); + } + + public void testFormWithContext() throws Exception { + request.setupGetContext("/somecontext"); + + FormTag tag = new FormTag(); + tag.setTheme("xhtml"); + tag.setPageContext(pageContext); + tag.setAction("testAction"); + tag.doStartTag(); + tag.doEndTag(); + + + verify(FormTag.class.getResource("Formtag-13.txt")); + } public void testFormWithActionAttributeContainingBothActionAndDMIMethod() throws Exception { FormTag tag = new FormTag(); diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-13.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-13.txt new file mode 100644 index 000000000..3c74d927d --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-13.txt @@ -0,0 +1,4 @@ +
+ +
+
\ No newline at end of file diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-14.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-14.txt new file mode 100644 index 000000000..842c9d896 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-14.txt @@ -0,0 +1,4 @@ +
+ +
+
\ No newline at end of file