Spring web flow, create subflow with input field - spring-webflow

I am starting with spring web flow, reading and following the documentation. I have created a new flow:
test-flow.xml
<?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">
<var name="testName" class="com.project.TestView" />
<view-state id="test">
<on-entry>
<set name="flowScope.name" value="testName.name" />
</on-entry>
<transition on="test" to="saveName"/>
</view-state>
<subflow-state id="subTest" subflow="testSub-flow">
<input name="nameVar" value="name" />
<transition to="error" />
</subflow-state>
<view-state id="error" />
<end-state id="finish" />
</flow>
And I am trying to create a testSub-flow.xml
<?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">
<input type="String" name="nameVar" />
<on-start>
<evaluate expression="com.project.TestView.printSomething(nameVar)" result="flowScope.testPrint" />
</on-start>
<view-state id="printTest" >
<transition on="restart" to="endSub" />
</view-state>
<end-state id="endSub" />
</flow>
The method called is:
#Transactional(readOnly = true)
public String printSomething(String text){
System.out.print(text + " this is a test");
return text + " this is a test";
}
I get some exception in the browser when it is loading the main flow, test-flow.xml
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.webflow.execution.ActionExecutionException: Exception thrown executing [AnnotatedAction#6ca837 targetAction = [EvaluateAction#7aed3a expression = com.project.TestView.printSomething(nameVar), resultExpression = flowScope.testPrint], attributes = map[[empty]]] in state 'null' of flow 'test' -- action execution attributes were 'map[[empty]]'
What could be the problem?? Thanks in advance.

At first sight, it seems that it can not find any start-state. Try adding start-state attribute in flow tag:
<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"
start-state="test">
If that does not fix the problem, it could be that flow builder can not find a state named "saveName". The problem could be in this line:
<transition on="test" to="saveName"/>
If you want to invoke the subflow when "test" event occurs, you write "subTest" instead of "saveName" in order to call the subflow.
So, that line should be:
<transition on="test" to="subTest"/>
Also, note that you are not specifying any view for those view-states.
Hope this helps.

Related

How to use the input/output elements in webflow?

I'm trying to understand how the input and output elements are used in webflow. The documentation suggests that flow input/output mapping is similar to calling a method with a signature, but I don't understand what passes the flow the input value or what the flow returns output to. How do I use these elements?
I've been reading the documentation found here, but there are no examples of the elements in action that I can find. This is an example from the documentation.
<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.xsd">
<input name="hotelId" />
<on-start>
<evaluate expression="bookingService.createBooking(hotelId, currentUser.name)"
result="flowScope.booking" />
</on-start>
<view-state id="enterBookingDetails">
<transition on="submit" to="reviewBooking" />
</view-state>
<view-state id="reviewBooking">
<transition on="confirm" to="bookingConfirmed" />
<transition on="revise" to="enterBookingDetails" />
<transition on="cancel" to="bookingCancelled" />
</view-state>
<end-state id="bookingConfirmed" />
<end-state id="bookingCancelled" />
In this code, where does the value assigned to "hotelId" come from? Does the input come from the URL? If so, does <input/> behavior change in subflows?
In this code, hotelId will be automatically mapped from the URL if you call /myFlow?hotelId=3 (assuming your flow is called "myFlow")
but you can also set the input directly when calling the flow from another flow, i.e. using:
<subflow-state id="mySubflow" subflow="myFlow">
<input name="hotelId" value="3"/>
</subflow-state>

Webflow On-Render/On-Entry Exception Still Runs On-Exit Methods

I have the following defined in my flow definition:
<view-state id="switchboard" view="switchboard2" model="reservationForm">
<on-entry>
<evaluate expression="flowController.enterSwitchboard(flowRequestContext)" />
<evaluate expression="flowController.populateActionFlags(flowRequestContext)" />
</on-entry>
<transition on="prev" to="switchboardAction" validate="false" />
<transition on="*" to="switchboardAction" />
<transition on-exception="java.lang.Exception" to="systemErrorView" />
<on-exit>
<evaluate expression="flowController.exitSwitchboard(flowRequestContext)" />
<evaluate expression="flowController.clearWebflowForms(flowRequestContext)" />
</on-exit>
</view-state>
What is happening is that an error (in this particular case, IllegalArgumentException, but could be other exceptions as well) in the populateActionFlags() method is ocurring, but the exitSwitchboard() method is still firing. After this, the clearWebflowForms() method throws an exception because the model is screwed up as a result of the previous exception. This is causing an infinite loop.
What I need is this: when an exception occurs, bypass the on-exit methods and go to an error state ("systemErrorView") defined in a section. Do not pass go, do not collect $200.
Webflow version is 2.4.1.
Can anyone assist?
Jason
try using a global transition in your flow:
<global-transitions>
<transition on-exception="java.lang.Exception" to="systemErrorView"/>
</global-transitions>
keep in mind this will be shared by all states of your flow.
If you don't want it to be shared, you might want to create a custom exception for this.
on-exit is to be executed at the end of a view state with intent. Declaring it means the same as a finally block in java.
In your case you may want to do this
<transition on="prev" to="switchboardAction" validate="false" >
<evaluate expression="flowController.exitSwitchboard(flowRequestContext)" />
<evaluate expression="flowController.clearWebflowForms(flowRequestContext)
</transition>
<transition on="*" to="switchboardAction" >
<evaluate expression="flowController.exitSwitchboard(flowRequestContext)" />
<evaluate expression="flowController.clearWebflowForms(flowRequestContext)
</transition>
This executes the evaluate statements on each transition but not on exception.

How to import globaltransitions.xml in myflow.xml?

I want import global transitions as a separate xml so that i can import this global transition xml from all the flow xmls. globaltransitions.xml looks as follows, so how can i import this from flow xml ?
<?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"
>
<global-transitions>
<transition on="login" to="login" />
<transition on="error" to="error" />
</global-transitions>
</flow>
Any idea on this?
You can use the globaltransitions.xml as parent flow, so all your flows should extend it. In order to get a flow extending the parent flow, use "parent" attribute.
First, define both parent and child flows in flow-registry:
<flow:flow-registry id="flowRegistry" ...>
<flow:flow-location id="globalTransitions" path="globaltransitions.xml"/>
<flow:flow-location id="childFlow" path="childflow.xml"/>
</flow:flow-registry>
Finally, implement parent flow as you described and child flow as follows:
<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"
start-state="start" parent="globalTransitions">
...
</flow>
Now all global transitions defined in globaltransitions.xml are imported into childflow.
Hope this helps.

Persisted entites in an ArrayList gone missing in jspx using Spring Webflow 2.0

I'm writing a spring webflow with MVC and persistence scaffolded by Spring Roo. In this flow, the user is supposed to be creating multiple instances of one entity, which in turn is to be referenced from another entity. For simplicity, I'll dub these entities MyClass1 and MyClass2. I'm having a hard time figuring out how to keep a list of persisted entities, which is needed at confirmation.
I have previously posted a question regarding the same topic. I do feel, however, that editing the original question (even more) in order to further clarify my issue would violate the SO-"protocol", and so I've decided to ask a refined version of the original question. In retrospect, I realize that the original question should've been more accurate. I'm probably gonna get some heat for this, but I feel the question is important enough (at least to me!) to take it. :)
I'm including my roo-script to let anyone easily reproduce my setup. Here it is:
project --topLevelPackage com.test.webflow
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
entity --class ~.domain.Class1 --testAutomatically
field string --fieldName name
entity --class ~.domain.Class2 --testAutomatically
field string --fieldName name
field reference --fieldName class1 --type ~.domain.Class1
controller scaffold --class ~.web.Class1Controller --entity ~.domain.Class1
controller scaffold --class ~.web.Class2Controller --entity ~.domain.Class2
web flow --flowName registration
The flow.xml in /WEB-INF/views/registration looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
<on-start>
<evaluate expression="new java.util.ArrayList()" result="flowScope.myList" result-type="java.io.Serializable"/>
</on-start>
<view-state id="view-state-1" view="registration/view-state-1" model="class1">
<on-entry>
<evaluate expression="new com.test.webflow.domain.Class1()" result="flowScope.class1"/>
</on-entry>
<transition on="repeat" to="view-state-1"/>
<transition on="success" to="view-state-2"/>
<transition on="cancel" to="end-state"/>
<on-exit>
<evaluate expression="class1.persist()" result="flowScope.class1"/>
<evaluate expression="myList.add(class1)"/>
</on-exit>
</view-state>
<view-state id="view-state-2" view="registration/view-state-2">
<transition on="cancel" to="end-state"/>
</view-state>
<end-state id="end-state" view="registration/end-state"/>
</flow>
(In a real-life version of the flow, there would be another view-state in which entities of Class2 would be registered.) The view-state-1.jspx looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:spring="http://www.springframework.org/tags" xmlns:form="http://www.springframework.org/tags/form" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />
<spring:message var="title" code="webflow_state1_title" htmlEscape="false" />
<util:panel id="title" title="${title}">
<h1>${fn:escapeXml(title)}</h1>
<p>
<spring:message code="webflow_state1_message" />
</p>
<form:form commandName="class1">
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}" />
<p>Enter name: <form:input path="name"/></p>
<div class="submit">
<spring:message var="cancel" code="button_cancel" htmlEscape="false" />
<spring:message var="proceed" code="button_proceed" htmlEscape="false" />
<spring:message var="repeat" code="button_repeat" htmlEscape="false" />
<input type="submit" id="cancel" name="_eventId_cancel" value="${fn:escapeXml(cancel)}" />
<input type="submit" id="success" name="_eventId_success" value="${fn:escapeXml(proceed)}" />
<input type="submit" id="repeat" name="_eventId_repeat" value="${fn:escapeXml(repeat)}" />
</div>
</form:form>
</util:panel>
</div>
The view-state-2.jspx looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:spring="http://www.springframework.org/tags" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" xmlns:form="http://www.springframework.org/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />
<spring:message var="title" code="webflow_state2_title" htmlEscape="false" />
<util:panel id="title" title="${title}">
<h1>${fn:escapeXml(title)}</h1>
<p>
<spring:message code="webflow_state2_message" />
</p>
<p>
<c:forEach var="class1" items="${myList}">
<li><c:out value="${class1.name}"/></li>
</c:forEach>
</p>
</util:panel>
</div>
From all I've read so far, I think my solution should work. However, I still don't get the expected output; i.e. a print out of every name-field. I get the same number of <li>-elements as I put in, but they all seem to be evaluated to null, as explained in my previous post. Can anyone explain to me why this code doesn't display the contents of the persisted Class1.name-fields? (Btw: they do show up in the CRUD.)
Thanks in advance!
D-O-(freakin')-H! The signature of Class1.persist() is public void Class1.persist(). Ahem. So
<evaluate expression="class1.persist()" result="flowScope.class1"/>
will, apparently, quite effectively set the flowScope.class1 variable to null. By dropping the result-attribute will solve your (and my!) problem. :)

