[英]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. 我希望从我的Java类本身获取它,而不是在jsp中使用EL。
How to get the servlet context path in Spring? 如何在Spring中获取servlet上下文路径?
for SpringMVC 对于SpringMVC
@Autowired
ServletContext servletContext;
Another way is implementing ServletConfigAware in the class that depends on ServletContext
. 另一种方式是实现ServletConfigAware在依赖于类ServletContext
。 In the setServletConfig
method you'll get an instance of ServletContext
and you can do there what you have to do. 在setServletConfig
方法中,您将获得ServletContext
一个实例,您可以在那里做你需要做的事情。
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 这里发布了一个解决方案: ServletContext和Spring MVC
@Autowired
ServletContext context;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.