JSP/JSF从web.xml中取出context-param的配置信息
JSP/JSF从web.xml中取出context-param的配置信息。
应用场景:我们配置了项目的版本信息,想让他显示在页面上,如:
- <context-param><!-- ######### DON'T touch this param. ######### -->
- <param-name>snx.jsf2.APP_VERSION</param-name>
- <param-value>${project.build.finalName}-${version.on.web.page_ver}.${version.on.web.page_timestamp}</param-value>
- </context-param>
我们怎么方便的从页面拿到它呢?!
对于JSP页面中,可通过EL表达式:
- 系统版本:${initParam['snx.jsf2.APP_VERSION']}
对于Servelt中:
- String version = getServletContext().getInitParameter("snx.jsf2.APP_VERSION");
对于JSF页面中:
- Version:<h:outputText value="#{initParam['snx.jsf2.APP_VERSION']}" />
对于ViewModel中:
- FacesContext facesContext = FacesContext.getCurrentInstance();
- String version = facesContext.getExternalContext().getInitParameter("snx.jsf2.APP_VERSION");
别外:取Servelt中的init-param :
- <servlet>
- <servlet-name>SpringMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- String version = this.getInitParameter("contextConfigLocation");