How to implement some if-then logic with JSF and Facelets?

I have a bean with a field status. Depending on the value status, a different css class must be used to render it.

So, I need something like this (very far from the pseudo-exchange of real things):

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

I tried to apply jstl, but I can not get it to work with facelets and jsf (but I heard that this is possible, maybe its true). Here 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 do this with JSF / Facelets?

+3
source share
3 answers

( ) , enum , .

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

Java, EL.

<span class="#{bean.status}" />
+8

JSF rendered h-. , EL ( ) JSF, ?: . .

<span class="status #{report.approved ? 'approved' : report.rejected ? 'rejected' : report.inProgress ? 'progress' : report.pendingHR ? 'pending' : ''}">
+4

cssStatus bean, CSS.

public String getCssStatus() {
    if( this.status == .... )
       return "green";
    else 
       ...
}

<span class="status #{report.cssStatus}">#{report.formattedStatus}</span>

AFAIK, .

+1

Source: https://habr.com/ru/post/1757851/


All Articles