簡體   English   中英

無法從Spring Boot Controller獲取web.xml中的context-param

[英]Cannot get context-param in web.xml from Spring Boot Controller

我有一個使用Spring Boot Web的簡單webapp,並且在src/main/webapp/WEB-INF中有一個web.xml文件,其中包含這樣的context-param

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

在Servlet Controller中,我可以獲得Servlet Context

@Autowired
private ServletContext servletContext;  

但是當我嘗試獲取參數時,它返回null

servletContext.getInitParameter("confDir");

當我嘗試獲取servletContext的真實路徑時

servletContext.getRealPath(File.separator);

它返回

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

如何在web.xml中獲取配置變量?

謝謝

Havent自己嘗試了一下。 如果您想讓控制器實現ServletContextAware,我想您可以從以下網址獲取上下文參數: 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");
    }
}

試一試

感謝sudakatux的問題是我用jar部署了Spring Boot而不是war,所以無法從web.xml中讀取

按照此處更改為戰爭並正常使用tomcat EE進行部署

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM