Access JSF and Primefaces version numbers programmatically

你离开我真会死。 提交于 2019-12-30 05:00:34

问题


I use PrimeFaces 3.5.x and Mojarra JSF 2.1.x I would like to access and show the versions of both libraries programmatically.

I use the versions as maven2 properties, but I hope there is an easier way to get the versions. I hope to find something like:

Primeface.getVersion();
FacesContext.getCurrentInstance();

A JavaScript based solution would be fine too, since I only want to display the version on a status page.


回答1:


For JSF:

//returns the major version (2.1)
FacesContext.class.getPackage().getImplementationVersion();

//returns the specification version (2.1)
Package.getPackage("com.sun.faces").getSpecificationVersion();

//returns the minor implementation version (2.1.x)
Package.getPackage("com.sun.faces").getImplementationVersion();

For Primefaces 3.x you can use the Constants class in utils package:

import org.primefaces.util.Constants;

Constants.VERSION



回答2:


In PrimeFaces 4.0, Constants.VERSION is removed in favor of;

RequestContext.getCurrentInstance().getApplicationContext().getConfig().getBuildVersion();

Also watch out for FacesContext.class.getPackage().getImplementationVersion();, it doesn't work on some app servers like websphere.




回答3:


For PrimeFaces, you can use the Constants class:

org.primefaces.util.Constants.VERSION



回答4:


If you need to get the Version on runtime, there will not be any instance of RequestContext. Therefore you could use the ImplementationVersion of the package:

Package.getPackage("org.primefaces").getImplementationVersion()

If the package cannot be resolved, you can try to resolve the version via PrimeFaces class:

PrimeFaces.class.getPackage().getImplementationVersion()


来源:https://stackoverflow.com/questions/18355985/access-jsf-and-primefaces-version-numbers-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!