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
This commit is contained in:
JCgH4164838Gh792C124B5
2018-12-20 10:59:30 -05:00
committed by Aleksandr Mashchenko
parent db085dbeef
commit 6f176cc5a7
3 changed files with 58 additions and 0 deletions
@@ -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;
@@ -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());
}
}
}
@@ -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();