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?