JSF rowStyleClass how to get the current date

advertisements

I am working with JSF and Primefaces. I want to show some rows of a datatable with a different background color depending on a condition. That condition is if a date () is greater or equal than current date that rows must be shown with a color especified in the CSS file. How can I get the current date?

This is the Facelet

<p:dataTable id="dataTableCitizens" rowStyleClass="#{item.dateLastDate ge currentDate ? 'colored' : null}" value="#{citizensManagedBean.listCitizens}"  var="item">

This is the CSS

.colored {
background-color: yellow;
}

I have created a faces-config.xml file inside WEB-INF with the following code:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.0"
              xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">

    <managed-bean>
    <managed-bean-name>currentDate</managed-bean-name>
    <managed-bean-class>java.util.Date</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

, but when I run the web app it gives me a java.lang.NullPointerException. What am I doing wrong?


The most basic solution is to create a variable in your bean and initiate it with a @PostConstruct annotation :

private Date currentDate;

@PostConstruct
public void init() {
     currentDate = new Date();
}

//Getter and Setters...

Then you can access it in your view : #{yourBean.currentDate}

Or you can use Omnifaces' default implementation of #{now}.