编程语言
首页 > 编程语言> > JavaConfig Spring Web Flow返回404未找到(JSP)

JavaConfig Spring Web Flow返回404未找到(JSP)

作者:互联网

我一直在尝试通过将现有项目从xml配置修改为JavaConfig来实现Spring Web Flow 2.4的Java配置功能. XML版本有效,但JavaConfig没有.每次我尝试使用URL http://localhost:8080/sia_p219_ch08_spring_web_flow_order_pizza_customer_flow_complete/pizza启动流程时,它都会返回404.没有例外.控制台显示没有“找不到请求映射…”消息.该网页显示原始服务器未找到目标资源的当前表示,或者不愿意透露该目标资源是否存在.

该项目于github托管,工作XML版本为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[] { "/" };
}

RootConfig:

@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;
    }
}

WebConfig

@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调用.这个对我有用.

标签:java,jsp,spring,spring-webflow,spring-java-config
来源: https://codeday.me/bug/20190705/1390897.html