Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference static variables using EL 3.0?

I am trying to get a static variable in my JSF page.

I followed instructions on this post. I am able to get the variables using the Primefaces extension, however, I am not getting anything in the xhtml when doing the following.

I have a constants file:

public class Test {
    public static final String NAME = "EL Test";
}

And following the post by balusC, I added an application scoped bean (however, this is being called with every request):

import java.lang.reflect.Field;
import javax.annotation.PostConstruct;
import javax.el.ELContextEvent;
import javax.el.ELContextListener;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ManagedBean(eager = true)
@ApplicationScoped
public class Config {
    @PostConstruct
    public void init() {
        FacesContext.getCurrentInstance().getApplication().addELContextListener(new ELContextListener() {
            @Override
            public void contextCreated(ELContextEvent event) {
                event.getELContext().getImportHandler().importClass("my.package.constants.Test");
                Class<?> clazz = event.getELContext().getImportHandler().resolveClass("Test");
                for (Field field : clazz.getFields()) {
                    System.out.println(field.getName());
                }
                System.out.println("clazz = " + clazz);
                System.out.println(clazz.getPackage());
            }
        });
    }
}

And my xhtml page:

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta charset="utf-8"></meta>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
</h:head>

<h:body>

    <h:outputText value="#{Test}"></h:outputText>
    <h:outputText value="#{Test.NAME}"></h:outputText>

</h:body>
</html>

Is there anything I am missing?

like image 501
GauravS Avatar asked Oct 17 '25 20:10

GauravS


2 Answers

p:importConstants was added in PrimeFaces 6.x.

XHTML:

<p:importConstants type="com.example.Constants" var="Constants" />

<h:outputText value="#{Constants.TEST}" />

Java:

package com.example;

public class Constants {
    public final static String TEST = "Imported Constant";
}
like image 179
Vasil Lukach Avatar answered Oct 20 '25 08:10

Vasil Lukach


JSF 2.3 supports referencing static variables in EL using the f:importConstants tag.

Your constants file

public class Test {
    public static final String NAME = "EL Test";
}

can be imported in the view by adding the following metadata.

<f:metadata>
    <f:importConstants type="mypackage.Test" />
</f:metadata>

And then be referenced using EL.

#{Test.NAME}

So your view becomes:

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <f:metadata>
        <f:importConstants type="mypackage.Test" />
    </f:metadata>

    <h:head>
        <meta charset="utf-8"></meta>
        <meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
        <meta name="viewport" content="width=device-width, initial-scale=1"> </meta>
    </h:head>

    <h:body>
        <h:outputText value="#{Test.NAME}"></h:outputText>
    </h:body>
</html>

Source: Arjan Tijms' Weblog.

like image 22
Christophe Weis Avatar answered Oct 20 '25 08:10

Christophe Weis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!