使用servletContextListener将属性文件值加载到web.xml中

时间:2014-07-27 10:33:42

标签: java xml java-ee web servletcontextlistener

我想将值表单属性文件加载到我的web.xml

这是我的web.xml文件

<?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>

这是我的servlet上下文列表器类

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));
             }
          }

    }

}

当我从这个类打印值时,它向我显示了这些值 但在web.xml中,当我试图访问$ {primefacestheme}这个值时,它不会加载

这是我的config.properties文件

primefacestheme=glass-x
wellcomepage=accdenied.xhtml

请帮帮我

2 个答案:

答案 0 :(得分:0)

由于您的属性文件包含密钥primefacestheme,但在调用其值时,您尝试使用其他密钥

更改此

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

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

答案 1 :(得分:0)

您可以查看web.xml中的更改:

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

作为config.properties,其属性键名称为primefacestheme,在代码中我们正在检查该键名称,如果是null,则将属性值设置为该键名。

简而言之<param-name>,属性键名称必须相同。