Bằng Rikimaru
Bằng Rikimaru

Reputation: 1595

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

I have a simple webapp which use Spring Boot web and I have a web.xml file in src/main/webapp/WEB-INF which contains context-param like this

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

In Servlet Controller, I could get the Servlet Context

@Autowired
private ServletContext servletContext;  

but when I tried to get the parameter, it returns null

servletContext.getInitParameter("confDir");

when I tried to get the real path to the servletContext

servletContext.getRealPath(File.separator);

it returns

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

How can I get the configuration variable in web.xml?

Thanks

Upvotes: 0

Views: 2611

Answers (2)

Bằng Rikimaru
Bằng Rikimaru

Reputation: 1595

Thanks to sudakatux the problem is I used deploy Spring Boot with jar not war then cannot read from web.xml

follow here to change to war and deploy with tomcat EE normally

Upvotes: 0

jstuartmilne
jstuartmilne

Reputation: 4508

Havent tried it myself but. If you make a Controller implement ServletContextAware I think you can get your context-param from : http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/ServletContextAware.html

The idea would be

public class MyCoolController implements ServletContextAware {


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

Give it a shot

Upvotes: 1

Related Questions