Skip to content

Instantly share code, notes, and snippets.

@noriyukitakei
Created April 16, 2018 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noriyukitakei/f89b4b01d573e96ec0ac6d2132e08672 to your computer and use it in GitHub Desktop.
Save noriyukitakei/f89b4b01d573e96ec0ac6d2132e08672 to your computer and use it in GitHub Desktop.
Spring Web Flowによる今どきでないWebアプリケーション開発
package com.sios.flow;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.webflow.config.AbstractFlowConfiguration;
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
import org.springframework.webflow.engine.builder.ViewFactoryCreator;
import org.springframework.webflow.engine.builder.support.FlowBuilderServices;
import org.springframework.webflow.executor.FlowExecutor;
import org.springframework.webflow.mvc.builder.MvcViewFactoryCreator;
import org.springframework.webflow.mvc.portlet.FlowHandlerAdapter;
import org.springframework.webflow.mvc.servlet.FlowHandlerMapping;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.AjaxThymeleafViewResolver;
import org.thymeleaf.spring4.view.FlowAjaxThymeleafView;
@Configuration
public class WebFlowConfig extends AbstractFlowConfiguration{
@Autowired
SpringTemplateEngine templateEngine;
@Bean
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(false);
return filter;
}
/**
* フロー定義を配置するルートフォルダを定義します。
* 下記の設定では、templatesフォルダ配下にFlow定義ファイルを配置します。
* そして、templatesフォルダ配下に1つフォルダを作成して、
* そこにflow-*.xmlというファイル名とします。今回は、flow-bbs.xmlとしています。
*
* @return
*/
@Bean
public FlowDefinitionRegistry flowRegistry() {
return getFlowDefinitionRegistryBuilder()
.setBasePath("classpath:templates")
.addFlowLocationPattern("/**/flow-*.xml")
.setFlowBuilderServices(flowBuilderServices()).build();
}
@Bean
public FlowBuilderServices flowBuilderServices() {
return getFlowBuilderServicesBuilder()
.setViewFactoryCreator(viewFactoryCreator())
.setValidator(validator())
.setDevelopmentMode(true)
.build();
}
@Bean
public Validator validator(){
return new LocalValidatorFactoryBean();
}
@Bean
public ViewFactoryCreator viewFactoryCreator() {
MvcViewFactoryCreator bean = new MvcViewFactoryCreator();
bean.setViewResolvers(Arrays.asList(viewResolver()));
bean.setUseSpringBeanBinding(true);
return bean;
}
@Bean
public FlowExecutor flowExecutor() {
return getFlowExecutorBuilder(flowRegistry())
.build();
}
@Bean
public org.springframework.webflow.mvc.servlet.FlowHandlerAdapter flowHandler() {
org.springframework.webflow.mvc.servlet.FlowHandlerAdapter bean = new org.springframework.webflow.mvc.servlet.FlowHandlerAdapter();
bean.setFlowExecutor(flowExecutor());
bean.setSaveOutputToFlashScopeOnRedirect(true);
return bean;
}
@Bean
public FlowHandlerMapping flowHandlerMapping() {
FlowHandlerMapping bean = new FlowHandlerMapping();
bean.setFlowRegistry(flowRegistry());
bean.setOrder(0);
return bean;
}
@Bean
public AjaxThymeleafViewResolver viewResolver() {
AjaxThymeleafViewResolver viewResolver = new AjaxThymeleafViewResolver();
viewResolver.setViewClass(FlowAjaxThymeleafView.class);
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment