Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WW-5424] Fixes ClassCastException when using short var name in s:set tag #946

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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