Spring Web Flow: How do I pass values from one flow to another flow

I have a Java web application using spring web flow.
How do I pass values from one flow to another flow?
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:c="http://java.sun.com/jsp/jstl/core" 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">
<persistence-context />
<var name="editBean" class="jp.co.anicom.domain.User" />
<var name="deleteBean" class="jp.co.anicom.domain.User" />
<var name="authorityBean" class="jp.co.anicom.domain.Authority" />
<on-start>
<set name="flowScope.username" value="requestParameters.username" />
</on-start>
<action-state id="queryAll">
<evaluate expression="employeeAction.GetAuthority(flowScope.username)"
result="authorityBean" />
<transition to="editForm" />
</action-state>
<view-state id="editForm" model="editBean" view="../xhtml/framework/edit">
<transition on="editButton" to="validateAccount" />
<transition on="delete" to="getId" />
<transition on="back" to="editSuccessful" />
</view-state>
<action-state id="validateAccount">
<evaluate expression="employeeAction.GetEmployee(flowScope.username, oldPassword)"
result="editBean" />
<transition to="checkUserAccount" />
</action-state>
<action-state id="getId">
<evaluate expression="employeeAction.GetEmployee(flowScope.username)"
result="deleteBean" />
<transition to="deleteUser" />
</action-state>
<decision-state id="checkUserAccount">
<if test="editBean == null" then="queryAll"
else="confirmPassword" />
</decision-state>
<decision-state id="confirmPassword">
<if test="newPassword.equals(confirmPassword)" then="editUser1"
else="queryAll" />
</decision-state>
<action-state id="editUser1">
<set name="editBean.password" value="newPassword" />
<transition to="editUser2" />
</action-state>
<action-state id="editUser2">
<evaluate
expression="employeeAction.editEmployee(editBean, authorityBean.authority)" />
<transition to="editSuccessful" />
</action-state>
<action-state id="deleteUser">
<evaluate expression="employeeAction.deleteEmployee(deleteBean)" />
<transition to="editSuccessful" />
</action-state>
<end-state id="editSuccessful"
view="externalRedirect:contextRelative:/admin_main.do" commit="true" />
<end-state id="displayError" view="../xhtml/framework/displayError" />
<end-state id="dummy1" view="../xhtml/framework/dummy" />
<global-transitions>
<transition on-exception="java.lang.Exception" to="displayError" />
</global-transitions>
</flow>
I am having a problem with the edit functionality here. In my edit page I have username, oldpassword, newpassword and confirm password fields.
First in validateAccount state I check if the username and oldpassword exists in the database, if it it doesn't exist I forward it to queryall state.
If it exists I check if the new password and confirmpassword values are the same, if they are the same I proceed with the editing.
If not I return again to queryAll.
QueryAll state gets the authority of the user to populate it in the form upon re-displaying the page. When I leave the password fields blank and the first time I click edit button It throws a java.lang.NullPointerException.
Create your two flows as subflows and then the data in each flow should be available in the parent and the other subflows.
Mapping data to the subflow happens
before the subflow session is started.
Mapping data from the subflow back to
the parent flow is done when the
subflow completes and the parent flow
session resumes.

Resources