我下WEB-INF目錄複製的配置文件,名爲configDEV.properties和configPRO.properties(一個用於開發環境,另一個爲生產環境)。合適的方式來傳遞由彈簧加載到JSF全球配置屬性
我加載正確的文件,由於這些春天的聲明,這Tomcat啓動參數:
<context:property-placeholder
location="WEB-INF/config${project.environment}.properties" />
-Dproject.environment=PRO
(or –Dproject.environment=DEV)
然後,在的servlet監聽(稱爲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>
所有這一切工作確定,但我想請問專家是否是最適當的方式來做到這一點,或者如果這可以通過某種方式簡化?
在此先感謝您的提示和建議
您使用的是哪個版本的Spring和JSF? – Ravi
Spring 3.1.1&JSF 2.0(Mojarra 2.0.6?) – webmeiker