Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Spring WebContext in class annotated @controller

In Spring MVC with annotation, we mark any POJO with @Controller. In this controller we can get WebApplicationContext, using autowired property.

@Controller
public class HomePageController {

@Autowired
ApplicationContext act;

    @RequestMapping("/*.html")
    public String handleBasic(){
        SimpleDomain sd = (SimpleDomain)act.getBean("sd1");
        System.out.println(sd.getFirstProp());
        return "hello";
}

But in this approach we do not have servletContext handy with us. So is there way we can still use older way of getting WebApplicationContext ? i.e.

WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)

How will we get servletContext here ?

I am not facing any compulsion to use old way; so this question is just out of curiosity to check flexibility of spring. Also It can be a interview question.

like image 910
Kaushik Lele Avatar asked Nov 03 '12 06:11

Kaushik Lele


5 Answers

You can just inject it into your controller:

@Autowired private ServletContext servletContext;

Or take HttpServletRequest as a parameter and get it from there:

@RequestMapping(...)
public ModelAndView myMethod(HttpServletRequest request ...){
    ServletContext servletContext = request.getServletContext()
}
like image 169
Biju Kunjummen Avatar answered Oct 22 '22 06:10

Biju Kunjummen


The following is correct approach :

@Autowired
ServletContext context;

Otherwise instead of auto wiring the ServletContext, you can implement ServletContextAware. Spring will notice this when running in a web application context and inject the ServletContext. Read this.

like image 36
Jeevan Patil Avatar answered Oct 22 '22 05:10

Jeevan Patil


You can also do it inline:

@RequestMapping(value = "/demp", method = RequestMethod.PUT)
public String demo(@RequestBody String request) {
    HttpServletRequest re3 = ((ServletRequestAttributes) RequestContextHolder

            .getRequestAttributes()).getRequest();
    return "sfsdf";
 }
like image 20
Guy L Avatar answered Oct 22 '22 07:10

Guy L


You can implement an Interface from Spring called org.springframework.web.context.ServletContextAware

public class MyController implements ServletContextAware {
    private ServletContext servletContext; 

    @Override
    public void setServletContext(ServletContext servletContext) {
       this.servletContext=servletContext;
    }
}

Then you can use the servletContext any place in the class.

like image 28
sendon1982 Avatar answered Oct 22 '22 07:10

sendon1982


By accessing the session you can get the servlet context, sample code:

@Controller
public class MyController{

....
@RequestMapping(...)
public ModelAndView myMethod(HttpSession session ...){

WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext())

}

}

You can get the HttpSession from the HttpServletRequest also.

like image 21
ElderMael Avatar answered Oct 22 '22 05:10

ElderMael