2017-02-15 41 views
0

我有一個簡單的應用,它使用Spring啓動網絡和 我在 src/main/webapp/WEB-INF一個web.xml文件,其中包含的context-param這樣無法從春天啓動控制器獲得的context-param在web.xml

<context-param> 
    <description>Directory containing the configuration files</description> 
    <param-name>confDir</param-name> 
    <param-value>/opt/rasdaman/etc/</param-value> 
</context-param> 

在Servlet的控制器,我能得到servlet上下文

@Autowired 
private ServletContext servletContext; 

,但是當我試圖獲取參數,則返回null

servletContext.getInitParameter("confDir"); 

,當我試圖讓ServletContext的真實路徑

servletContext.getRealPath(File.separator); 

返回

"..../src/main/webapp/" 

我怎樣才能在web.xml配置變量?

謝謝

+0

爲什麼不把你的參數放在屬性文件中? – jstuartmilne

+0

它是一個使用web.xml配置的遺留源,因此我無法移動到其他文件。 –

回答

1

沒有嘗試過,但我自己。如果你犯了一個控制器實現ServletContextAware我想你可以從你的context-param:http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/ServletContextAware.html

這個想法是

public class MyCoolController implements ServletContextAware { 


    @Override 
    setServletContext(ServletContext servletContext) { 
     String confDir = servletContext.getInitParameter("confDir"); 
    } 
} 

給它一個鏡頭

+0

它的作用與@Autowired相同,謝謝。 –

+1

您是否將您的應用程序部署爲戰爭文件? – jstuartmilne

+0

它是一個jar文件,因爲Spring Boot像這樣開始。 public static void main(String [] args){0} ApplicationContext app = SpringApplication.run(ApplicationMain.class,args); –

0

由於sudakatux問題是我用使用jar部署Spring Boot不是戰爭然後無法從web.xml讀取

跟隨here改變爲war並與tomcat EE部署正常

相關問題