You can just use the conditional operator ?:
in EL.
E.g.
<c:set var="B" value="#{(list.A / list.C) * 100}" />
<h:outputText value="#{B}" style="display: block; background-color: #{B lt 50 ? 'red' : (B lt 90 ? 'blue' : 'green')}" />
If B
is also used elsewhere in the model or controller, then you could add a public int getB()
method which just contains return (A/C) * 100;
and then use #{list.B}
instead of #{B}
.
Note that proper design is to use a CSS class instead. E.g.
<h:outputText value="#{B}" styleClass="percentage #{B lt 50 ? 'poor' : (B lt 90 ? 'average' : 'good')}" />
with
td .percentage {
display: block;
}
.percentage.poor {
background-color: red;
}
.percentage.average {
background-color: blue;
}
.percentage.good {
background-color: green;
}
You can of course also perform the determination of CSS style/class in a getter method as suggested by the other answer, but that's a poor separation of concerns.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…