Java – how to implement some if then logic using JSF and facelets?

I have a bean with live state Depending on the status value, different CSS classes should be applied to render it

So I need something like this (far from the real pseudo code):

if status == "Approved"
     cssClass = "green"
if status == "Rejected"
     cssClass = "red"
<span class="cssClass">Some info</span>

I tried to apply JSTL, but I couldn't make it with facelets and JSF (but I heard it was possible, maybe its truth) This is the code:

<c:choose>
    <c:when test="#{report.approved}">
        <c:set var="statusClass" value="approved"/>
    </c:when>
    <c:when test="#{report.rejected}">
        <c:set var="statusClass" value="rejected"/>
    </c:when>
    <c:when test="#{report.inProgress}">
        <c:set var="statusClass" value="progress"/>
    </c:when>
    <c:when test="#{report.pendingHR}">
        <c:set var="statusClass" value="pending"/>
    </c:when>
</c:choose>
<span class="status ${statusClass}">#{report.formattedStatus}</span>

How to use JSF / facelets?

Solution

Bring benefits to both sides (model and view) instead of using enumeration instead of four independent Boolean values, which may only lead to maintenance trouble after all

public enum Status {
    APPROVED,REJECTED,PROGRESS,PENDING;
}

Not only is it easier and clearer in Java, but you can also print it in El

<span class="#{bean.status}" />
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>