3

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?

Community
  • 1
  • 1
GauravS
  • 81
  • 1
  • 6

4 Answers4

10

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";
}
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
8

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.

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
5

You can use o:importConstants by omnifaces for that

For example:

 public class Foo {
     public static final String FOO1 = "foo1";
     public static final String FOO2 = "foo2";
 }

 public interface Bar {
     public String BAR1 = "bar1";
     public String BAR2 = "bar2";
 }

 public enum Baz {
     BAZ1, BAZ2;
 }

The constant field values of the above types can be mapped into the request scope as follows:

 <o:importConstants type="com.example.Foo" />
 <o:importConstants type="com.example.Bar" />
 <o:importConstants type="com.example.Baz" var="Bazzz" />
 ...
 #{Foo.FOO1}, #{Foo.FOO2}, #{Bar.BAR1}, #{Bar.BAR2}, #{Bazzz.BAZ1}, #{Bazzz.BAZ2}
Daniel
  • 36,833
  • 10
  • 119
  • 200
4

As I see you're using JSF 2, you could go with the Omnifaces library:

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

Then in the facelet:

<o:importConstants type="com.example.Test " />

#{Test.NAME}

Otherwise, if you want to avoid using third party libraries, use an @ApplicationScoped managed bean with a getter for this aim:

@ManagedBean
@ApplicationScoped
public class Test{
    public static final String name = "EL Test";

    public String getName(){
        return name;
    }

}

Which you can reference with:

#{test.name}

See also:

Aritz
  • 30,971
  • 16
  • 136
  • 217