0

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

2
  • why not put your param in the property file? Commented Feb 15, 2017 at 12:31
  • it is a legacy source which use web.xml configuration so I could not move to other file. Commented Feb 15, 2017 at 12:34

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Are you deploying your app as a war file?
it is a jar file as Spring Boot starts like this public static void main(String[] args) { ApplicationContext app = SpringApplication.run(ApplicationMain.class, args);
You should probably take a look at this then spring.io/guides/gs/convert-jar-to-war
I tried change to war and add dependency as docs.spring.io/spring-boot/docs/current/reference/htmlsingle/… for maven but also still null for configuration variable in web.xml.
When your war is packaged. Do you see your web.xml inside the war file in the appropriate directory?
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.