My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

sâmbătă, 18 aprilie 2015

[OmniFaces utilities (2.0)] Get the absolute disk file system path representation of the given web content path


[OmniFaces utilities] The getRealPath() method returns the absolute disk file system path representation of the given web content path. This thus converts the given path of a web content resource (e.g. /index.xhtml) to an absolute disk file system path (e.g. /path/to/server/work/folder/some.war/index.xhtml) which can then be used in File, FileInputStream, etc.
Note that this will return null when the WAR is not expanded into the disk file system, but instead into memory. If all you want is just an InputStream of the web content resource, then better use #getResourceAsStream(String) instead.
Also note that it wouldn't make sense to modify or create files in this location, as those changes would get lost anyway when the WAR is redeployed or even when the server is restarted. This is thus absolutely not a good location to store for example uploaded files.

Method:
See also: Faces#getContext()
Usage:

For example, let's suppose that we have the following folder structure of an web application:
Now, the demo.xhtml page URL may be something like:

http://localhost:8080/MyApp/faces/user/demo/demo.xhtml

But, for obtaining the absolute disk file system path representation for our demo.xhtml page, we can use the OmniFaces utility method, Faces#getRealPath(), as below:

String onDisk = Faces.getRealPath("/user/pages/demo.xhtml");

A possible output will be:

A:\Applications\UseCase\build\web\user\pages\demo.xhtml

If you simply want to obtain the absolute disk file system path representation of the current view, then use this approach (suppose that index.xhtml is the current view):

String onDisk = Faces.getRealPath(FacesContext.getCurrentInstance().getViewRoot().getViewId());

Or, using one more OmniFaces utilities, we can write this:

String onDisk = Faces.getRealPath(Faces.getViewId());

The result of the above two examples can be:

A:\Applications\UseCase\build\web\index.xhtml


Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors