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