2016-12-02 22 views
0

下面是我的web.xml如何在Spring控制器得到调度servlet名称

<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名称。 到目前为止,这是我已经试过:

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名称而异。

+0

那你试试这么远吗? –

+0

@KurtVandenBranden增加了问题 –

+0

不要......你应该首先使用依赖注入,其次'WebApplicationContextUtils'只会给你根上下文,而不是'DispatcherServlet'中的那个。 –

回答

1

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

request.getServletContext().getServletContextName() 

或者

​​
+0

我没有请求对象&getServletContextName不返回我的调度程序servlet名称 –

+0

请问您可以添加您的控制器方法吗? –

+0

我已经更新了我的答案。 –

相关问题