From 6f176cc5a7ee90efe9b3dfbe477d18c2245ea583 Mon Sep 17 00:00:00 2001 From: JCgH4164838Gh792C124B5 <43964333+JCgH4164838Gh792C124B5@users.noreply.github.com> Date: Thu, 20 Dec 2018 10:59:30 -0500 Subject: [PATCH] Enhancement for s:set tag for 2.5.x to allow better tag body whitespace control. (#296) WW-4995 Add trimBody attribute to set tag --- .../org/apache/struts2/components/Set.java | 6 +++ .../org/apache/struts2/views/jsp/SetTag.java | 14 +++++++ .../apache/struts2/views/jsp/SetTagTest.java | 38 +++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/core/src/main/java/org/apache/struts2/components/Set.java b/core/src/main/java/org/apache/struts2/components/Set.java index 65494543b..a8233b9e7 100644 --- a/core/src/main/java/org/apache/struts2/components/Set.java +++ b/core/src/main/java/org/apache/struts2/components/Set.java @@ -83,6 +83,7 @@ import com.opensymphony.xwork2.util.ValueStack; public class Set extends ContextBean { protected String scope; protected String value; + protected boolean trimBody = true; public Set(ValueStack stack) { super(stack); @@ -136,6 +137,11 @@ public class Set extends ContextBean { this.value = value; } + @StrutsTagAttribute(description="Set to false to prevent the default whitespace-trim of this tag's body content", type="Boolean", defaultValue="true") + public void setTrimBody(boolean trimBody) { + this.trimBody = trimBody; + } + @Override public boolean usesBody() { return true; diff --git a/core/src/main/java/org/apache/struts2/views/jsp/SetTag.java b/core/src/main/java/org/apache/struts2/views/jsp/SetTag.java index 161ed259a..f369bca21 100644 --- a/core/src/main/java/org/apache/struts2/views/jsp/SetTag.java +++ b/core/src/main/java/org/apache/struts2/views/jsp/SetTag.java @@ -35,6 +35,7 @@ public class SetTag extends ContextBeanTag { protected String scope; protected String value; + protected boolean trimBody = true; public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { return new Set(stack); @@ -59,4 +60,17 @@ public class SetTag extends ContextBeanTag { public void setValue(String value) { this.value = value; } + + public void setTrimBody(boolean trimBody) { + this.trimBody = trimBody; + } + + @Override + protected String getBody() { + if (trimBody) { + return super.getBody(); + } else { + return (bodyContent == null ? "" : bodyContent.getString()); + } + } } diff --git a/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java index e5eba5e47..16e56f24b 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java @@ -18,6 +18,8 @@ */ package org.apache.struts2.views.jsp; +import com.mockobjects.servlet.MockJspWriter; +import java.io.IOException; import javax.servlet.jsp.JspException; @@ -83,6 +85,42 @@ public class SetTagTest extends AbstractUITagTest { assertEquals(chewie, context.get("chewie")); } + public void testSetTrimBody() throws JspException, IOException { + final String beginEndSpaceString = " Preceding and trailing spaces. "; + final String trimmedBeginEndSpaceString = beginEndSpaceString.trim(); + StrutsMockBodyContent mockBodyContent; + + tag.setName("foo"); + tag.setValue(null); + // Do not set any value - default for tag should be true + mockBodyContent = new StrutsMockBodyContent(new MockJspWriter()); + mockBodyContent.setString(beginEndSpaceString); + tag.setBodyContent(mockBodyContent); + tag.doStartTag(); + tag.doEndTag(); + assertEquals(trimmedBeginEndSpaceString, context.get("foo")); + + tag.setName("foo"); + tag.setValue(null); + tag.setTrimBody(true); + mockBodyContent = new StrutsMockBodyContent(new MockJspWriter()); + mockBodyContent.setString(beginEndSpaceString); + tag.setBodyContent(mockBodyContent); + tag.doStartTag(); + tag.doEndTag(); + assertEquals(trimmedBeginEndSpaceString, context.get("foo")); + + tag.setName("foo"); + tag.setValue(null); + tag.setTrimBody(false); + mockBodyContent = new StrutsMockBodyContent(new MockJspWriter()); + mockBodyContent.setString(beginEndSpaceString); + tag.setBodyContent(mockBodyContent); + tag.doStartTag(); + tag.doEndTag(); + assertEquals(beginEndSpaceString, context.get("foo")); + } + protected void setUp() throws Exception { super.setUp();