loading the property file values into web.xml using servletContextListener

user2567005 picture user2567005 · Jul 27, 2014 · Viewed 12.8k times · Source

i want load the values form property file in to my web.xml

this is my web.xml file

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">


    <!-- Property listeners -->
    <listener>       
      <listener-class>com.kpowd.utility.PropertyReading</listener-class>
   </listener>  

    <display-name>JSF 2 + Spring 3 Integration example</display-name>
    <!-- The welcome page -->
    <welcome-file-list>
        <welcome-file>login.xhtml</welcome-file>
    </welcome-file-list>

    <!-- Spring listeners -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-security.xml,
            /WEB-INF/applicationContext.xml            
        </param-value>
    </context-param>
    <!-- Change the primeface theme -->

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>${primefacestheme}</param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Start JSF -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- JSF URL mapping -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

</web-app>

and this is my servelet context listner class

package com.kpowd.utility;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class PropertyReading implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // TODO Auto-generated method stub

    }

    @Override
    public void contextInitialized(ServletContextEvent event) {

        final String props = "/config.properties";
        final Properties propsFromFile = new Properties();
        try {               
            propsFromFile.load(getClass().getResourceAsStream(props));

        } catch (final IOException e) {
            e.printStackTrace();
        }
        for (String prop : propsFromFile.stringPropertyNames())
          {
             if (System.getProperty(prop) == null)
             {
                 System.setProperty(prop, propsFromFile.getProperty(prop));
             }
          }

    }

}

when i print the values form this class it shows me the proprety values but in web.xml when im trying to access ${primefacestheme} this value it doesn't load

and this is my config.properties file

primefacestheme=glass-x
wellcomepage=accdenied.xhtml

please help me

Answer

SparkOn picture SparkOn · Jul 27, 2014

As your properties file contains the key as primefacestheme but while calling its value you are trying to use some other key

change this

<context-param>
  <param-name>primefaces.THEME</param-name>
  <param-value>${primefacestheme}</param-value>
</context-param>

to

<context-param>
  <param-name>primefacestheme</param-name>
  <param-value>${primefacestheme}</param-value>
</context-param>