From 4a8ff99b1cc04bc69927aa3508c64fcb9d95519e Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Sun, 2 Jun 2024 13:53:37 +0200 Subject: [PATCH] WW-5424 Fixes ClassCastException when using short var name in s:set tag --- .../org/apache/struts2/components/Set.java | 12 ++-- .../apache/struts2/views/jsp/SetTagTest.java | 58 ++++++++++++++++--- 2 files changed, 55 insertions(+), 15 deletions(-) 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 cca990ea84..4d8f7c8981 100644 --- a/core/src/main/java/org/apache/struts2/components/Set.java +++ b/core/src/main/java/org/apache/struts2/components/Set.java @@ -104,17 +104,17 @@ public boolean end(Writer writer, String body) { body=""; if (DispatcherConstants.APPLICATION.equalsIgnoreCase(scope)) { - stack.setValue("#application['" + getVar() + "']", o); + stack.setValue(String.format("#application[\"%s\"]", getVar()), o); } else if (DispatcherConstants.SESSION.equalsIgnoreCase(scope)) { - stack.setValue("#session['" + getVar() + "']", o); + stack.setValue(String.format("#session[\"%s\"]", getVar()), o); } else if (DispatcherConstants.REQUEST.equalsIgnoreCase(scope)) { - stack.setValue("#request['" + getVar() + "']", o); + stack.setValue(String.format("#request[\"%s\"]", getVar()), o); } else if (DispatcherConstants.PAGE.equalsIgnoreCase(scope)) { - stack.setValue("#attr['" + getVar() + "']", o, false); + stack.setValue(String.format("#attr[\"%s\"]", getVar()), o, false); } else { // Default scope is action. Note: The action scope handling also adds the var to the page scope. - stack.getContext().put(getVar(), o); - stack.setValue("#attr['" + getVar() + "']", o, false); + putInContext(o); + stack.setValue(String.format("#attr[\"%s\"]", getVar()), o, false); } return super.end(writer, body); 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 9e5c30eb50..f14e077355 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 @@ -22,14 +22,10 @@ import java.io.IOException; import javax.servlet.jsp.JspException; - -/** - */ public class SetTagTest extends AbstractUITagTest { - Chewbacca chewie; - SetTag tag; - + private Chewbacca chewie; + private SetTag tag; public void testApplicationScope() throws JspException { tag.setName("foo"); @@ -397,6 +393,50 @@ public void testEmptyBody_clearTagStateSet() throws JspException { strutsBodyTagsAreReflectionEqual(tag, freshTag)); } + public void testShortVarNameInPageScope() throws JspException { + tag.setName("f"); + tag.setValue("name"); + tag.setScope("page"); + + tag.doStartTag(); + tag.doEndTag(); + + assertEquals("chewie", pageContext.getAttribute("f")); + } + + public void testShortVarNameInRequestScope() throws JspException { + tag.setName("f"); + tag.setValue("name"); + tag.setScope("request"); + + tag.doStartTag(); + tag.doEndTag(); + + assertEquals("chewie", request.getAttribute("f")); + } + + public void testShortVarNameInSessionScope() throws JspException { + tag.setName("f"); + tag.setValue("name"); + tag.setScope("session"); + + tag.doStartTag(); + tag.doEndTag(); + + assertEquals("chewie", session.get("f")); + } + + public void testShortVarNameInApplicationScope() throws JspException { + tag.setName("f"); + tag.setValue("name"); + tag.setScope("application"); + + tag.doStartTag(); + tag.doEndTag(); + + assertEquals("chewie", servletContext.getAttribute("f")); + } + @Override protected void setUp() throws Exception { super.setUp(); @@ -408,9 +448,9 @@ protected void setUp() throws Exception { } - public class Chewbacca { - String name; - boolean furry; + public static class Chewbacca { + private String name; + private boolean furry; public Chewbacca(String name, boolean furry) { this.name = name;