2

I'm looking for a possibility to either insert a variable in a table cell or if the value turns out to be null, write another text in the cell, but with different styling. How can I achieve that?

this one obviously doesnt work:

<c:out value="${data.onlineData.state}" default="<span class="customercare-null"><spring:theme code="productOffline.null" /></span>" escapeXml="false" />

would this be correct syntax for what I'm trying to achieve?

<c:out value="${data.onlineData.state}">
    <span class="customercare-null"><spring:theme code="productOffline.null" /></span>
</c:out>

i know that a c:choose could solve this, but i'd rather like to know if the code presented above is also legit

thank you in advance

SHRX
  • 559
  • 1
  • 6
  • 17
  • btw my current solution is a c:choose, but its really bulky so i would love to change that part of the code – SHRX Jul 05 '18 at 06:59
  • 2
    c:choose or c:if are the right tools for the job. – JB Nizet Jul 05 '18 at 07:00
  • Possible duplicate of [if...else within JSP or JSTL](https://stackoverflow.com/questions/5935892/if-else-within-jsp-or-jstl) – Jasper de Vries Jul 05 '18 at 07:47
  • my question was more about if the code i presented at the end would also be correct, not about the if/else situation – SHRX Jul 05 '18 at 10:57

1 Answers1

4

Use <c:if> to check null value

<c:if test="${empty data.onlineData.state}">
    <span class="customercare-null"><spring:theme code="productOffline.null" /></span>
</c:if>
<c:if test="${not empty data.onlineData.state}">
   <c:out value="${data.onlineData.state}" />
</c:if>

Or using <c:choose>:

<c:choose>
    <c:when test="${empty data.onlineData.state}">
        <span class="customercare-null"><spring:theme code="productOffline.null" /></span>
    </c:when>
    <c:otherwise>
       <c:out value="${data.onlineData.state}" />
    </c:otherwise>
</c:choose>

would this be correct syntax for what I'm trying to achieve?

If you use

<c:out value="${data.onlineData.state}">
    <span class="customercare-null"><spring:theme code="productOffline.null" /></span>
</c:out>

Then <span class="customercare-null"><spring:theme code="productOffline.null" /></span> will diplay as text. Html component will not render.

HybrisHelp
  • 5,518
  • 2
  • 27
  • 65
  • thanks for your answer, i will change the code to c:choose. But out of interest, is there a specific reason why i shouldnt use the method i described in my post? it seems to work just fine and, since i have to use this code over and over again, would safe me some characters. – SHRX Jul 05 '18 at 07:50
  • If you use your posted code. It will render `` as text not as html component. You can try and see. – HybrisHelp Jul 12 '18 at 10:59