基于 DWR Spring-MVC 4.0.5 java 的配置

DWR Spring-MVC 4.0.5 java based configuration

我正在尝试在使用 spring mvc 4.0.5 的项目中包含 dwr。 Java 项目中使用了基于配置。 对于基于 java 的配置,我在 MVCConfiguration 中使用以下代码行。

@Bean
    public DwrController dwrController(){
        Map<String,String> configParam = new HashMap<String, String>();
        configParam.put("activeReverseAjaxEnabled","true");

        DwrController dwrController = new DwrController();
        dwrController.setDebug(true);
        dwrController.setConfigParams(configParam);
/*
        AnnotationsConfigurator annotationsConfigurator = new AnnotationsConfigurator();
        annotationsConfigurator.configure();
        dwrController.setConfigurators(configurators);
*/
        List<Configurator> configurators = new ArrayList<Configurator>();
        configurators.add(annotationsConfigurator);
        return dwrController;
    }
    @Bean
    public BeanNameUrlHandlerMapping beanNameUrlHandlerMapping(){
        BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
        return beanNameUrlHandlerMapping;
    }
    @Bean
    public DwrHandlerMapping dwrHandlerMapping(){
        Map<String,String> urlMap = new HashMap<String, String>();
        urlMap.put("/dwr/**/*","dwrController");

        DwrHandlerMapping dwrHandlerMapping = new DwrHandlerMapping();
        dwrHandlerMapping.setAlwaysUseFullPath(true);
        dwrHandlerMapping.setUrlMap(urlMap);
        return dwrHandlerMapping;
    }

WebApplicationInitializer 扩展 class 我已经为 dwr 请求添加了映射。

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

但在部署时显示错误 "The required 'configurators' property should be set"。 您能否建议配置是否需要任何修改以及如何设置配置器。

将 dwrController() 方法更改为:

@Bean
    @Autowired
    public DwrController dwrController(BeanFactory beanFactory){
        Map<String,String> configParam = new HashMap<String, String>();
        configParam.put("activeReverseAjaxEnabled","true");

        DwrController dwrController = new DwrController();
        dwrController.setDebug(true);
        dwrController.setConfigParams(configParam);

        AnnotationsConfigurator annotationsConfigurator = new AnnotationsConfigurator();
        SpringContainer springContainer = new SpringContainer();
        springContainer.setBeanFactory(beanFactory);
        annotationsConfigurator.configure(springContainer);
        List<Configurator> configurators = new ArrayList<Configurator>();
        configurators.add(annotationsConfigurator);
        dwrController.setConfigurators(configurators);
        return dwrController;
    }

现在正在部署应用程序,但是对于 /dwr/engine.js 和其他请求,它给出了错误 The requested resource is not available.

谢谢。

您已创建列表但尚未实际设置。添加以下行:

dwrController.setConfigurators(configurators);

甚至更好...在一行中完成所有操作

dwrController.setConfigurators(Arrays.asList(new AnnotationsConfigurator()));

我终于得到了工作配置。这是我的配置class的代码

    @Bean
    public DwrController dwrController(ApplicationContext applicationContext){

        BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry)applicationContext.getAutowireCapableBeanFactory();
        Map<String,String> configParam = new HashMap<String, String>();
        configParam.put("activeReverseAjaxEnabled","true");

        ClassPathBeanDefinitionScanner scanner = new DwrClassPathBeanDefinitionScanner(beanDefinitionRegistry);
        scanner.addIncludeFilter(new AnnotationTypeFilter(RemoteProxy.class));
        scanner.addIncludeFilter(new AnnotationTypeFilter(DataTransferObject.class));
        scanner.addIncludeFilter(new AnnotationTypeFilter(GlobalFilter.class));
        scanner.scan("package.to.scan");

        DwrController dwrController = new DwrController();
        dwrController.setDebug(true);
        dwrController.setConfigParams(configParam);

        SpringConfigurator springConfigurator = new SpringConfigurator();
        List<Configurator> configurators = new ArrayList<Configurator>();
        configurators.add(springConfigurator);
        dwrController.setConfigurators(configurators);


        return dwrController;
    }
    @Bean
    public BeanNameUrlHandlerMapping beanNameUrlHandlerMapping(){
        BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
        return beanNameUrlHandlerMapping;
    }
    @Bean
    public DwrHandlerMapping dwrHandlerMapping(DwrController dwrController){
        Map<String,DwrController> urlMap = new HashMap<String, DwrController>();
        urlMap.put("/dwr/**/*",dwrController);

        DwrHandlerMapping dwrHandlerMapping = new DwrHandlerMapping();
        dwrHandlerMapping.setAlwaysUseFullPath(true);
        dwrHandlerMapping.setUrlMap(urlMap);
        return dwrHandlerMapping;
    }

在实现 class as

的 WebApplicationInitializer 中添加了调度程序映射
dispatcher.addMapping("/dwr/*");
dispatcher.addMapping("/");

注意:DwrClassPathBeanDefinitionScanner 将跳过 @Controller,@Service,@Repository,@Component 的扫描,因此不要将 @RemoteProxy 与这些 spring 注释中的任何一个一起使用。