2013-02-14 84 views
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。

我在做什么错?

+2

我想这应该工作;使用'#attr'应该搜索所有的作用域(如使用JSP EL,'$ {appName}')。 – 2013-02-14 18:41:51

+1

如果这两方面的工作比我以前想到的要容易得多......' '当然这需要启用静态方法调用......这将完成直接从jsp中执行的操作。 – Quaternion 2013-02-14 18:49:10

+0

嗨戴夫,它似乎#attr按预期工作。我不明白的是为什么这不适用于#应用程序。 ServletContext与应用程序范围不等价吗? ps提交这个答案,以便我可以批准它。 – 2013-02-14 18:56:05

回答

0

您可以使用

<s:property value="#attr.appName"/> - <s:property value="#attr.appVer"/> 

或者

${appName} - ${appVer}