Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 3,AbstractAnnotationConfigDispatcherServletInitializer,多个servlet_Spring_Servlet 3.0 - Fatal编程技术网

Spring 3,AbstractAnnotationConfigDispatcherServletInitializer,多个servlet

Spring 3,AbstractAnnotationConfigDispatcherServletInitializer,多个servlet,spring,servlet-3.0,Spring,Servlet 3.0,在Servlet2.5中,通过简单地复制和编辑以下xml标记,就可以使用web.xml文件中配置的多个Servlet <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>

在Servlet2.5中,通过简单地复制和编辑以下xml标记,就可以使用web.xml文件中配置的多个Servlet

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

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/
使用Spring的AbstractAnnotationConfigDispatchersServletInitializer和Servlet3是否可以创建多个Servlet

我认为在getServletConfigClasses()方法中返回2个类,在getServletMappings()方法中返回2个路径就足够了,但这并没有像我预期的那样有效

那么,有没有一种(简单的)方法可以使用Spring3和Servlet3配置多个Servlet呢


谢谢你的回答

您可以执行以下操作:

public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {

      XmlWebApplicationContext appContext = new XmlWebApplicationContext();
      appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");

     ServletRegistration.Dynamic dispatcher =
        container.addServlet("dispatcher", new DispatcherServlet(appContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");

     ServletRegistration.Dynamic anotherServlet =
        container.addServlet("anotherServlet", "com.xxx.AnotherServlet");
      anotherServlet.setLoadOnStartup(2);
      anotherServlet.addMapping("/another/*");

     ServletRegistration.Dynamic yetAnotherServlet =
        container.addServlet("yetAnotherServlet", "com.xxx.YetAnotherServlet");
      yetAnotherServlet.setLoadOnStartup(3);
      yetAnotherServlet.addMapping("/yetanother/*");

    }

 }

当然,您可以根据自己的方便使用任何方法。

您可以执行以下操作:

public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {

      XmlWebApplicationContext appContext = new XmlWebApplicationContext();
      appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");

     ServletRegistration.Dynamic dispatcher =
        container.addServlet("dispatcher", new DispatcherServlet(appContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");

     ServletRegistration.Dynamic anotherServlet =
        container.addServlet("anotherServlet", "com.xxx.AnotherServlet");
      anotherServlet.setLoadOnStartup(2);
      anotherServlet.addMapping("/another/*");

     ServletRegistration.Dynamic yetAnotherServlet =
        container.addServlet("yetAnotherServlet", "com.xxx.YetAnotherServlet");
      yetAnotherServlet.setLoadOnStartup(3);
      yetAnotherServlet.addMapping("/yetanother/*");

    }

 }

当然,您可以根据自己的方便使用任何一种方法。

太好了!我甚至不知道ServletRegistration班。非常感谢。完美的我甚至不知道ServletRegistration班。非常感谢。