Search code examples
jspstruts2ognl

Exchange label from the box ticked


I have in my page a boolean that allows me to choose between two labels to display

package.properties:

viaje.tel=Numéro de téléphone
viaje.tel2=N° de téléphone portable

My Jsp:

<%-- Numéro de téléphone --%>
<div class="yui3-g-r margin-bottom-small">
    <div class="yui3-u">
        <c:choose>
            <c:when test="${viaje.blCcoEtablissementScolaire==true}">
                <s:label for="viaje.tel2" required="true" value="%{getText('viaje.tel2')}" 
                tooltip="%{getText('tooltip.telephone')}" tooltipConfig="#{'tooltipIcon':'%{icoTooltipUrl}', 'jsTooltipEnabled':'true'}" />
            </c:when>
            <c:otherwise>
                <s:label for="viaje.tel" required="true" value="%{getText('viaje.tel')}" 
                tooltip="%{getText('tooltip.telephone')}" tooltipConfig="#{'tooltipIcon':'%{icoTooltipUrl}', 'jsTooltipEnabled':'true'}" />
            </c:otherwise>
        </c:choose>     
    </div>
    <div class="yui3-u-1-2">
        <s:textfield id="viaje.tel" name="viaje.tel" maxlength="20" size="20" />
    </div>
</div>  

Could my code be simplified?


Solution

  • You can use ternary operator

    <s:set var='tel' value="%{viaje.blCcoEtablissementScolaire?'viaje.tel2':'viaje.tel'}"/>
    <s:label for="%{#tel}" required="true" 
             value="%{getText(#tel)}" 
             tooltip="%{getText('tooltip.telephone')}" 
             tooltipConfig="#{'tooltipIcon':'%{icoTooltipUrl}', 'jsTooltipEnabled':'true'}" />