【发布时间】: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
<context-param> <param-name>primefacestheme</param-name> <param-value>${primefacestheme}</param-value> </context-param> -
我猜属性键名和
<param-name>必须相同,正如你的代码所说的System.setProperty(prop, propsFromFile.getProperty(prop)); -
是的,“Amogh”它工作得非常好,谢谢队友。您可以将其发布为答案,以便我可以将其标记为答案
标签: java xml jakarta-ee web servletcontextlistener