From 300b9ab78d71b29256b73e6ff10f2a3cbfe07143 Mon Sep 17 00:00:00 2001 From: Stephen Braimah Date: Fri, 31 Mar 2017 10:51:39 +0000 Subject: [PATCH] BAEL-318: Guide to Spring WebFlow --- pom.xml | 1 + spring-mvc-webflow/.gitignore | 13 ++ spring-mvc-webflow/README.md | 8 + spring-mvc-webflow/pom.xml | 184 ++++++++++++++++++ .../org/baeldung/servlet/WebInitializer.java | 35 ++++ .../org/baeldung/spring/WebFlowConfig.java | 43 ++++ .../org/baeldung/spring/WebMvcConfig.java | 54 +++++ .../src/main/resources/flow-definition.xml | 33 ++++ .../src/main/resources/logback.xml | 20 ++ .../webapp/WEB-INF/flows/activation-flow.xml | 17 ++ .../main/webapp/WEB-INF/view/activation.jsp | 23 +++ .../src/main/webapp/WEB-INF/view/failure.jsp | 5 + .../src/main/webapp/WEB-INF/view/sample.jsp | 7 + .../src/main/webapp/WEB-INF/view/success.jsp | 5 + .../src/test/resources/.gitignore | 13 ++ 15 files changed, 461 insertions(+) create mode 100644 spring-mvc-webflow/.gitignore create mode 100644 spring-mvc-webflow/README.md create mode 100644 spring-mvc-webflow/pom.xml create mode 100644 spring-mvc-webflow/src/main/java/org/baeldung/servlet/WebInitializer.java create mode 100644 spring-mvc-webflow/src/main/java/org/baeldung/spring/WebFlowConfig.java create mode 100644 spring-mvc-webflow/src/main/java/org/baeldung/spring/WebMvcConfig.java create mode 100644 spring-mvc-webflow/src/main/resources/flow-definition.xml create mode 100644 spring-mvc-webflow/src/main/resources/logback.xml create mode 100644 spring-mvc-webflow/src/main/webapp/WEB-INF/flows/activation-flow.xml create mode 100644 spring-mvc-webflow/src/main/webapp/WEB-INF/view/activation.jsp create mode 100644 spring-mvc-webflow/src/main/webapp/WEB-INF/view/failure.jsp create mode 100644 spring-mvc-webflow/src/main/webapp/WEB-INF/view/sample.jsp create mode 100644 spring-mvc-webflow/src/main/webapp/WEB-INF/view/success.jsp create mode 100644 spring-mvc-webflow/src/test/resources/.gitignore diff --git a/pom.xml b/pom.xml index 19bec5bf81..1d51bb710c 100644 --- a/pom.xml +++ b/pom.xml @@ -127,6 +127,7 @@ spring-mvc-tiles spring-mvc-velocity spring-mvc-web-vs-initializer + spring-mvc-webflow spring-mvc-xml spring-mvc-simple spring-openid diff --git a/spring-mvc-webflow/.gitignore b/spring-mvc-webflow/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-mvc-webflow/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-mvc-webflow/README.md b/spring-mvc-webflow/README.md new file mode 100644 index 0000000000..e627fdbffe --- /dev/null +++ b/spring-mvc-webflow/README.md @@ -0,0 +1,8 @@ +========= + +## Spring MVC with Spring Web Flow + +###The Course + +### Relevant Articles: +- diff --git a/spring-mvc-webflow/pom.xml b/spring-mvc-webflow/pom.xml new file mode 100644 index 0000000000..e3c3bf92da --- /dev/null +++ b/spring-mvc-webflow/pom.xml @@ -0,0 +1,184 @@ + + 4.0.0 + com.baeldung + 0.1-SNAPSHOT + spring-mvc-webflow + + spring-mvc-webflow + war + + + + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + + + org.springframework.webflow + spring-webflow + ${spring.webflow} + + + + + + javax.servlet + javax.servlet-api + ${javax.servlet-api.version} + provided + + + + javax.servlet + jstl + ${jstl.version} + runtime + + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + + + junit + junit + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + spring-mvc-webflow + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + + + + + + + + + 4.3.4.RELEASE + + + 2.4.4.RELEASE + + + 1.7.21 + 1.1.7 + + + 1.3 + 4.12 + 1.10.19 + + 3.1.0 + 1.2 + + 4.4.5 + 4.5.2 + + 2.9.0 + + + 3.6.0 + 2.6 + 2.19.1 + 2.7 + 1.6.1 + + + + \ No newline at end of file diff --git a/spring-mvc-webflow/src/main/java/org/baeldung/servlet/WebInitializer.java b/spring-mvc-webflow/src/main/java/org/baeldung/servlet/WebInitializer.java new file mode 100644 index 0000000000..2f52cad804 --- /dev/null +++ b/spring-mvc-webflow/src/main/java/org/baeldung/servlet/WebInitializer.java @@ -0,0 +1,35 @@ +package org.baeldung.servlet; + +import javax.servlet.ServletRegistration.Dynamic; + +import org.baeldung.spring.WebFlowConfig; +import org.baeldung.spring.WebMvcConfig; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + + public WebInitializer() { + super(); + } + + // API + protected Class[] getRootConfigClasses() { + return new Class[] { WebMvcConfig.class, WebFlowConfig.class }; + } + + @Override + protected Class[] getServletConfigClasses() { + return null; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/" }; + } + + @Override + protected void customizeRegistration(final Dynamic registration) { + super.customizeRegistration(registration); + } + +} diff --git a/spring-mvc-webflow/src/main/java/org/baeldung/spring/WebFlowConfig.java b/spring-mvc-webflow/src/main/java/org/baeldung/spring/WebFlowConfig.java new file mode 100644 index 0000000000..3d2de2a014 --- /dev/null +++ b/spring-mvc-webflow/src/main/java/org/baeldung/spring/WebFlowConfig.java @@ -0,0 +1,43 @@ +package org.baeldung.spring; + +import java.util.Collections; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.webflow.config.AbstractFlowConfiguration; +import org.springframework.webflow.definition.registry.FlowDefinitionRegistry; +import org.springframework.webflow.engine.builder.support.FlowBuilderServices; +import org.springframework.webflow.executor.FlowExecutor; +import org.springframework.webflow.mvc.builder.MvcViewFactoryCreator; + +@Configuration +public class WebFlowConfig extends AbstractFlowConfiguration { + + @Autowired + private WebMvcConfig webMvcConfig; + + @Bean + public FlowDefinitionRegistry flowRegistry() { + return getFlowDefinitionRegistryBuilder(flowBuilderServices()).addFlowLocation("/WEB-INF/flows/activation-flow.xml", "activationFlow").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; + } + +} diff --git a/spring-mvc-webflow/src/main/java/org/baeldung/spring/WebMvcConfig.java b/spring-mvc-webflow/src/main/java/org/baeldung/spring/WebMvcConfig.java new file mode 100644 index 0000000000..0f0f1bacdb --- /dev/null +++ b/spring-mvc-webflow/src/main/java/org/baeldung/spring/WebMvcConfig.java @@ -0,0 +1,54 @@ +package org.baeldung.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; +import org.springframework.webflow.mvc.servlet.FlowHandlerAdapter; +import org.springframework.webflow.mvc.servlet.FlowHandlerMapping; + +@EnableWebMvc // +@Configuration +// @ImportResource("classpath:/flow-definition.xml") +public class WebMvcConfig extends WebMvcConfigurerAdapter { + + @Autowired + private WebFlowConfig webFlowConfig; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Bean + public InternalResourceViewResolver viewResolver() { + InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setViewClass(JstlView.class); + viewResolver.setPrefix("/WEB-INF/view/"); + viewResolver.setSuffix(".jsp"); + return viewResolver; + } + + @Bean + public FlowHandlerMapping flowHandlerMapping() { + FlowHandlerMapping handlerMapping = new FlowHandlerMapping(); + handlerMapping.setOrder(-1); + handlerMapping.setFlowRegistry(this.webFlowConfig.flowRegistry()); + return handlerMapping; + } + + @Bean + public FlowHandlerAdapter flowHandlerAdapter() { + FlowHandlerAdapter handlerAdapter = new FlowHandlerAdapter(); + handlerAdapter.setFlowExecutor(this.webFlowConfig.flowExecutor()); + handlerAdapter.setSaveOutputToFlashScopeOnRedirect(true); + return handlerAdapter; + } + +} diff --git a/spring-mvc-webflow/src/main/resources/flow-definition.xml b/spring-mvc-webflow/src/main/resources/flow-definition.xml new file mode 100644 index 0000000000..0240fa6dcb --- /dev/null +++ b/spring-mvc-webflow/src/main/resources/flow-definition.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-mvc-webflow/src/main/resources/logback.xml b/spring-mvc-webflow/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/spring-mvc-webflow/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-mvc-webflow/src/main/webapp/WEB-INF/flows/activation-flow.xml b/spring-mvc-webflow/src/main/webapp/WEB-INF/flows/activation-flow.xml new file mode 100644 index 0000000000..ab9eb1a9b7 --- /dev/null +++ b/spring-mvc-webflow/src/main/webapp/WEB-INF/flows/activation-flow.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-mvc-webflow/src/main/webapp/WEB-INF/view/activation.jsp b/spring-mvc-webflow/src/main/webapp/WEB-INF/view/activation.jsp new file mode 100644 index 0000000000..ab1d8abfd1 --- /dev/null +++ b/spring-mvc-webflow/src/main/webapp/WEB-INF/view/activation.jsp @@ -0,0 +1,23 @@ + + +

