DispatcherServlet与base-path、流程id、流程定义文件
/WEB-INF/flows/regist/regist-flow.xml,
/WEB-INF/flows/是我们上面配置的base-path,
/regist/regist-flow.xml是/流程id/流程定义文件, 流程点以文件命名为”流程id-flow.xml”,
这样我们访问regist请求时,如果DispatcherServlet找不到对应的处理器,就会交给Web Flow,这样Web Flow就会按照流程定义开始流程。
xml 流程定义
view-state,action-state,decision-state,end-state,subflow-state
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<!-- flow作用域的变量,我们这个流程的模型对象 -->
<var name="user"
class="com.yjp.springmvc.blog.beans.model.User"/>
<!-- 欢迎界面,默认找welcome.jsp -->
<view-state id="welcome">
<transition on="register" to="register"/>
</view-state>
<!-- 注册界面,默认找register.jsp -->
<view-state id="register" model="user">
<transition on="doRegist" to="doRegist"/>
</view-state>
<!-- 使用registAction判断是否注册成功,执行跳转 -->
<decision-state id="doRegist">
<if test="registAction.regist(user)"
then="complete"
else="register" />
</decision-state>
<!-- 完成界面,默认找complete.jsp -->
<view-state id="complete" model="user">
<transition on="login" to="end"/>
</view-state>
<!-- 结束状态-->
<end-state id="end" view="externalRedirect:/"/>
<!-- 全局转移 -->
<global-transitions>
<transition on="cancel" to="end" />
</global-transitions>
</flow>
使用了view-state,decision-state和end-state,通过转移将他们串联起来。
<global-transitions>
全局转移,无论哪个状态以cancel终结,都会进入到end-state。如下图:
decision-state
decision-state中使用的action类
package com.yjp.springmvc.blog.web.flowaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yjp.springmvc.blog.beans.model.User;
import com.yjp.springmvc.blog.beans.service.UserService;
@Component
public class RegistAction {
private UserService userService;
@Autowired
public RegistAction(UserService userService) {
this.userService = userService;
}
public boolean regist(User user) {
return userService.saveUser(user);
}
}
模板文件
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎</title>
</head>
<body>
<h1>欢迎您选择简微</h1>
如果您喜欢我们的简微,请点击下一步,开始您的旅程<br><br>
<sf:form>
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey} }"/>
<input type="submit" name="_eventId_register"
value="下一步"/>
<input type="submit" name="_eventId_cancel"
value="取消"/>
</sf:form>
</body>
</html>
Spring的标签库中的form标签:
eventId事件: 以表明点击该按钮触发的事件,这会用在流程中transition的on属性,用来判断下一步的流程走向。其他的没有特殊的。
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<h1>会员注册</h1>
<sf:form commandName="user">
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey} }"/>
<table style="background-color:#cccccc">
<tr>
<td>邮件地址:</td>
<td><sf:input type="emil" path="email" size="25" maxlength="100" cssErrorClass="error"/></td>
</tr>
<tr>
<td>名称(最大16字符):</td>
<td><sf:input type="text" path="username" size="25" maxlength="16" cssErrorClass="error"/></td>
</tr>
<tr>
<td>密码(6到16字符):</td>
<td><sf:input type="password" path="password" size="25" maxlength="16" cssErrorClass="error"/></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="confirmedPasswd" size="25" maxlength="16"></td>
</tr>
<tr>
<td align="center"><input type="submit" name="_eventId_doRegist" value="注册"></td>
<td align="center"><input type="submit" name="_eventId_cancel" value="取消"></td>
</tr>
</table>
</sf:form>
</body>
</html>
complete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册完成</title>
</head>
<body>
<h1>恭喜 ${user.username} 完成注册</h1>
<a href="${flowExecutionUrl}&_eventId=login">去登陆</a>
</body>
</html>