【问题标题】:loading the property file values into web.xml using servletContextListener使用 servletContextListener 将属性文件值加载到 web.xml
【发布时间】:2014-09-18 17:52:09
【问题描述】:

我想将值表单属性文件加载到我的 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>

这是我的 serverlet 上下文监听器类

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

请帮帮我

【问题讨论】:

  • 你能检查一下更改 web.xml &lt;context-param&gt; &lt;param-name&gt;primefacestheme&lt;/param-name&gt; &lt;param-value&gt;${primefacestheme}&lt;/param-value&gt; &lt;/context-param&gt;
  • 我猜属性键名和&lt;param-name&gt; 必须相同,正如你的代码所说的System.setProperty(prop, propsFromFile.getProperty(prop));
  • 是的,“Amogh”它工作得非常好,谢谢队友。您可以将其发布为答案,以便我可以将其标记为答案

标签: java xml jakarta-ee web servletcontextlistener


【解决方案1】:

你能检查一下 web.xml 的变化吗:

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

由于config.properties 的属性键名称为primefacestheme,并且在代码中我们正在检查该键名称,如果它的null 则将属性值设置为该键名称。

简而言之&lt;param-name&gt; 和属性键名需要相同。

【讨论】:

    【解决方案2】:

    由于您的属性文件包含 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>
    

    【讨论】:

      猜你喜欢
      • 2011-09-03
      • 2012-02-02
      • 1970-01-01
      • 2011-03-16
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多