WEB-INFディレクトリの下に、 configDEV.propertiesおよびconfigPRO.propertiesという重複した構成ファイルがあります(1つは開発環境用、もう1つは実稼働環境用)。
これらのSpring宣言とこのTomcat起動パラメーターのおかげで、適切なファイルをロードします。
<context:property-placeholder
location="WEB-INF/config${project.environment}.properties" />
-Dproject.environment=PRO
(or –Dproject.environment=DEV)
次に、サーブレットリスナー(StartListenerと呼ばれます)で、JSFがこれらのプロパティ、マネージドBean、およびjspビューにアクセスできるようにするために次のことを行います。(具体的には、 cfg.skin.richSelectorというプロパティを試してみます)。
public class StartListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
//Environment properties
Map<String, String> vblesEntorno = System.getenv();
//Project properties
String entorno = vblesEntorno.get("project.environment");
String ficheroPropiedades = "/WEB-INF/config" + entorno + ".properties";
try {
Properties props = new Properties();
props.load(sc.getResourceAsStream(ficheroPropiedades));
setSkinRichSelector(sc, props.getProperty("cfg.skin.richSelector"));
} catch (Exception e) {
//...
}
}
private void setSkinRichSelector(ServletContext sc, String skinRichSelector) {
sc.setInitParameter("cfg.skin.richSelector", skinRichSelector);
}
public void contextDestroyed(ServletContextEvent sce) {}
}
JSFマネージドBeanの場合:
public class ThemeSwitcher implements Serializable {
private boolean richSelector;
public ThemeSwitcher() {
richSelector = Boolean.parseBoolean(
FacesContext.getCurrentInstance().getExternalContext().getInitParameter("cfg.skin.richSelector"));
if (richSelector) {
//do A
} else {
//do B
}
}
//getters & setters
}
xhtmlページの場合:
<c:choose>
<c:when test="#{themeSwitcher.richSelector}">
<ui:include src="/app/comun/includes/themeSwitcherRich.xhtml"/>
</c:when>
<c:otherwise>
<ui:include src="/app/comun/includes/themeSwitcher.xhtml"/>
</c:otherwise>
</c:choose>
これはすべて問題なく機能しますが、それが最も適切な方法であるかどうか、またはこれを何らかの方法で簡略化できるかどうかを専門家に尋ねたいと思います。
ヒントやアドバイスを事前に感謝します