简体   繁体   English

getServletConfig()。getServletContext()在Spring中等效

[英]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.

相关问题 getServletConfig()/ getServletContext()返回null值 - getServletConfig() / getServletContext() returning null value 使用 getServletConfig().getServletContext().getAttribute() (Java) 时获取 NPE - Getting NPE when using getServletConfig().getServletContext().getAttribute() (Java) GWT + Spring:getServletContext()调用的NullPointerException - GWT + Spring: NullPointerException on getServletContext() call Java的getServletContext()在常规Java类中是等价的吗? - Java's getServletContext() equivalent in regular Java class? 如何在spring mvc Controller中获取getServletContext() - how to get getServletContext() in spring mvc Controller Java(JSP / Servlet):相当于.jsp中的getServletContext() - Java (JSP/Servlet): equivalent of getServletContext() from inside a .jsp 旧版本的Java中的HttpServletRequest.getServletContext()等价于什么? - What is the equivalent of HttpServletRequest.getServletContext() in old version of java? req.getsession()。getservletcontext()和getservletcontext()之间的区别 - difference between req.getsession().getservletcontext() and getservletcontext() 空指针异常与getServletContext()吗? - Null pointer exception with getServletContext()? getServletContext().getResourceAsStream 中的 NullPointerException - NullPointerException in getServletContext().getResourceAsStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM