1

In my JSP page, I am getting double values as obtained from database. Inside a scriptlet tag, I am initializing java variables to be used as shown below :

<body>
    <%
        Map<String, Double> colorMap = new HashMap<>();
        colorMap = InsightDbConn.getInstance().getFpyRtyColorLimits();
        double var_yellow = colorMap.get("fpy_yellow"); // storing 85
        double var_green = colorMap.get("fpy_green"); // storing 93
    %>

I need to use these values so as to set the row colors of the table based on the values I have obtained. I am using a CSS class to set the row colors. But to define that class value, I am using JSP's ternary operator to loop through the values obtained from database.

Below is my code :

 <c:forEach items="${fpyrtyDailyList}" var="entry">
     <tr>
        <td>${entry.category}</td>
        <td>
          <div class="${entry.percentage == 0 ? 'green' : entry.percentage < var_yellow ? 'red' : entry.percentage < var_green ? 'yellow' : 'green' }">
             ${entry.percentage}
          </div>
        </td>
        <td>${entry.daily_date}</td>
        <td>${entry.total_tests}</td>
        <td>${entry.total_passed}</td>
     </tr>
 </c:forEach>

As seen in the above code, I am trying to set the value of CSS class with the help of Java variables i defined in scriptlet. If i use numerical values, the work gets done. The values are dynamic and may change over time so I intend to use the scriptlet variables instead.

Can someone kindly tell where I went wrong.

EDIT 1 : I wish to iterate the CSS class value inside the loop so that every row gets its own row color based on value it holds

Vishal A
  • 144
  • 3
  • 17
  • In the first case by using scriptlets... – chrylis -cautiouslyoptimistic- Feb 08 '17 at 06:33
  • I am trying to use value of scriptlets, but it paints all rows of green color or to be precise using CSS defined 'green' class. Its not using the variables – Vishal A Feb 08 '17 at 06:37
  • `${entry.percentage}` is not a scriptlet but an Expression Language notation ;) Scriptlet would be `<% String s = value == 0 ? "foo" : "bar" %>` – AxelH Feb 08 '17 at 06:44
  • Possible duplicate of [Ternary operator in JSTL/EL](http://stackoverflow.com/questions/14482451/ternary-operator-in-jstl-el) – AxelH Feb 08 '17 at 06:44
  • Or, if my seconds reading of the question is more accurate (not sur of the need), [How to evaluate a scriptlet variable in EL?](http://stackoverflow.com/q/5965812/4391450) – AxelH Feb 08 '17 at 06:47
  • That didnt solve the issue. I wish to iterate the CSS class value inside the for loop so that every row gets its own row color based on the value it holds – Vishal A Feb 08 '17 at 08:48

1 Answers1

0

First you have to create a variable with the value which you receive in percentage and after that use that variable to get the value in ternary operator. Below is the example.

<c:set var="entryPercentage" value="${entry.percentage}"/>
<div class="${entryPercentage == 0 ? 'green' : entryPercentage < var_yellow ? 'red' : entryPercentage < var_green ? 'yellow' : 'green' }">
Rashid
  • 343
  • 4
  • 14