-->

Spring Webflow - decision-state vs action-state

2020-08-09 08:44发布

问题:

I am using Spring WebFlow 2 and I want to know the diff of decision-state vs action-state.

I am reading up and dont understand the diff of decision-state vs action-state. I understand that view-state will display a jsp for input but whats the diff of decision-state vs action-state?

why should I use decision-state over a action-state? why should I use a action-state over a decision-state?

Can someone shot some light on this

回答1:

Generally, decision-state is used exclusively for a boolean conditional. It's more clear and concise as to what it occurs.

For instance,

<decision-state id="myDecisionState">
    <if test="myBooleanFunction()" then="resultIsTrueState" else="resultIsFalseState" />
</decision-state>

This can be replicated using an action-state like so:

<action-state id="myActionState">
    <evaluate expression="myBooleanFunction()" />
    <transition on="yes" to="resultIsTrueState" />
    <transition on="no" to="resultIsFalseState" />
</action-state>

However, the difference is that action-state does not just operate on booleans - it can trigger transitions on String (string value), Boolean (yes/no), Enum (enum name) with any other result considered a success.

So, by contrast to a decision-state which actually has to decide something, an action-state can simply be used to execute some code.

<action-state id="myActionState">
    <evaluate expression="myFunction()" />
    <transition on="success" to="myNextState" />
</action-state>

I hope that clears stuff up.



回答2:

1. Confusing case

In Webflow there are cases where <decision-state> can be used in a similar fashion as <action-state>. The documentation shows that the below two expressions are interchangable.

<action-state id="moreAnswersNeeded">
  <evaluate expression="interview.moreAnswersNeeded()" />
  <transition on="yes" to="answerQuestions" />
  <transition on="no" to="finish" />
</action-state>

and:

<decision-state id="moreAnswersNeeded">
  <if test="interview.moreAnswersNeeded()" then="answerQuestions" else="finish" />
</decision-state>

2. When to use what?

Given that <decision-state> can handle only subset of what <action-state> handles - we should start from the former when considering two candidates.

  1. The <decision-state> is meant to be used for if-else routing, as an alternative to <action-state>. It works as a binary decision on rounting. If it can be applied - it should be used.
  2. The <action-state> allows for dealing with more complex logic. You can deal with Exceptions, you can execute expression without making them conditional and you can handle more cases than with the former one.

Hope this helps.



回答3:

They're very similar. You could write any decision state as an action state. Decision state just provides a convenient, concise syntax for conditional transitions (using the if element). If I only need to evaluate one expression and transition depending on the outcome, I use a decision state. Otherwise (eg., if I have multiple expressions to evaluate), I use an action state.

HTH



回答4:

you could use lambda condition

example: x = y ? "true result" : "false result"

	<view-state id="viewname">
		<on-entry>
			<evaluate expression="flowScope.varx == x ? Bean.somethingX : Bean.somethingY " result="flowScope.varResult" />
		</on-entry>
	</view-state>

remember condition