Skip to content

Commit

Permalink
Merge pull request #946 from apache/fix/WW-5424-class-cast-exception
Browse files Browse the repository at this point in the history
[WW-5424] Fixes ClassCastException when using short var name in s:set tag
  • Loading branch information
lukaszlenart authored Jun 5, 2024
2 parents ee040ba + 4a8ff99 commit 0cdce05
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
12 changes: 6 additions & 6 deletions core/src/main/java/org/apache/struts2/components/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
58 changes: 49 additions & 9 deletions core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit 0cdce05

Please sign in to comment.