How to access values of web.xml in Spring Controller

后端 未结 3 1464
自闭症患者
自闭症患者 2020-12-10 14:53

I am defining a context-param in web.xml of my application as below


    baseUrl
    

        
3条回答
  •  清歌不尽
    2020-12-10 15:34

    Make your Controller implement the ServletContextAware interface. This will force you implement a setServletContext(ServletContext servletContext) method and Spring will inject the ServletContext in it. Then just copy the ServletContext reference to a private class member.

    public class MyController implements ServletContextAware {
    
        private ServletContext servletContext;
    
        @Override
        setServletContext(ServletContext servletContext) {
            this.servletContext = servletContext;
        }
    }
    

    You can get the param-value with:

    String urlValue = servletContext.getInitParameter("baseUrl");
    

提交回复
热议问题