Can anybody help me understand what wrong here?
This is fragment from Spring web flow project. I create new object order and get a list of tables object from DB in tag of view state.
<?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.xsd">
<input name="order" required="true" />
<view-state id="chooseTable" model="flowScope.order">
<on-entry>
<set name="flowScope.order" value="new pizza.Order()"></set>
<evaluate expression="pizzaFlowActions.getFreeTables()"
result="viewScope.tables" />
</on-entry>
<transition on="chosenCreateOrder" to="bookedToOrder">
<evaluate expression="order.setTable(flowScope.order)" />
</transition>
<transition on="chosenNoOrder" to="booked">
<evaluate expression="order.setTable(flowScope.order)" />
</transition>
<transition on="cancel" to="cancel" />
</view-state>
<end-state id="cancel" />
<end-state id="bookedToOrder">
</end-state>
<end-state id="booked">
</end-state>
<global-transitions>
<transition on="cancel" to="cancel" />
</global-transitions>
</flow>
Jsp file with form with which object order is binded, and radiobuttons' values extracted from tables list.
<html xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:spring="http://www.springframework.org/tags"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:form="http://www.springframework.org/tags/form">
<jsp:output omit-xml-declaration="yes" />
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<head>
<title>Book a table</title>
</head>
<body background = "${pageContext.request.contextPath}/resources/plan.gif">
<form:form commandName="order" class="box login">
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}" />
<b>Choose table: </b>
<c:forEach items="${tables}" var="place">
<form:radiobutton path="table" label="${place}" value="${place}"/>
<br></br>
<br></br>
</c:forEach>
<input type="submit" class="btnLogin" name="_eventId_chosenCreateOrder" value="Now choose Pizza" />
<input type="submit" class="btnLogin" name="_eventId_chosenNoOrder" value="Just book a table" />
<input type="submit" class="btnLogin" name="_eventId_cancel" value="Cancel" />
</form:form>
</body>
</html>
Piece of Order class
public class Order implements Serializable {
private static final Logger LOGGER = getLogger(Order.class);
private int id;
private Customer customer;
private Set<Pizza> pizzas;
private Payment payment;
private Place table;
The problem here is that after choosing any variant any time the form just reloading and nothing happens more.
Will appreciate any help and hints.
Related
I am trying to use Spring Webflow 2.3.2.
I have a legacy web flow similar to:
<start-state id-ref="A" />
<action-state id="A">
<action bean="B" />
<transition on="success" to="T" />
</action-state>
<action-state id="T">
...
</action-state>
The equivalent code that I am writing for Spring Webflow 2.3.2 is:
<on-start>
<evaluate expression="B" />
</on-start>
<action-state>
<transition on="success" to="T" />
</action-state>
<action-state id="T">
...
</action-state>
Clearly I am missing the string to connect the initial evaluation to the transition. How can I connect the two?
Let's assume your flow starts with a form
Form
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}" />
<input type="hidden" name="_eventId" value="send" /> //This starts the flow
Flow.xml
<view-state id="showForm" model="formModel">
<transition on="send" to="send2"></transition>
</view-state>
<action-state id="send2">
<evaluate expression="..."></evaluate>
<transition to="send3"></transition>
</action-state>
<view-state id="send3">
</view-state>
Based on comments from #M. Deinum, I realize that a Spring upgrade does not affect Spring webflow usage in the application. The new webflow which finally worked is:
<action-state id="A">
<evaluate expression="B" />
<transition on="success" to="T" />
</action-state>
<action-state id="T">
...
</action-state>
All that was required is to replace <action bean="B" /> with <evaluate expression="B" /> and add an id to the <action-state>.
Below is my code snippet from jsp & flow xml, while trying to submit the form my page is getting refreshed but request is not going to the server. Please assist.
Jsp
<form action="${flowExecutionUrl}" modelAttribute="employeeDto" method="post">
<div class="formButton">
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}" />
<input name="_eventId_nextBtn" type="submit" value="submit">
</div>
</form>
Flow xml
<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.4.xsd">
<var name="employeeDto" class="com.school.transferobjects.EmployeeDto"/>
<view-state id="employeeView" model="employeeDto" view="employee">
<transition on="nextBtn" to="createEmployee" />
</view-state>
<action-state id="createEmployee">
<evaluate
expression="employeeHelper.saveEmployee(flowRequestContext,employeeDto)"
result="flowScope.res"/>
<transition on="${flowScope.res.success=='success'}" to="student.do"/>
</action-state>
</flow>
I think eventId is not being recognized, and so when you submit by click of the button and eventId is not found, resulting in flow execution getting refreshed.
Make sure you include form tag:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form modelAttribute="yourModel" method="post">
...
</form:form>
I got a annotated service bean which is used to store form values on newSpace_1 page when I navigate to another flow I wish cleaning this bean because if I return to newSpace_1 page I would like showing empty fields because I got navigate buttons that depend on the value of the fields.
I tried to declare this bean as var on flow definition and a #service bean, then set method works but get method retrieve null for each properties. So I need to use as #service for set/get methods work properly on newSpace_1 page.
<flow...>
<var name="spaceFields" class="x.y.x.Spacields" />
<view-state>
......
</view-state>
</flow>
SpaceFields
#Service("spaceFields")
public class SpaceFields implements Serializable {
private static final long serialVersionUID = 1L;
#Min(value=1)
#Max(value=194)
private Integer idCountry;
#Min(value=1)
#Max(value=5)
private Integer idContinent;
....
....
}
Flow definition
<?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"
parent="commons-header">
<secured attributes="ROLE_USER" />
<view-state id="adminSpace" view="admin_spaces.xhtml">
<transition on="new" to="step1" />
</view-state>
<view-state id="step1" view="newSpace_1.xhtml">
<on-entry>
<evaluate expression="place.findContinents()" result="flowScope.continents" />
</on-entry>
<transition on="next" to="step2" />
</view-state>
<view-state id="step2" view="newSpace_2.xhtml">
<on-entry>
<evaluate expression="category.findCategories()" result="flowScope.categories" />
</on-entry>
<transition on="back" to="step1" />
<transition on="next" to="step3" />
</view-state>
<view-state id="step3" view="newSpace_3.xhtml">
<transition on="back" to="step2" />
<transition on="next" to="step4" />
</view-state>
newSpace_1 code
<ui:composition template="/WEB-INF/templates/mainPage.xhtml">
<ui:define name="title">
<h:outputText value="${msg['newSpace.title']}" />
</ui:define>
<ui:define name="centerContainer">
<h:panelGroup layout="block" id="creatingSpaceContainer_step1_info" styleClass="creatingSpaceContainer_step1_info">
<h1>
<h:outputText value="#{msg['newSpace.step1.main.header']}" />
</h1>
<h:panelGroup layout="block" id="creatingSpaceContainer_step1_info_msg" styleClass="creatingSpaceContainer_step1_info_msg">
<h:outputText value="#{msg['newSpace.step1.main.msg']}" />
</h:panelGroup>
</h:panelGroup>
<h:form>
<h:panelGroup layout="block" id="creatingSpaceContainer_step1_fields" styleClass="creatingSpaceContainer_step1_fields">
<h:outputLabel for="continent" value="#{msg['continent.txt']}" styleClass="labelInput" />
<h:selectOneMenu id="continent" value="#{spaceFields.idContinent}" title="#{msg['continent.select.title']}" tabindex="1">
<f:selectItem itemLabel="#{msg['continent.select.zeroption']}" itemValue="0" noSelectionOption="true" />
<f:selectItems var="continent" value="#{continents}" itemLabel="#{continent.name}" itemValue="#{continent.primaryKey.continent.idContinent}" />
<f:ajax render="#this country messageContinentError" listener="#{place.findCountries(flowRequestContext)}" event="change" />
<f:validateBean for="idContinent" />
</h:selectOneMenu>
<h:message id="messageContinentError" for="continent" styleClass="messageError" />
<h:outputLabel for="country" value="#{msg['country.txt']}" styleClass="labelInput" />
<h:selectOneMenu id="country" value="#{spaceFields.idCountry}" title="#{msg['country.select.title']}" tabindex="2">
<f:selectItem itemLabel="#{msg['country.select.zeroption']}" itemValue="0" noSelectionOption="true" />
<f:selectItems var="countryLocale" value="#{countries}" itemLabel="#{countryLocale.name}" itemValue="#{countryLocale.primaryKey.country.idCountry}" />
<f:ajax render="#this -controls-creatingSpaceContainer_step1_controls messageCountryError" event="change" />
<f:validateBean for="idCountry" />
</h:selectOneMenu>
<h:message id="messageCountryError" for="country" styleClass="messageError" />
</h:panelGroup>
</h:form>
<h:form id="controls">
<h:panelGroup layout="block" id="creatingSpaceContainer_step1_controls" styleClass="creatingSpaceContainer_step1_controls">
<h:commandLink action="next" value="#{msg['next.button.txt']}" title="#{msg['next.button.title']}" alt="#{msg['next.button.alt']}" tabindex="3" rendered="#{spaceFields.idCountry>0}" />
</h:panelGroup>
</h:form>
</ui:define>
</ui:composition>
Use on-exit action in the view state to clear the bean while navigating away from view as:
<view-state id="step1" view="newSpace_1.xhtml">
<on-entry>
....
</on-entry>
<transition on="next" to="step2">
<on-exit>
<evaluate expression="someaction.clear(flowScope.spaceFields)"/>
</on-exit>
</view-state>
This way when you re-enter view all your bean fields are empty.
You can find more details about on-exit action here.
I had a requirement of getting the image from the filesystem and displaying it in browser. The code i had tried till now is shown below:
.jsp code:
<formspring:form enctype="multipart/form-data" modelAttribute="onlinePurchase">
Please select a file to upload : <input type="file" name="multipartFileUpload" id="multipartFileUpload" />
<div class="submit">
<input type="submit" id="upload" name="_eventId_upload"
value="Upload Image" />
</div>
</formspring:form>
controller code:
public void saveImage(OnlinePurchase onlinepurchase,
MultipartFile multipartFile) {
try {
onlinepurchase.setContent(multipartFile.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
onlinepurchase.setContentSize(new Long(multipartFile.getSize())
.intValue());
onlinepurchase.setContentType(multipartFile.getContentType());
onlinepurchase.setFilename(multipartFile.getOriginalFilename());
}
flow.xml code:
<view-state id="onlinePurchaseStep1" view="onlinepurchase/onlinepurchaserequest-step-1"
model="onlinePurchase">
<on-render>
<evaluate expression="oPAction.initialize" />
</on-render>
<transition on="submit" to="onlinePurchaseStep1" validate="false">
<evaluate expression="oPAction.cropImage" />
</transition>
<transition on="upload" to="onlinePurchaseStep1" validate="false">
<evaluate
expression="oPAction.saveImage(onlinePurchase,requestParameters.multipartFileUpload)"
result="" result-type="" />
</transition>
<transition on="success" to="onlinePurchaseStep2">
<evaluate expression="oPAction.addMedia" />
</transition>
<transition on="cancel" to="login" validate="false" />
</view-state>
The issue is that saveImage(OnlinePurchase onlinepurchase, String multipartFile) method in action is not getting called.
Any suggestions are appreciated.
You need a MultipartResolver :
add this line in your springs's webmvc xml configuration file.
<bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
Note that the CommonsMultipartResolver uses classes from org.apache.commons.io package so you may need to add commons-io.jar in your classpath.
If your project is maven you can add the following dependency in the pom:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>a valid version</version> <!-- 2.4 worked for me -->
</dependency>
I am using spring mvc 3.0.4 and webflow 2.2.1, I config my webflow like the following:
<view-state id="createTaskDetail" model="task" view="task/createTaskDetail">
<transition on="upload" to="uploadFile" />
<transition on="revise" to="createTaskBasic" />
<transition on="publish" to="publish" />
<transition on="cancel" to="cancel" />
</view-state>
<view-state id="uploadFile" model="task" view="task/uploadFile">
<transition on="confirm" to="createTaskConfirm" >
<evaluate expression="task.processFile()"/>
</transition>
<transition on="revise" to="createTaskDetail" />
<transition on="publish" to="publish" />
<transition on="cancel" to="cancel" />
</view-state>
<view-state id="createTaskConfirm" model="task"
view="task/createTaskConfirm">
<transition on="publish" to="publish" />
<transition on="revise" to="uploadFile" />
<transition on="cancel" to="cancel" />
</view-state>
createTaskDetail is my 2nd view, when I arrived this page I can't forward or backward(When clicking button next or return or cancel). There is no js error or java exception,here is my jsp code of 2nd view:
<button type="submit" id="upload" name="_eventId_upload">next</button>
<button type="submit" name="_eventId_revise" >return</button>
<button type="submit" name="_eventId_cancel" >cancel</button>
<button type="submit" name="_eventId_publish" >publish</button>
Anyone has an idea? Thanks.
Do you have a form tag around the buttons?
<form:form modelAttribute="task">
inputs
buttons ...
</form:form>
Maybe validation errors which are not shown?