可能已经过时的Spring Web Flow简介2
环境的设置
可能不需要,但嗯,我会继续做。
我将尝试创建一个简单的应用程序,用于注册和查看图书信息。
首先,我会使用常规的Spring MVC(Spring Boot)控制器来创建菜单页面。
项目结构
src/main/java: 项目主体
src/main/resources/templates: 模板根目录和流程定义根目录
src/test/groovy: 使用Spock进行测试。
build.gradle: 构建文件,使用gradle。
※我已在ggts 3.6.3中进行了测试确认。要执行测试,需要将Groovy编译器版本降级至2.0。从Gradle中应该可以继续运行。
构建文件
使用Gradle进行。
buildscript {
    ext {
        springBootVersion = '1.2.0.RELEASE'
    }
    repositories {
        // NOTE: You should declare only repositories that you need here
        mavenLocal()
        mavenCentral()
        maven { url "http://repo.spring.io/release" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url "http://repo.spring.io/snapshot" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'eclipse'
//apply plugin: 'idea'
apply plugin: 'spring-boot'
def webFlowVersion = '2.4.1.RELEASE'
jar {
    baseName = 'flowApp'
    version =  '0.1.0'
}
run {
  systemProperties = System.properties
}
repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/release" }
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")   
    compile ("org.springframework.webflow:spring-webflow:${webFlowVersion}")
    compile ("org.springframework.webflow:spring-js:${webFlowVersion}")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("org.spockframework:spock-spring:0.7-groovy-2.0")
}
task wrapper(type: Wrapper) {
    gradleVersion = '1.6'
}
启动类
package flowapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplication(Application.class).run(args);
    }
}
控制器 qì)
package flowapp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MenuController {
    @RequestMapping("/")
    public String menu(){
        return "menu";
    }
}
模板
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>メニュー画面</title>
</head>
<body>
    メニュー画面です。<br/>
    <a th:href="@{/search}">検索</a>
    <a th:href="@{/create}">登録</a>
</body>
</html>
考試
断言是不恰当的。
package flowapp;
import static org.junit.Assert.*
//static インポート以外は省略
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*
import static org.hamcrest.Matchers.*;
@ContextConfiguration(loader = SpringApplicationContextLoader.class,classes=[Application.class])
@WebAppConfiguration
class MenuControllerSpec extends Specification {
    @Autowired
    MenuController menuController;
    @Autowired
    WebApplicationContext wac;
    MockMvc mockMvc;
    def setup(){
        MockitoAnnotations.initMocks(this);
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }
    def "メニュー画面のテスト"(){
        when:"コンテキストルートにアクセスしたら"
        def pf=mockMvc
        .perform(get("/"))
        then:"メニュー画面が表示される。"
        pf
        .andExpect(status()
            .isOk())
        .andExpect(view().name("menu"))
        .andExpect(content().string(containsString("メニュー画面です。")))
    }
}
 
    