Updated Branches: refs/heads/master 763264238 -> b0dee67bf
Copy changes from checkboxlist and refactor changes to match existing code. See WW-4181. Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/f3c54b90 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/f3c54b90 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/f3c54b90 Branch: refs/heads/master Commit: f3c54b90ac5e63cd403c256ad10312fc2eb753cb Parents: 7632642 Author: Greg Huber <[email protected]> Authored: Fri Jan 31 13:45:03 2014 +0000 Committer: Greg Huber <[email protected]> Committed: Fri Jan 31 13:45:03 2014 +0000 ---------------------------------------------------------------------- .../views/java/simple/CheckboxListHandler.java | 80 ++++++++++++-------- .../struts2/views/java/simple/RadioHandler.java | 70 +++++++++++------ 2 files changed, 96 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/f3c54b90/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxListHandler.java ---------------------------------------------------------------------- diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxListHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxListHandler.java index 478e6d1..f44746d 100644 --- a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxListHandler.java +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxListHandler.java @@ -22,6 +22,7 @@ package org.apache.struts2.views.java.simple; import com.opensymphony.xwork2.util.ValueStack; + import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.apache.struts2.util.MakeIterator; @@ -35,9 +36,9 @@ import java.util.Map; public class CheckboxListHandler extends AbstractTagHandler implements TagGenerator { public void generate() throws IOException { - Map<String, Object> params = context.getParameters(); + Map<String, Object> params = context.getParameters(); - //Get parameters + // Get parameters Object listObj = params.get("list"); String listKey = (String) params.get("listKey"); String listValue = (String) params.get("listValue"); @@ -47,7 +48,7 @@ public class CheckboxListHandler extends AbstractTagHandler implements TagGenera int cnt = 1; - //This will interate through all lists + // This will interate through all lists ValueStack stack = this.context.getStack(); if (listObj != null) { Iterator itt = MakeIterator.convert(listObj); @@ -55,44 +56,39 @@ public class CheckboxListHandler extends AbstractTagHandler implements TagGenera Object item = itt.next(); stack.push(item); - //key + // key Object itemKey = findValue(listKey != null ? listKey : "top"); String itemKeyStr = StringUtils.defaultString(itemKey == null ? null : itemKey.toString()); - //value + // value Object itemValue = findValue(listValue != null ? listValue : "top"); String itemValueStr = StringUtils.defaultString(itemValue == null ? null : itemValue.toString()); - //Checkbox button section + // Checkbox button section Attributes a = new Attributes(); - a.add("type", "checkbox") - .add("name", name) - .add("value", itemKeyStr) - .addIfTrue("checked", isChecked(params, itemKeyStr)) - .addIfTrue("readonly", params.get("readonly")) - .addIfTrue("disabled", disabled) - .addIfExists("tabindex", params.get("tabindex")) - .addIfExists("id", id + "-" + Integer.toString(cnt)); - start("input", a); - end("input"); - - //Label section + a.add("type", "checkbox").add("name", name).add("value", itemKeyStr) + .addIfTrue("checked", isChecked(params, itemKeyStr)) + .addIfTrue("readonly", params.get("readonly")).addIfTrue("disabled", disabled) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", id + "-" + Integer.toString(cnt)); + start("input", a); + end("input"); + + // Label section a = new Attributes(); - a.add("for",id + "-" + Integer.toString(cnt)) - .addIfExists("class", params.get("cssClass")) - .addIfExists("style", params.get("cssStyle")); + a.add("for", id + "-" + Integer.toString(cnt)).addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")); super.start("label", a); if (StringUtils.isNotEmpty(itemValueStr)) characters(itemValueStr); super.end("label"); - //Hidden input section + // Hidden input section a = new Attributes(); a.add("type", "hidden") .add("id", "__multiselect_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(id))) .add("name", "__multiselect_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(name))) - .add("value", "") - .addIfTrue("disabled", disabled); + .add("value", "").addIfTrue("disabled", disabled); start("input", a); end("input"); @@ -102,16 +98,38 @@ public class CheckboxListHandler extends AbstractTagHandler implements TagGenera } } + /** + * It's set to true if the nameValue (the value associated with the name + * which is typically set in the action is equal to the current key value. + * + * @param params + * the params + * + * @param itemKeyStr + * the item key str + * + * @return the boolean + */ private Boolean isChecked(Map<String, Object> params, String itemKeyStr) { Boolean checked = false; if (itemKeyStr != null) { - String[] nameValues = (String[]) params.get("nameValue"); - if (nameValues != null) { - for (String value : nameValues) { - if (checked = value.equalsIgnoreCase(itemKeyStr)) { - break; - } - } + + // NameValue are the values that is provided by the name property + // in the action + Object nameValue = params.get("nameValue"); + + if (nameValue != null) { + + Iterator itt = MakeIterator.convert(nameValue); + while (itt.hasNext()) { + + String value = (String) itt.next(); + if (checked = value.equalsIgnoreCase(itemKeyStr)) { + break; + } + + } + } } return checked; http://git-wip-us.apache.org/repos/asf/struts/blob/f3c54b90/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/RadioHandler.java ---------------------------------------------------------------------- diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/RadioHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/RadioHandler.java index 9ee3010..756abdc 100644 --- a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/RadioHandler.java +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/RadioHandler.java @@ -40,8 +40,6 @@ public class RadioHandler extends AbstractTagHandler implements TagGenerator { Object listObj = params.get("list"); String listKey = (String) params.get("listKey"); String listValue = (String) params.get("listValue"); - // NameValue is the value that is provided by the name property in the action - Object nameValue = params.get("nameValue"); int cnt = 0; ValueStack stack = this.context.getStack(); @@ -51,40 +49,28 @@ public class RadioHandler extends AbstractTagHandler implements TagGenerator { Object item = itt.next(); stack.push(item); - //key + // key Object itemKey = findValue(listKey != null ? listKey : "top"); String itemKeyStr = StringUtils.defaultString(itemKey == null ? null : itemKey.toString()); - //value + // value Object itemValue = findValue(listValue != null ? listValue : "top"); String itemValueStr = StringUtils.defaultString(itemValue == null ? null : itemValue.toString()); - // nameValue needs to cast to a string from object - String itemNameValueStr = (nameValue == null ? null : nameValue.toString()); - - //Checked value. It's set to true if the nameValue (the value associated with the name which is typically set in - //the action is equal to the current key value. - Boolean checked = itemKeyStr != null && itemNameValueStr != null && itemNameValueStr.equals(itemKeyStr); - - //Radio button section + // Radio button section String id = params.get("id") + Integer.toString(cnt++); Attributes a = new Attributes(); - a.add("type", "radio") - .addDefaultToEmpty("name", params.get("name")) - .addIfTrue("checked", checked) - .addIfExists("value", itemKeyStr) - .addIfTrue("disabled", params.get("disabled")) - .addIfExists("tabindex", params.get("tabindex")) + a.add("type", "radio").addDefaultToEmpty("name", params.get("name")) + .addIfTrue("checked", isChecked(params, itemKeyStr)).addIfExists("value", itemKeyStr) + .addIfTrue("disabled", params.get("disabled")).addIfExists("tabindex", params.get("tabindex")) .addIfExists("id", id); super.start("input", a); super.end("input"); - //Label section + // Label section a = new Attributes(); - a.addIfExists("for", id) - .addIfExists("class", params.get("cssClass")) - .addIfExists("style", params.get("cssStyle")) - .addIfExists("title", params.get("title")); + a.addIfExists("for", id).addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")).addIfExists("title", params.get("title")); super.start("label", a); if (StringUtils.isNotEmpty(itemValueStr)) { characters(itemValueStr); @@ -95,4 +81,42 @@ public class RadioHandler extends AbstractTagHandler implements TagGenerator { } } + /** + * It's set to true if the nameValue (the value associated with the name + * which is typically set in the action is equal to the current key value. + * + * @param params + * the params + * + * @param itemKeyStr + * the item key str + * + * @return the boolean + */ + private Boolean isChecked(Map<String, Object> params, String itemKeyStr) { + Boolean checked = false; + if (itemKeyStr != null) { + + // NameValue are the values that is provided by the name property + // in the action + Object nameValue = params.get("nameValue"); + + if (nameValue != null) { + + Iterator itt = MakeIterator.convert(nameValue); + while (itt.hasNext()) { + + String value = (String) itt.next(); + if (checked = value.equalsIgnoreCase(itemKeyStr)) { + break; + } + + } + + } + + } + return checked; + } + }
