JavaConfig Spring 网络流 returns 404 未找到 (JSP)

JavaConfig Spring Web Flow returns 404 not found (JSP)

我一直在尝试 Spring Web Flow 2.4 的 Java 配置功能,方法是将现有项目从 xml 配置修改为 JavaConfig。 XML 版本有效,但 JavaConfig 无效。每次我尝试使用 URL http://localhost:8080/sia_p219_ch08_spring_web_flow_order_pizza_customer_flow_complete/pizza 启动流程时,它都会 returns 404。没有例外。控制台没有显示 "no request mapping found for..." 消息。网页显示The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

项目托管于 github, the working XML version is here

我认为问题是请求 URL 没有调用披萨流 (/WEB-INF/flows/pizza/pizza-flow.xml)。

以下是一些代码片段:

WebAppInitializer:

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { RootConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class };
}

@Override
// map DispatcherServlet to /
protected String[] getServletMappings() {
    return new String[] { "/" };
}

根配置:

@Configuration
@Import({WebFlowConfig.class})
public class RootConfig {}

WebFlowConfig:

@Configuration
@ComponentScan({"pizza"})
public class WebFlowConfig extends AbstractFlowConfiguration {
    static{
        System.out.println("WebFlowConfig loaded");
    }

    @Autowired
    private WebConfig webMvcConfig;

    @Bean
    public FlowDefinitionRegistry flowRegistry() {
        return 
            getFlowDefinitionRegistryBuilder(flowBuilderServices())
            .setBasePath("/WEB-INF/flows")
            .addFlowLocationPattern("/**/*-flow.xml")
            .build();
    }

    @Bean
    public FlowExecutor flowExecutor(){
        return getFlowExecutorBuilder(flowRegistry()).build();
    }

    @Bean
    public FlowBuilderServices flowBuilderServices() {
        return getFlowBuilderServicesBuilder()
                .setViewFactoryCreator(mvcViewFactoryCreator())
                .setDevelopmentMode(true)
                .build();
    }

    @Bean
    public MvcViewFactoryCreator mvcViewFactoryCreator() {
        MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
        factoryCreator.setViewResolvers(Collections.singletonList(this.webMvcConfig.viewResolver()));
        factoryCreator.setUseSpringBeanBinding(true);
        return factoryCreator;
    }
}

网络配置

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    static{
        System.out.println("WebConfig loaded");
    }

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

    // configure static content handling
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {           
        configurer.enable();
    }

流程定义文件和 JSP 很好,如果需要,您可以在 github 上查看它们。

非常感谢,非常感谢任何帮助。

目前我发现 WebFlowConfig 中的配置确实缺少这部分配置(详情请查看 documentation page for integration with Spring MVC):

@Bean 
@Autowired
public FlowHandlerAdapter flowHandlerAdapter(FlowExecutor flowExecutor) {
    FlowHandlerAdapter flowHandlerAdapter = new FlowHandlerAdapter();
    flowHandlerAdapter.setFlowExecutor(flowExecutor);

    return flowHandlerAdapter;
}

@Bean 
@Autowired
public FlowHandlerMapping flowHandlerMapping(FlowDefinitionRegistry flowDefinitionRegistry) {
    FlowHandlerMapping flowHandlerMapping = new FlowHandlerMapping();
    flowHandlerMapping.setFlowRegistry(flowDefinitionRegistry);
    flowHandlerMapping.setOrder(0);

    return flowHandlerMapping;
}

同时从 flowBuilderServices bean 定义中删除 mvcViewFactoryCreator 定义和 setViewFactoryCreator 调用。它对我有用。