mirror of
https://github.com/apache/struts.git
synced 2026-08-05 22:56:59 +00:00
Refactors name/value calculation logic
This commit is contained in:
@@ -45,6 +45,7 @@ import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -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;
|
||||
@@ -797,37 +800,7 @@ public abstract class UIBean extends Component {
|
||||
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);
|
||||
|
||||
@@ -911,6 +884,46 @@ public abstract class UIBean extends Component {
|
||||
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) {
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user