简体   繁体   中英

getServletConfig().getServletContext() equivalent in Spring

I referred to a lot of posts but still I am unable to find a correct working answer.
I want to get it from my Java class itself and not using EL in jsp.

How to get the servlet context path in Spring?

for SpringMVC

@Autowired
ServletContext servletContext;

Another way is implementing ServletConfigAware in the class that depends on ServletContext . In the setServletConfig method you'll get an instance of ServletContext and you can do there what you have to do.

public class MyClass implements ServletConfigAware {

    private ServletConfig config;

    public void setServletConfig(ServletConfig servletConfig) {
        this.config = servletConfig;
    }

A solution is posted here: ServletContext and Spring MVC

@Autowired
ServletContext context;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM