繁体   English   中英

如何在Spring控制器中获取调度程序Servlet名称

[英]How to get dispatcher servlet name in Spring controller

下面是我的web.xml

<servlet>
    <servlet-name>DispatcherName</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

有什么方法可以在我的应用程序控制器中获得servlet名称“ DispatcherName”?

我希望它可以从XMLWebApplicationContext访问控制器对象,并且需要RequestDispatcher Name。 到现在为止,这就是我尝试过的方法:

webApplicationContext=WebApplicationContextUtils.getWebApplicationContext(GetServletContextWebListner.getServletContext());     
XmlWebApplicationContext xmlWebApplicationContext = (XmlWebApplicationContext)GetServletContextWebListner.getServletContext().getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT."+webApplicationContext.getApplicationName().replace("/", ""));

并尝试了这个

@WebListener
public class GetServletContextWebListner implements ServletContextListener {
private static ServletContext servletContext;

public static ServletContext getServletContext() {
    return servletContext;
}

@Override
public void contextInitialized(ServletContextEvent sce) {
    servletContext = sce.getServletContext();
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    servletContext = null;
}    
}

   (XmlWebApplicationContext)GetServletContextWebListner.getServletContext().getServletContextName()

由于我无法获取servlet名称,因此我使用的是getApplicationName(),但这可能因servlet名称而异。

在您的控制器中,您可以尝试:

request.getServletContext().getServletContextName()

要么

RequestContextUtils.getWebApplicationContext(request).getDisplayName()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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