如何在带注释的@controller 类中获取 Spring WebContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13206588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:29:12  来源:igfitidea点击:

How to get Spring WebContext in class annotated @controller

springservletsspring-mvcautowiredapplicationcontext

提问by Kaushik Lele

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

在带有注解的 Spring MVC 中,我们用 @Controller 标记任何 POJO。在这个控制器中,我们可以使用自动装配的属性获取 WebApplicationContext。

@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.

但是在这种方法中,我们手头没有 servletContext。那么有没有办法我们仍然可以使用旧的方式来获取 WebApplicationContext ?IE

WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)

How will we get servletContext here ?

我们将如何在这里获得 servletContext ?

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.

我没有面临使用旧方式的任何强迫;所以这个问题只是出于好奇来检查弹簧的灵活性。也可以是面试题。

回答by Biju Kunjummen

You can just inject it into your controller:

您可以将其注入控制器:

@Autowired private ServletContext servletContext;

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

或者将 HttpServletRequest 作为参数并从那里获取它:

@RequestMapping(...)
public ModelAndView myMethod(HttpServletRequest request ...){
    ServletContext servletContext = request.getServletContext()
}

回答by Jeevan Patil

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.

否则,您可以实现 ServletContextAware,而不是自动连接 ServletContext。Spring 在 Web 应用程序上下文中运行时会注意到这一点并注入 ServletContext。读这个

回答by Guy L

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";
 }

回答by sendon1982

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

您可以从 Spring 实现一个名为的接口 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 servletContextany place in the class.

然后你可以servletContext在类中的任何地方使用。

回答by ElderMael

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

通过访问会话,您可以获得 servlet 上下文,示例代码:

@Controller
public class MyController{

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

WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext())

}

}

You can get the HttpSession from the HttpServletRequest also.

您也可以从 HttpServletRequest 获取 HttpSession。