3
我有一些常量,比如我想在我的Struts 2應用程序的每個頁面上顯示APPVERSION,APPNAME和APPREV。struts2 ognl servlet context
有了這些要求,我認爲將這個信息介紹servletContext並在應用程序部署時加載這些信息會很棒。
我已經創建了一個實現ServletContextListener
監聽器:
public class ApplicationInitListenerImpl extends GenericVsService implements ApplicationInitListener,ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
sc.setAttribute("appVer",xxx.utils.VConstants.APPVER);
sc.setAttribute("appName",xxx.utils.VConstants.APPNAME);
sc.setAttribute("appRev",xxx.utils.VConstants.APPREV);
}
}
,然後我在web.xml
添加監聽器:
<listener>
<listener-class>xxx.listeners.ApplicationInitListenerImpl</listener-class>
</listener>
在我Tiles模板我已經加入:
<s:property value="#application.appName"/> - <s:property value="#application.appVer"/>
但我在這裏什麼也沒得到。
如果我從Struts 2 Action中檢索servletContext,我可以讀取正確的值,所以值設置爲ok。
我在做什麼錯?
我想這應該工作;使用'#attr'應該搜索所有的作用域(如使用JSP EL,'$ {appName}')。 – 2013-02-14 18:41:51
如果這兩方面的工作比我以前想到的要容易得多......'
'當然這需要啓用靜態方法調用......這將完成直接從jsp中執行的操作。 – Quaternion 2013-02-14 18:49:10嗨戴夫,它似乎#attr按預期工作。我不明白的是爲什麼這不適用於#應用程序。 ServletContext與應用程序範圍不等價嗎? ps提交這個答案,以便我可以批准它。 – 2013-02-14 18:56:05