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-5168] Support submit unchecked in Javatemplates and fixes logic for FTL template #531

Merged
merged 4 commits into from
Feb 20, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ protected void evaluateExtraParams() {
}

if (submitUnchecked != null) {
addParameter("submitUnchecked", findValue(submitUnchecked, Boolean.class));
Object parsedValue = findValue(submitUnchecked, Boolean.class);
addParameter("submitUnchecked", parsedValue == null ? Boolean.valueOf(submitUnchecked) : parsedValue);
} else {
addParameter("submitUnchecked", false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ protected Object findValue(String expression, Class<?> toType) {
return expression;
}
} else {
expression = stripExpression(expression);

return getStack().findValue(expression, toType, throwExceptionOnELFailure);
String strippedExpression = stripExpression(expression);
return getStack().findValue(strippedExpression, toType, throwExceptionOnELFailure);
}
}

Expand Down
12 changes: 7 additions & 5 deletions core/src/main/java/org/apache/struts2/components/UIBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,13 @@ public void evaluateParams() {
}

if (requiredLabel != null) {
addParameter("required", findValue(requiredLabel, Boolean.class));
Object parsedValue = findValue(requiredLabel, Boolean.class);
addParameter("required", parsedValue == null ? Boolean.valueOf(requiredLabel) : parsedValue);
}

if (disabled != null) {
addParameter("disabled", findValue(disabled, Boolean.class));
Object parsedValue = findValue(disabled, Boolean.class);
addParameter("disabled", parsedValue == null ? Boolean.valueOf(disabled) : parsedValue);
}

