开发者

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>
            &l开发者_如何学运维t;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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