I faced your problem some times ago. The solution I had for myself was to create a Constants
file where I put all the system-dependent variable inside. For example,
public class Constants {
// Local development
private static final String PHOTO_FOLDER = "C:\path\to\photos\Windows";
private static final String APP_PATH = "localhost:8080/project/";
// Online server
private static final String PHOTO_FOLDER = "/path/to/photos/Linux";
private static final String APP_PATH = "www.project.com/";
}
Any constants in the above class can be accessed in any @ManagedBean
using Constants.PROPERTY
. In case, you want to access your constants inside your .xhtml
pages, you can create a .properties
file with similar content:
// Local development
PHOTO_FOLDER = C:\path\to\photos\Windows
APP_PATH = localhost:8080/project/
// Online server
PHOTO_FOLDER = /path/to/photos/Linux
APP_PATH = www.project.com/
Then declare this .properties
file in your faces-config.xml
as a ResourceBundle
:
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<resource-bundle>
<base-name>your_package.constants</base-name>
<var>constants</var>
</resource-bundle>
</application>
</faces-config>
At this point, you can access your constants in .xhtml
pages using #{constants.PROPERTY}
.
Depend on where you're deploying the app, just comment out the unnecessary lines :).
Besides, when you're dealing with file names, just use File.separator
to separate the folders and you should be fine :P.
What I'm doing is definitely NOT the best practices, but it's a pretty easy way to achieve what you want :P. BalusC's answer should be the best practices at the moment while dealing with those components. So perhaps a hybrid should be cool? :)