if (tabindex != null) {
Expand Down Expand Up @@ -886,9 +888,9 @@ public void evaluateParams() {
this.addParameter("tooltipDelay", findString(this.tooltipDelay));

if (this.javascriptTooltip != null) {
Boolean jsTooltips = (Boolean) findValue(this.javascriptTooltip, Boolean.class);
Object jsTooltips = findValue(this.javascriptTooltip, Boolean.class);
//TODO use a Boolean model when tooltipConfig is dropped
this.addParameter("jsTooltipEnabled", jsTooltips.toString());
this.addParameter("jsTooltipEnabled", jsTooltips == null ? this.javascriptTooltip : jsTooltips.toString());

if (form != null)
form.addParameter("hasTooltip", jsTooltips);
Expand Down Expand Up @@ -968,7 +970,7 @@ protected Map<String, String> getTooltipConfig(UIBean component) {
// 1] UI component's tooltipConfig attribute OR
// 2] <param name="tooltip" value="" /> param tag value attribute

result = new LinkedHashMap<>((Map) tooltipConfigObj);
result = new LinkedHashMap<String, String>((Map) tooltipConfigObj);
} else if (tooltipConfigObj instanceof String) {

// we get this if its configured using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
public class Attributes extends LinkedHashMap<String, String> {

private static final long serialVersionUID = 4103241472140545630L;
private static final long serialVersionUID = 4103241472140545630L;

public Attributes add(String key, String value) {
return add(key, value, true);
Expand Down Expand Up @@ -74,15 +74,17 @@ public Attributes addIfExists(String attrName, Object paramValue, boolean encode
* @return this
*/
public Attributes addIfTrue(String attrName, Object paramValue) {
if (paramValue != null) {
if ((paramValue instanceof Boolean && ((Boolean) paramValue).booleanValue()) ||
(Boolean.valueOf(paramValue.toString()).booleanValue())) {
put(attrName, attrName);
}
if (paramValue != null && isTrue(paramValue)) {
put(attrName, attrName);
}
return this;
}

private boolean isTrue(Object paramValue) {
return (paramValue instanceof Boolean && (Boolean) paramValue)
|| (Boolean.parseBoolean(paramValue.toString()));
}

/**
* Add a key/value pair to the attributes, if the value is null, it will be set as an empty string.
* Value is html encoded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public void generate() throws IOException {
Attributes attrs = new Attributes();

attrs.addIfExists("name", params.get("name"))
.addIfExists("id", params.get("id"))
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"))
.addIfExists("href", params.get("href"), false)
.addIfExists("title", params.get("title"))
.addIfExists("tabindex", params.get("tabindex"));
.addIfExists("id", params.get("id"))
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"))
.addIfExists("href", params.get("href"), false)
.addIfTrue("disabled", params.get("disabled"))
.addIfExists("title", params.get("title"))
.addIfExists("tabindex", params.get("tabindex"));

start("a", attrs);
String body = (String) params.get("body");
Boolean escapeHtmlBody = (Boolean) params.get("escapeHtmlBody");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
*/
package org.apache.struts2.views.java.simple;

import org.apache.struts2.views.java.Attributes;
import org.apache.struts2.views.java.TagGenerator;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.struts2.views.java.Attributes;
import org.apache.struts2.views.java.TagGenerator;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

public class CheckboxHandler extends AbstractTagHandler implements TagGenerator {
public void generate() throws IOException {
Expand All @@ -35,29 +36,33 @@ public void generate() throws IOException {
String id = (String) params.get("id");
String name = (String) params.get("name");
Object disabled = params.get("disabled");
boolean submitUnchecked = Boolean.parseBoolean(Objects.toString(params.get("submitUnchecked"), "false"));

attrs.add("type", "checkbox")
.add("name", name)
.add("value", fieldValue)
.addIfTrue("checked", params.get("nameValue"))
.addIfTrue("readonly", params.get("readonly"))
.addIfTrue("disabled", disabled)
.addIfExists("tabindex", params.get("tabindex"))
.addIfExists("id", id)
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"))
.addIfExists("title", params.get("title"));
.add("name", name)
.add("value", fieldValue)
.addIfTrue("checked", params.get("nameValue"))
.addIfTrue("readonly", params.get("readonly"))
.addIfTrue("disabled", disabled)
.addIfExists("tabindex", params.get("tabindex"))
.addIfExists("id", id)
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"))
.addIfExists("title", params.get("title"));
start("input", attrs);
end("input");

//hidden input
attrs = new Attributes();
attrs.add("type", "hidden")
if (submitUnchecked) {
//hidden input
attrs = new Attributes();

attrs.add("type", "hidden")
.add("id", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(id)))
.add("name", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(name)))
.add("value", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(fieldValue)))
.addIfTrue("disabled", disabled);
start("input", attrs);
end("input");
start("input", attrs);
end("input");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ public void generate() throws IOException {
.addIfExists("referrerpolicy", params.get("referrerpolicy"))
.addIfExists("type", params.get("type"))
.addIfExists("as", params.get("as"))
.addIfExists("disabled", params.get("disabled"))
.addIfExists("title", params.get("title"));

// see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-disabled
if ("stylesheet".equals(params.get("rel"))) {
attrs.addIfTrue("disabled", params.get("disabled"));
}

start("link", attrs);
end("link");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.apache.struts2.views.java.simple;

import org.apache.struts2.views.java.TagGenerator;
import org.apache.struts2.views.java.Attributes;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.views.java.Attributes;
import org.apache.struts2.views.java.TagGenerator;

import java.io.IOException;
import java.util.Map;
Expand All @@ -34,12 +34,13 @@ public void generate() throws IOException {
boolean isButton = "button".equals(params.get("type"));

attrs.addDefaultToEmpty("name", params.get("name"))
.add("type", "reset")
.addIfExists("value", params.get("nameValue"))
.addIfExists("tabindex", params.get("tabindex"))
.addIfExists("id", params.get("id"))
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"));
.add("type", "reset")
.addIfExists("value", params.get("nameValue"))
.addIfTrue("disabled", params.get("disabled"))
.addIfExists("tabindex", params.get("tabindex"))
.addIfExists("id", params.get("id"))
.addIfExists("class", params.get("cssClass"))
.addIfExists("style", params.get("cssStyle"));

if (!isButton)
attrs.addIfExists("title", params.get("title"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testRenderAnchor() {
theme.renderTag(getTagName(), context);
theme.renderTag(getTagName() + "-close", context);
String output = writer.getBuffer().toString();
String expected = s("<a name='name_' id='id_' class='class' style='style' href='http://sometest.com?ab=10' title='title' tabindex='1'></a>");
String expected = s("<a name='name_' id='id_' class='class' style='style' href='http://sometest.com?ab=10' disabled='disabled' title='title' tabindex='1'></a>");
assertEquals(expected, output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ public class CheckboxTest extends AbstractCommonAttributesTest {
private Checkbox tag;

public void testRenderCheckbox() {
tag.setName("name_");
tag.setDisabled("false");
tag.setTabindex("1");
tag.setId("id_");
tag.setCssClass("class");
tag.setCssStyle("style");
tag.setTitle("title");
tag.setFieldValue("xyz");

tag.evaluateParams();
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input type='checkbox' name='name_' value='xyz' tabindex='1' id='id_' class='class' style='style' title='title'></input>");
assertEquals(expected, output);
}

public void testRenderUncheckCheckbox() {
tag.setName("name_");
tag.setDisabled("true");
tag.setTabindex("1");
Expand All @@ -36,25 +54,25 @@ public void testRenderCheckbox() {
tag.setCssStyle("style");
tag.setTitle("title");
tag.setFieldValue("xyz");
tag.setSubmitUnchecked("true");

tag.evaluateParams();
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input type='checkbox' name='name_' value='xyz' tabindex='1' id='id_' class='class' style='style' title='title'></input><input type='hidden' id='__checkbox_id_' name='__checkbox_name_' value='__checkbox_xyz'></input>");
String expected = s("<input type='checkbox' name='name_' value='xyz' disabled='disabled' tabindex='1' id='id_' class='class' style='style' title='title'></input><input type='hidden' id='__checkbox_id_' name='__checkbox_name_' value='__checkbox_xyz' disabled='disabled'></input>");
assertEquals(expected, output);
}

public void testRenderCheckboxWithNameValue() {
tag.setName("name_");
tag.setValue("%{someValue}");
tag.setDisabled("true");

tag.evaluateParams();
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input type='checkbox' name='name_' value='true' checked='checked' id='name_'></input><input type='hidden' id='__checkbox_name_' name='__checkbox_name_' value='__checkbox_true'></input>");
String expected = s("<input type='checkbox' name='name_' value='true' checked='checked' id='name_'></input>");
assertEquals(expected, output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testRenderTextField() {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input name='name' type='file' size='10' value='val1' accept='accept_' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
String expected = s("<input name='name' type='file' size='10' value='val1' disabled='disabled' accept='accept_' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
assertEquals(expected, output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testRenderHidden() {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input name='name' type='hidden' value='val1' id='id1' class='class1' style='style1'></input>");
String expected = s("<input name='name' type='hidden' value='val1' disabled='disabled' id='id1' class='class1' style='style1'></input>");
assertEquals(expected, output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class LinkTest extends AbstractTest{

private static final String NONCE_VAL = "r4andom";

public void testRenderScriptTag() {
public void testRenderLinkTag() {
tag.setHref("testhref");
tag.setHreflang("test");
tag.setRel("module");
Expand All @@ -41,27 +41,60 @@ public void testRenderScriptTag() {
tag.setCrossorigin("same-origin");
tag.setType("anonymous");
tag.setAs("test");
tag.setDisabled("disabled_");
tag.setDisabled("true");
tag.setTitle("test");

tag.evaluateParams();
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String s = writer.getBuffer().toString();

assertTrue("Incorrect href attribute for link tag", s.contains("href=\"testhref\""));
assertTrue("Incorrect hreflang attribute for link tag", s.contains("hreflang=\"test\""));
assertTrue("Incorrect rel attribute for link tag", s.contains("rel=\"module\""));
assertTrue("Incorrect media attribute for link tag", s.contains("media=\"foo\""));
assertTrue("Incorrect referrerpolicy attribute for link tag", s.contains("referrerpolicy=\"test\""));
assertTrue("Incorrect sizes attribute for link tag", s.contains("sizes=\"foo\""));
assertTrue("Incorrect crossorigin attribute for link tag", s.contains("crossorigin=\"same-origin\""));
assertTrue("Incorrect type attribute for link tag", s.contains("type=\"anonymous\""));
assertTrue("Incorrect as attribute for link tag", s.contains("as=\"test\""));
assertTrue("Non-existent disabled attribute for link tag", s.contains("disabled=\"disabled_\""));
assertTrue("Incorrect title attribute for link tag", s.contains("title=\"test\""));
assertTrue("Incorrect nonce attribute for link tag", s.contains("nonce=\"" + NONCE_VAL+"\""));
String output = writer.getBuffer().toString();

assertTrue("Incorrect href attribute for link tag", output.contains(s("href='testhref'")));
assertTrue("Incorrect hreflang attribute for link tag", output.contains(s("hreflang='test'")));
assertTrue("Incorrect rel attribute for link tag", output.contains(s("rel='module'")));
assertTrue("Incorrect media attribute for link tag", output.contains(s("media='foo'")));
assertTrue("Incorrect referrerpolicy attribute for link tag", output.contains(s("referrerpolicy='test'")));
assertTrue("Incorrect sizes attribute for link tag", output.contains(s("sizes='foo'")));
assertTrue("Incorrect crossorigin attribute for link tag", output.contains(s("crossorigin='same-origin'")));
assertTrue("Incorrect type attribute for link tag", output.contains(s("type='anonymous'")));
assertTrue("Incorrect as attribute for link tag", output.contains(s("as='test'")));
assertFalse("Non-existent disabled attribute for link tag", output.contains(s("disabled='disabled'")));
assertTrue("Incorrect title attribute for link tag", output.contains(s("title='test'")));
assertTrue("Incorrect nonce attribute for link tag", output.contains(s("nonce='" + NONCE_VAL+"'")));
}

public void testRenderLinkTagAsStylesheet() {
tag.setHref("testhref");
tag.setHreflang("test");
tag.setRel("stylesheet");
tag.setMedia("foo");
tag.setReferrerpolicy("test");
tag.setSizes("foo");
tag.setCrossorigin("same-origin");
tag.setType("anonymous");
tag.setAs("test");
tag.setDisabled("true");
tag.setTitle("test");

tag.evaluateParams();
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();

assertTrue("Incorrect href attribute for link tag", output.contains(s("href='testhref'")));
assertTrue("Incorrect hreflang attribute for link tag", output.contains(s("hreflang='test'")));
assertTrue("Incorrect rel attribute for link tag", output.contains(s("rel='stylesheet'")));
assertTrue("Incorrect media attribute for link tag", output.contains(s("media='foo'")));
assertTrue("Incorrect referrerpolicy attribute for link tag", output.contains(s("referrerpolicy='test'")));
assertTrue("Incorrect sizes attribute for link tag", output.contains(s("sizes='foo'")));
assertTrue("Incorrect crossorigin attribute for link tag", output.contains(s("crossorigin='same-origin'")));
assertTrue("Incorrect type attribute for link tag", output.contains(s("type='anonymous'")));
assertTrue("Incorrect as attribute for link tag", output.contains(s("as='test'")));
assertTrue("Incorrect disabled attribute for link tag", output.contains(s("disabled='disabled'")));
assertTrue("Incorrect title attribute for link tag", output.contains(s("title='test'")));
assertTrue("Incorrect nonce attribute for link tag", output.contains(s("nonce='" + NONCE_VAL+"'")));
}

@Override
protected UIBean getUIBean() throws Exception {
return tag;
Expand Down
Loading