下面是我的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名称而异。
那你试试这么远吗? –
@KurtVandenBranden增加了问题 –
不要......你应该首先使用依赖注入,其次'WebApplicationContextUtils'只会给你根上下文,而不是'DispatcherServlet'中的那个。 –