简体   繁体   English

如何在Play中实施流量控制?

[英]How to implement flow control in Play?

We are trying to implement some flow control in Play, kind of a wizard with several steps. 我们正在尝试在Play中实现一些流控制,这是一个包含多个步骤的向导。 What is the best practice to do it in Play? 在Play中最佳做法是什么?

Requirements we have: 我们的要求:

  1. Should allow multi-step flows, like step1 -> step2 -> step3 -> step4 -> finish 应该允许多步流,例如step1-> step2-> step3-> step4->完成
  2. Should be able to change order or steps depending on context, so if user selects a checkbox on step2, flow should be step1 -> step2 -> warningStep -> step5 -> finish 应该能够根据上下文更改顺序或步骤,因此,如果用户在步骤2上选中了一个复选框,则流程应为步骤1->步骤2->警告步骤->步骤5->完成
  3. Ideally needs support for "Back" button to return between steps 理想情况下,需要支持“返回”按钮以在步骤之间返回

The problem we have is that any single step in flow doesn't know where it should redirect next and since Play session is very simple, it won't help here much. 我们遇到的问题是,流程中的任何单个步骤都不知道下一步应该重定向到哪里,并且由于Play会话非常简单,因此在此无济于事。

Here is the solution we currently have: 这是我们目前拥有的解决方案:

  1. Store Flow steps in database in user object with @OneToMany public List<FlowStep> flowSteps 使用@OneToMany public List<FlowStep> flowSteps在用户对象中的数据库中存储Flow步骤
  2. Provide methods in user model to add/remove/skip and change order of flow steps stored for this user 在用户模型中提供方法以添加/删除/跳过和更改为此用户存储的流程步骤的顺序
  3. Implement steps normally, with form action leading to "doStep3" controller etc 正常执行步骤,通过表单操作导致“ doStep3”控制器等
  4. Implement "Flows" controller that uses @Before and @After interceptors to correctly redirect to next step after current step is processed and no validation errors found 实现使用“ @Before”和“ @After”拦截器的“流”控制器,以在处理当前步骤后正确重定向到下一步,未发现任何验证错误
  5. Added Flows.next() controller that redirects to next step (used for "Skip" button href) 添加了Flows.next()控制器,该控制器重定向到下一步(用于“跳过”按钮href)

What are the disadvantages of this solution? 该解决方案的缺点是什么? Is there any better way (maybe some Play built-in methods) to improve it? 有没有更好的方法(可能是一些Play内置方法)来改进它?

What you want is a finite state machine . 您想要的是一个有限状态机 To implement it, you'll need a class that knows all the possible transitions between steps. 要实现它,您需要一个知道步骤之间所有可能转换的类。 Then you can provide to it the current step and any relevant input, and it will return the output (where the output is the view to render next). 然后,您可以为其提供当前步骤和任何相关输入,它将返回输出(其中输出是下一个要呈现的视图)。

Then you use render to redirect the user, as in: 然后,使用render重定向用户,如下所示:

render("my/view/path.html", myparams);

This is not the only option, and storage of the transitions will depend on how complex you need them (can be hardcoded for simple scenarios, maybe stored in database for more complex ones), but it should work. 这不是唯一的选择,并且过渡的存储将取决于您需要它们的复杂程度(可以为简单的场景进行硬编码,也可以为更复杂的场景存储在数据库中),但是它应该可以工作。

As Play is stateless you'll need to keep the information in the database (for complex scenarios where you need to take in account information for several steps) or, if the relevant togles are just a few, store them in the cookie itself. 由于Play是无状态的,因此您需要将信息保存在数据库中(对于复杂的场景,您需要考虑几个步骤的信息),或者,如果相关的触发器只是少数,则将其存储在cookie本身中。

I would avoid using @Before/@After as you are coupling the state machine to the controller. 当您将状态机耦合到控制器时,我会避免使用@ Before / @ After。 Ideally you want them to be idnependent, with the state machine returning only transitions that you can translate later into view paths. 理想情况下,您希望它们是独立的,状态机仅返回转换,您可以稍后将其转换为视图路径。 That will simplify changing transitions. 这将简化变化的过渡。

If the scenario is not extremely complex, I would not even bother to store them in the database. 如果情况不是非常复杂,我什至不用费心将它们存储在数据库中。 If you want it reusable and extremely flexible, then do it, otherwise it may be simpler to just "hardcode it". 如果您希望它可重用且非常灵活,请执行此操作,否则仅“对其进行硬编码”可能会更简单。

Did you check this section in the Play documentation (a very quick read): 您是否在Play文档中检查了此部分(快速阅读):
http://www.playframework.org/documentation/1.2.4/model#stateless http://www.playframework.org/documentation/1.2.4/model#stateless

It lists the options you have for exactly what you are asking. 它列出了您所要求的选项。

You can try to use Play Cache mechanism as pseudo session to store the validated steps instead of handling in the database. 您可以尝试将“ 播放缓存”机制用作伪会话来存储经过验证的步骤,而不是在数据库中进行处理。 Using Play Cache would be a simpler solution 使用播放缓存将是一个更简单的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM