Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
Java 在spring应用程序中使用多个dispatcher servlet_Java_Spring_Spring Mvc - Fatal编程技术网

Java 在spring应用程序中使用多个dispatcher servlet

Java 在spring应用程序中使用多个dispatcher servlet,java,spring,spring-mvc,Java,Spring,Spring Mvc,在我的spring应用程序中,我为spring环境提供了以下配置类: WebAppInitializer.java @Order(value=1) public class WebAppInitializer implements WebApplicationInitializer { @SuppressWarnings("resource") @Override public void onStartup(ServletContext container) {

在我的spring应用程序中,我为spring环境提供了以下配置类:

WebAppInitializer.java

@Order(value=1)
public class WebAppInitializer implements WebApplicationInitializer {

    @SuppressWarnings("resource")
    @Override
    public void onStartup(ServletContext container) {
      // Create the 'root' Spring application context
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.register(WebAppConfig.class);

      // Manage the lifecycle of the root application context
      //container.addListener(new ContextLoaderListener(rootContext));

      // Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(DispatcherConfig.class);

      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
    }

}
@EnableWebMvc
@EnableTransactionManagement(mode=AdviceMode.PROXY, proxyTargetClass=true)
@ComponentScan(value="spring.webapp.lojavirtual")
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/").setCachePeriod(31556926);
        registry.addResourceHandler("/extras/**").addResourceLocations("/extras/").setCachePeriod(31556926);
        registry.addResourceHandler("/jquery/**").addResourceLocations("/jquery/").setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}
@Controller
@Import(WebAppConfig.class)
public class DispatcherConfig {

    @Bean
    public ViewResolver jspResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}
WebAppConfig.java

@Order(value=1)
public class WebAppInitializer implements WebApplicationInitializer {

    @SuppressWarnings("resource")
    @Override
    public void onStartup(ServletContext container) {
      // Create the 'root' Spring application context
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.register(WebAppConfig.class);

      // Manage the lifecycle of the root application context
      //container.addListener(new ContextLoaderListener(rootContext));

      // Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(DispatcherConfig.class);

      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
    }

}
@EnableWebMvc
@EnableTransactionManagement(mode=AdviceMode.PROXY, proxyTargetClass=true)
@ComponentScan(value="spring.webapp.lojavirtual")
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/").setCachePeriod(31556926);
        registry.addResourceHandler("/extras/**").addResourceLocations("/extras/").setCachePeriod(31556926);
        registry.addResourceHandler("/jquery/**").addResourceLocations("/jquery/").setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}
@Controller
@Import(WebAppConfig.class)
public class DispatcherConfig {

    @Bean
    public ViewResolver jspResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}
DispatcherConfig.java

@Order(value=1)
public class WebAppInitializer implements WebApplicationInitializer {

    @SuppressWarnings("resource")
    @Override
    public void onStartup(ServletContext container) {
      // Create the 'root' Spring application context
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.register(WebAppConfig.class);

      // Manage the lifecycle of the root application context
      //container.addListener(new ContextLoaderListener(rootContext));

      // Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(DispatcherConfig.class);

      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
    }

}
@EnableWebMvc
@EnableTransactionManagement(mode=AdviceMode.PROXY, proxyTargetClass=true)
@ComponentScan(value="spring.webapp.lojavirtual")
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/").setCachePeriod(31556926);
        registry.addResourceHandler("/extras/**").addResourceLocations("/extras/").setCachePeriod(31556926);
        registry.addResourceHandler("/jquery/**").addResourceLocations("/jquery/").setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}
@Controller
@Import(WebAppConfig.class)
public class DispatcherConfig {

    @Bean
    public ViewResolver jspResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}
我想将其他DispatcherServlet添加到我的应用程序中。我的第一个想法是将以下代码添加到上面的类中:

WebAppInitializer.java中的

@Order(value=1)
public class WebAppInitializer implements WebApplicationInitializer {

    @SuppressWarnings("resource")
    @Override
    public void onStartup(ServletContext container) {
      // Create the 'root' Spring application context
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.register(WebAppConfig.class);

      // Manage the lifecycle of the root application context
      //container.addListener(new ContextLoaderListener(rootContext));

      // Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(DispatcherConfig.class);

      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
    }

}
@EnableWebMvc
@EnableTransactionManagement(mode=AdviceMode.PROXY, proxyTargetClass=true)
@ComponentScan(value="spring.webapp.lojavirtual")
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/").setCachePeriod(31556926);
        registry.addResourceHandler("/extras/**").addResourceLocations("/extras/").setCachePeriod(31556926);
        registry.addResourceHandler("/jquery/**").addResourceLocations("/jquery/").setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}
@Controller
@Import(WebAppConfig.class)
public class DispatcherConfig {

    @Bean
    public ViewResolver jspResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}
这样的新块,在适当的位置更改名称:

// Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(DispatcherConfig.class);

      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
并添加一个新类,如DispatcherConfig.java,名称在上面的代码中选择

我的问题是:

1) 首先,我的方法是添加新的DispatcherServlet的正确方法吗

2) 第二,如果问题1的答案是“是”,我应该在WebAppInitializer中更改哪些名称

3) 在我的控制器中,我如何确定我的请求应该放在哪个调度员servlet中?我的控制器使用如下方法调用视图:

@RequestMapping(value="view_mapping")
public method() {
    ModelAndView mav = new ModelAndView()
    mav.setViewName("view_name");
    return mav;
}

您可以拥有任意数量的
Dispatcherservlet
。基本上,您需要做的是复制配置并给servlet一个不同的名称(否则它将覆盖上一个名称),并为其提供一些单独的配置类(或xml文件)

您的控制器不应该关心它们在哪个
DispatcherServlet
中运行,您也不应该包含用于检测的代码(如果您添加了另一个,那么您需要不断修改控制器以修复该问题)


然而,虽然您通常可以有多个servlet,但并不需要多个servlet,您可以使用
DispatcherServlet
的单个实例来处理它

如果您使用的是spring 3.2或更高版本,则可以使用以下代码

使用重写
getServletName()
方法为所有
dispacher servlet
创建不同的类,以避免相同的名称冲突

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            // TODO Auto-generated method stub
            return new Class<?>[] { RootConfig.class };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            // TODO Auto-generated method stub
            return new Class<?>[] { WebConfig.class };
        }

        @Override
        protected String[] getServletMappings() {
            // TODO Auto-generated method stub
            return new String[] { "/config1/*" };
        }
    }




 public class WebAppInitializer2 extends AbstractAnnotationConfigDispatcherServletInitializer {


        @Override
        protected Class<?>[] getRootConfigClasses() {
            // TODO Auto-generated method stub
            return new Class<?>[] { RootConfig.class };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            // TODO Auto-generated method stub
            return new Class<?>[] { WebConfig2.class };
        }

        @Override
        protected String[] getServletMappings() {
            // TODO Auto-generated method stub
            return new String[] { "/config2/*" };
        }

           @Override
            protected String getServletName() {
              // TODO Auto-generated method stub
             return "config2";
         }
    }
公共类WebAppInitializer扩展了AbstractAnnotationConfigDispatchersServletInitializer{
@凌驾
受保护类[]getRootConfigClasses(){
//TODO自动生成的方法存根
返回新类[]{RootConfig.Class};
}
@凌驾
受保护类[]getServletConfigClasses(){
//TODO自动生成的方法存根
返回新类[]{WebConfig.Class};
}
@凌驾
受保护的字符串[]getServletMappings(){
//TODO自动生成的方法存根
返回新字符串[]{“/config1/*”};
}
}
公共类WebAppInitializer2扩展了AbstractAnnotationConfigDispatcherServletInitializer{
@凌驾
受保护类[]getRootConfigClasses(){
//TODO自动生成的方法存根
返回新类[]{RootConfig.Class};
}
@凌驾
受保护类[]getServletConfigClasses(){
//TODO自动生成的方法存根
返回新类[]{webconfig.Class};
}
@凌驾
受保护的字符串[]getServletMappings(){
//TODO自动生成的方法存根
返回新字符串[]{“/config2/*”};
}
@凌驾
受保护的字符串getServletName(){
//TODO自动生成的方法存根
返回“config2”;
}
}

我们可以有多个DispatcherServlet,就像我们可以有2个(或更多)DispatcherServlet和2个(或更多)Servlet名称一样。因此D1和D2可以映射到不同的URL路径。例如:-

<!-- configured by WEB-INF/mac-servlet.xml -->
<servlet>
    <servlet-name>mac</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- configured by WEB-INF/windows-servlet.xml -->
<servlet>
        <servlet-name>windows</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>

雨衣
org.springframework.web.servlet.DispatcherServlet
1.
窗户
org.springframework.web.servlet.DispatcherServlet
1.
URL路径可以映射为:-

<servlet-mapping>
<servlet-name>mac</servlet-name>
<url-pattern>/mac/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>windows</servlet-name>
     <url-pattern>/windows/*</url-pattern>
</servlet-mapping>

雨衣
/苹果/*
窗户
/窗户/*

您几乎总是不应该有多个servlet,而是应该将所有需要的映射添加到一个servlet中。为什么您认为需要更多?根配置类不是所有dispatcher servlet都通用吗?在您的示例中,这一事实意味着什么?要创建多个web应用初始化器,每个web应用初始化器类都应该实现
WebApplicationInitializer
接口,而不是扩展
AbstractAnnotationConfigDispatcherServletInitializer
类请,你能告诉我使用多个Dispatcherservlet有什么好处吗?好的。事实上,人们在这里投票反对这类问题。我仍将尝试现在提出的问题: