Skip to content

Commit

Permalink
Refactors name/value calculation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Nov 13, 2022
1 parent 4d58dd1 commit a77a80a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
75 changes: 44 additions & 31 deletions core/src/main/java/org/apache/struts2/components/UIBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
* <p>
Expand Down Expand Up @@ -439,6 +440,8 @@ public abstract class UIBean extends Component {
private static final Logger LOG = LogManager.getLogger(UIBean.class);

protected static final String ATTR_FIELD_VALUE = "fieldValue";
protected static final String ATTR_NAME_VALUE = "nameValue";
protected static final String ATTR_VALUE = "value";

protected HttpServletRequest request;
protected HttpServletResponse response;
Expand Down Expand Up @@ -797,37 +800,7 @@ public void evaluateParams() {
addParameter("title", findString(title));
}


// see if the value was specified as a parameter already
final String NAME_VALUE = "nameValue";
if (parameters.containsKey("value")) {
parameters.put(NAME_VALUE, parameters.get("value"));
} else {
if (evaluateNameValue()) {
final Class<?> valueClazz = getValueClassType();

if (valueClazz != null) {
if (value != null) {
addParameter(NAME_VALUE, findValue(value, valueClazz));
} else if (translatedName != null) {
boolean evaluated = !translatedName.equals(this.name);
boolean reevaluate = !evaluated || isAcceptableExpression(translatedName);
if (!reevaluate) {
addParameter(NAME_VALUE, translatedName);
} else {
String expr = completeExpression(translatedName);
addParameter(NAME_VALUE, findValue(expr, valueClazz));
}
}
} else {
if (value != null) {
addParameter(NAME_VALUE, findValue(value));
} else if (translatedName != null) {
addParameter(NAME_VALUE, findValue(translatedName));
}
}
}
}
applyValueParameter(translatedName);

final Form form = (Form) findAncestor(Form.class);

Expand Down Expand Up @@ -911,6 +884,46 @@ public void evaluateParams() {
evaluateExtraParams();
}

/**
* Tries to calculate the "value" parameter based either on the provided {@link #value} or {@link #name}
* @param translatedName the already evaluated {@link #name}
*/
protected void applyValueParameter(String translatedName) {
// see if the value has been specified as a parameter already
if (parameters.containsKey(ATTR_VALUE)) {
parameters.put(ATTR_NAME_VALUE, parameters.get(ATTR_VALUE));
} else {
if (evaluateNameValue()) {
final Class<?> valueClazz = getValueClassType();

if (valueClazz != null) {
if (value != null) {
addParameter(ATTR_NAME_VALUE, findValue(value, valueClazz));
} else if (translatedName != null) {
processTranslatedName(translatedName, (expr) -> findValue(expr, valueClazz));
}
} else {
if (value != null) {
addParameter(ATTR_NAME_VALUE, findValue(value));
} else if (translatedName != null) {
processTranslatedName(translatedName, this::findValue);
}
}
}
}
}

private void processTranslatedName(String translatedName, Function<String, Object> evaluator) {
boolean evaluated = !translatedName.equals(this.name);
boolean reevaluate = !evaluated || isAcceptableExpression(translatedName);
if (!reevaluate) {
addParameter(ATTR_NAME_VALUE, translatedName);
} else {
String expr = completeExpression(translatedName);
addParameter(ATTR_NAME_VALUE, evaluator.apply(expr));
}
}

protected String escape(String name) {
// escape any possible values that can make the ID painful to work with in JavaScript
if (name != null) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/site/resources/tags/checkbox-attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
<td class="tag-attribute">false</td>
<td class="tag-attribute">false</td>
<td class="tag-attribute">Boolean</td>
<td class="tag-attribute">If set to true, unchecked elements will be submitted with the form. Since Struts 6.1.1 you can use a constant "struts.ui.checkbox.submitUnchecked" to set this property globally</td>
<td class="tag-attribute">If set to true, unchecked elements will be submitted with the form. Since Struts 6.1.1 you can use a constant "struts.ui.checkbox.submitUnchecked" to set this attribute globally</td>
</tr>
<tr>
<td class="tag-attribute">tabindex</td>
Expand Down

0 comments on commit a77a80a

Please sign in to comment.