Skip to content

Commit

Permalink
WW-5117 Does a conditional evaluation depending on the tag
Browse files Browse the repository at this point in the history
Some tags requires to perform a lazy evaluation which can only happen in ftl template as performing it in the component class is not possible
  • Loading branch information
lukaszlenart committed Jan 13, 2022
1 parent 893a892 commit a4a4e9e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
16 changes: 13 additions & 3 deletions core/src/main/java/org/apache/struts2/components/CheckboxList.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,27 @@
allowDynamicAttributes = true)
public class CheckboxList extends ListUIBean {
final public static String TEMPLATE = "checkboxlist";

public CheckboxList(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
super(stack, request, response);
}

protected String getDefaultTemplate() {
return TEMPLATE;
}

public void evaluateExtraParams() {
super.evaluateExtraParams();
}

}
/**
* Checkboxlist tag requires lazy evaluation as list of tags is dynamically generated using <s:iterator/>
*
* @return boolean true by default
*/
@Override
protected boolean lazyEvaluation() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ public void setListTitle(String listTitle) {
this.listTitle = listTitle;
}


public void setThrowExceptionOnNullValueAttribute(boolean throwExceptionOnNullValueAttribute) {
this.throwExceptionOnNullValueAttribute = throwExceptionOnNullValueAttribute;
}
Expand Down
17 changes: 14 additions & 3 deletions core/src/main/java/org/apache/struts2/components/Radio.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,27 @@
allowDynamicAttributes = true)
public class Radio extends ListUIBean {
final public static String TEMPLATE = "radiomap";

public Radio(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
super(stack, request, response);
}

protected String getDefaultTemplate() {
return TEMPLATE;
}

public void evaluateExtraParams() {
super.evaluateExtraParams();
}
}

/**
* Radio tag requires lazy evaluation as list of tags is dynamically generated using <s:iterator/>
*
* @return boolean true by default
*/
@Override
protected boolean lazyEvaluation() {
return true;
}

}
14 changes: 12 additions & 2 deletions core/src/main/java/org/apache/struts2/components/UIBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -1277,8 +1277,8 @@ public void setDynamicAttributes(Map<String, String> tagDynamicAttributes) {
String attrValue = entry.getValue();

if (!isValidTagAttribute(attrName)) {
if (ComponentUtils.altSyntax(getStack()) && ComponentUtils.isExpression(attrValue)) {
dynamicAttributes.put(attrName, String.valueOf(ObjectUtils.defaultIfNull(findString(attrValue), attrValue)));
if (ComponentUtils.altSyntax(getStack()) && ComponentUtils.containsExpression(attrValue) && !lazyEvaluation()) {
dynamicAttributes.put(attrName, ObjectUtils.defaultIfNull(findString(attrValue), attrValue));
} else {
dynamicAttributes.put(attrName, attrValue);
}
Expand All @@ -1302,4 +1302,14 @@ public void copyParams(Map<String, Object> params) {
}
}

/**
* Used to avoid evaluating attributes in {@link #evaluateParams()} or {@link #evaluateExtraParams()}
* as evaluation will happen in tag's template
*
* @return boolean false if evaluation should be performed in ftl
*/
protected boolean lazyEvaluation() {
return false;
}

}
8 changes: 4 additions & 4 deletions core/src/main/resources/template/simple/checkboxlist.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<#assign itemKeyStr = stack.findString('top')>
</#if>
<#if parameters.listLabelKey??>
<#-- checks the valueStack for the 'valueKey.' The valueKey is then looked-up in the locale
<#-- checks the valueStack for the 'valueKey.' The valueKey is then looked-up in the locale
file for it's localized value. This is then used as a label -->
<#assign itemValue = struts.getText(stack.findString(parameters.listLabelKey))/>
<#elseif parameters.listValue??>
Expand Down Expand Up @@ -95,9 +95,10 @@
<#include "/${parameters.templateDir}/${parameters.expandTheme}/css.ftl" />
<#include "/${parameters.templateDir}/${parameters.expandTheme}/scripting-events.ftl" />
<#include "/${parameters.templateDir}/${parameters.expandTheme}/common-attributes.ftl" />
<#global evaluate_dynamic_attributes = true/>
<#include "/${parameters.templateDir}/${parameters.expandTheme}/dynamic-attributes.ftl" />
/>
<label<#rt/>
<label<#rt/>
<#if parameters.id?has_content>
for="${parameters.id}-${itemCount}"<#rt/>
<#else>
Expand All @@ -106,11 +107,10 @@
class="checkboxLabel">${itemValue}</label>
</@s.iterator>
<#else>
&nbsp;
</#if>
<input type="hidden" id="__multiselect_${parameters.id}" name="__multiselect_${parameters.name}"
value=""<#rt/>
<#if parameters.disabled!false>
disabled="disabled"<#rt/>
</#if>
/>
/>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
<#list aKeys?filter(acceptKey) as aKey><#rt/>
<#assign keyValue = parameters.dynamicAttributes.get(aKey)/>
<#if keyValue?is_string>
<#assign value = struts.translateVariables(keyValue)!keyValue/>
<#if evaluate_dynamic_attributes!false == true>
<#assign value = struts.translateVariables(keyValue)!keyValue/><#rt/>
<#else>
<#assign value = keyValue/><#rt/>
</#if>
<#else>
<#assign value = keyValue?string/>
</#if>
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/resources/template/simple/radiomap.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<#assign itemKeyStr = stack.findString('top')>
</#if>
<#if parameters.listValueKey??>
<#-- checks the valueStack for the 'valueKey.' The valueKey is then looked-up in the locale
<#-- checks the valueStack for the 'valueKey.' The valueKey is then looked-up in the locale
file for it's localized value. This is then used as a label -->
<#assign valueKey = stack.findString(parameters.listValueKey)!''/>
<#if valueKey?has_content>
Expand Down Expand Up @@ -94,9 +94,10 @@
<#include "/${parameters.templateDir}/${parameters.expandTheme}/css.ftl" />
<#include "/${parameters.templateDir}/${parameters.expandTheme}/scripting-events.ftl" />
<#include "/${parameters.templateDir}/${parameters.expandTheme}/common-attributes.ftl" />
<#global evaluate_dynamic_attributes = true/>
<#include "/${parameters.templateDir}/${parameters.expandTheme}/dynamic-attributes.ftl" />
/><#rt/>
<label for="${parameters.id}${itemKeyStr?replace(".", "_")}"<#include "/${parameters.templateDir}/${parameters.expandTheme}/css.ftl"/>><#rt/>
${itemValue}<#t/>
</label>
</@s.iterator>
</@s.iterator>

0 comments on commit a4a4e9e

Please sign in to comment.