Java 使用 servletContextListener 将属性文件值加载到 web.xml 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24980063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
loading the property file values into web.xml using servletContextListener
提问by user2567005
i want load the values form property file in to my web.xml
我想将值表单属性文件加载到我的 web.xml
this is my web.xml file
这是我的 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>
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
当我从此类打印值时,它向我显示了属性值,但在 web.xml 中,当我尝试访问 ${primefacestheme} 时,它不会加载此值
and this is my config.properties file
这是我的 config.properties 文件
primefacestheme=glass-x
wellcomepage=accdenied.xhtml
please help me
请帮我
采纳答案by Amogh
Can you check with change in web.xml:
您可以检查 web.xml 中的更改:
<context-param>
<param-name>primefacestheme</param-name>
<param-value>${primefacestheme}</param-value>
</context-param>
As config.propertieshaving property key name as primefacesthemeand in code we are checking with that key name and if its nullthen setting property value to that key name.
由于在代码中config.properties具有属性键名称,primefacestheme我们正在检查该键名称以及是否null将属性值设置为该键名称。
In short <param-name>and property key name need to be same.
简而言之<param-name>,属性键名称需要相同。
回答by SparkOn
As your properties file contains the key as primefacesthemebut while calling its value you are trying to use some other key
由于您的属性文件包含键,primefacestheme但在调用其值时,您正尝试使用其他键
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>