Hello World!

+<%--Start--%> +<%----%> +<%----%> + +
+ + + + +
+ +
+ + + + +
+ + + diff --git a/spring-mvc-webflow/src/main/webapp/WEB-INF/view/failure.jsp b/spring-mvc-webflow/src/main/webapp/WEB-INF/view/failure.jsp new file mode 100644 index 0000000000..c0945650c9 --- /dev/null +++ b/spring-mvc-webflow/src/main/webapp/WEB-INF/view/failure.jsp @@ -0,0 +1,5 @@ + + +

Activation Failed

+ + \ No newline at end of file diff --git a/spring-mvc-webflow/src/main/webapp/WEB-INF/view/sample.jsp b/spring-mvc-webflow/src/main/webapp/WEB-INF/view/sample.jsp new file mode 100644 index 0000000000..7cc14b5dcd --- /dev/null +++ b/spring-mvc-webflow/src/main/webapp/WEB-INF/view/sample.jsp @@ -0,0 +1,7 @@ + + + + +

This is the body of the sample view

+ + \ No newline at end of file diff --git a/spring-mvc-webflow/src/main/webapp/WEB-INF/view/success.jsp b/spring-mvc-webflow/src/main/webapp/WEB-INF/view/success.jsp new file mode 100644 index 0000000000..ab6163818c --- /dev/null +++ b/spring-mvc-webflow/src/main/webapp/WEB-INF/view/success.jsp @@ -0,0 +1,5 @@ + + +

Activation Successful!

+ + \ No newline at end of file diff --git a/spring-mvc-webflow/src/test/resources/.gitignore b/spring-mvc-webflow/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-mvc-webflow/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file