欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Grails中使用webflow

程序员文章站 2024-03-26 13:42:29
...
Grails从0.6版本开始集成Spring的webflow功能。由于其简单的配置,化简了流程的开发。
我在自己的项目里用到了webflow,其实是个很简单的流程,图如下:
[img]/upload/attachment/133914/484f1b41-504a-37a1-85b2-a8ad09a7a2ce.jpg[/img]
业务背景是这样的:商品售出后,有些商品坏了需要返修。先创建返修单,然后走返修流程。如果商品返修成功,则标识返修单已完成返修任务,退出流程;如果商品返修失败,则做相应的业务操作包括出货单中减去相应产品数量、利润表中减去相应的当天利润等等,最后退出流程。

下面显示的是流程的完整代码
 def startFlow = {
def id = params.id
start{
on("success").to "endRepair"
on("failed").to "endFailed"
}
endRepair{
def repairProduct = RepairProduct.findById(id)
repairProduct.setFlag(true)
repairProduct.save()
}
endFailed {
def repairProduct = RepairProduct.findById(id)
def rg = new ReturnedGoods()

def productName = repairProduct.getProductName()
def modelNo = repairProduct.getModelNo()
def amount = repairProduct.getRepairNumber()
def time = repairProduct.getTime()
def reason = repairProduct.getReason()
def number = repairProduct.getNumber()

// 创建退货单
rg.setProductName(productName)
rg.setModelNo(modelNo)
rg.setAmount(amount)
rg.setTime(time)
rg.setReason(reason)
rg.setNumber(number)
rg.save()

def exportProduct = ExportProduct.findByTimeAndNumber(time,number)
if (amount == exportProduct.getAmount())
{
def amount1 = exportProduct.getAmount()
def outPrice = exportProduct.getOutPrice()
def inPrice = exportProduct.getInPrice()
def sellTime = exportProduct.getTime()
def pureProfits = (outPrice - inPrice)*amount1
def profit = Profit.findByPureProfitsAndSellTime(pureProfits,sellTime)
profit.delete()//删除利润
exportProduct.delete()
}
if(amount < exportProduct.getAmount())
{
def outPrice = exportProduct.getOutPrice()
def inPrice = exportProduct.getInPrice()
def sellTime = exportProduct.getTime()
def pureProfits = (outPrice - inPrice)*amount
def profit = Profit.findBySellTime(sellTime)
profit.setPureProfits(profit.getPureProfits()-pureProfits)
profit.save()
exportProduct.setAmount(exportProduct.getAmount()-amount)
exportProduct.save()
}
}
}



在grails中controller可以定义流程,必须以Flow结尾。并且该流程可以看成是普通的action,不过在视图中需要创建该流程的文件夹。以上面的代码为例,代码中的controller的名字为RepairProductController,则创建的视图为repairProduct/start/start.gsp endRepair.gsp endFailed

其中需要关注的是start/start.gsp 完整代码如下:
<%@ page contentType="text/html;charset=UTF-8" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="layout" content="main" />
<title>返修流程</title>
</head>
<body>
<div class="nav">
<span class="menuButton"><g:link class="home" controller="management" action="main">返回主菜单</g:link></span>
<span class="menuButton"><g:link class="list" action="list">RepairProduct List</g:link></span>
</div>
<div class="body">
<h1>选择返修结果</h1>
<div style="margin-left:20px;width:60%;" mce_style="margin-left:20px;width:60%;" mce_style="margin-left:20px;width:60%;">
<g:form action="start">
<g:submitButton name="success" value="返修成功"/>
<g:submitButton name="failed" value="返修失败"/>
</g:form>
</div>
</div>
</body>
</html>


其中的action为start表示startFlow,而
<g:submitButton name="success" value="返修成功"/>
<g:submitButton name="failed" value="返修失败"/>
则是因为
start{
on("success").to "endRepair"
on("failed").to "endFailed"
}
通过 success 和 failed来跳转到各个相应的流程。

[img]/upload/attachment/133917/28b3a177-bcdf-335f-981c-b7f1c6043a96.jpg[/img]

[img]/upload/attachment/133919/715bebbb-3597-335f-ba7c-e7b8174f8c01.jpg[/img]

[img]/upload/attachment/133921/5306d2fe-0f26-3eea-895a-dbabaea9583f.jpg[/img]