`
fengzhizi715
  • 浏览: 159753 次
  • 性别: Icon_minigender_1
  • 来自: 上海 苏州
社区版块
存档分类
最新评论

Grails中使用webflow

阅读更多

   Grails从0.6版本开始集成Spring的webflow功能。由于其简单的配置,化简了流程的开发。
   我在自己的项目里用到了webflow,其实是个很简单的流程,图如下:

   业务背景是这样的:商品售出后,有些商品坏了需要返修。先创建返修单,然后走返修流程。如果商品返修成功,则标识返修单已完成返修任务,退出流程;如果商品返修失败,则做相应的业务操作包括出货单中减去相应产品数量、利润表中减去相应的当天利润等等,最后退出流程。

    下面显示的是流程的完整代码
 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来跳转到各个相应的流程。








  • 大小: 20.3 KB
  • 大小: 13.1 KB
  • 大小: 10.7 KB
  • 大小: 9.2 KB
分享到:
评论
2 楼 copoplar 2009-09-24  
很不错,学习了!
1 楼 supersnake 2009-08-09  
学习ing 

相关推荐

Global site tag (gtag.js) - Google Analytics