使用 Spring Webflow 2.0 保留 ArrayList 中的实体在 jspx 中丢失

发布于 2024-11-12 05:48:16 字数 5612 浏览 4 评论 0原文

我正在使用 MVC 编写一个 Spring Webflow,并由 Spring Roo 搭建持久性脚手架。在此流程中,用户应该创建一个实体的多个实例,而该实体又将从另一个实体引用。为简单起见,我将这些实体命名为 MyClass1 和 MyClass2。我很难弄清楚如何保留确认时需要的持久实体列表。

我之前发布过一个问题关于相同的主题。然而,我确实觉得,编辑原始问题(甚至更多)以进一步澄清我的问题会违反 SO-“协议”,因此我决定提出原始问题的改进版本。回想起来,我意识到原来的问题应该更准确。我可能会为此感到愤怒,但我觉得这个问题足够重要(至少对我来说!)。 :)

我包括了我的 roo 脚本,让任何人都可以轻松地重现我的设置。如下:

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

/WEB-INF/views/registration 中的 flow.xml 如下所示:(

<?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>

在流程的现实版本中,将有另一个视图状态,其中将注册 Class2 的实体。) view-state-1.jspx 看起来像这样:

<?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>

view-state-2.jspx 看起来像这样:

<?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>

从到目前为止我读过的所有内容来看,我认为我的解决方案应该有效。但是,我仍然没有得到预期的输出;即打印出每个name字段。我得到的

  • 元素数量与我输入的相同,但它们似乎都被评估为 null,如我上一篇文章中所述。谁能向我解释为什么这段代码不显示持久化的 Class1.name-fields 的内容? (顺便说一句:它们确实出现在 CRUD 中。)
  • 提前致谢!

    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!

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    写给空气的情书 2024-11-19 05:48:16

    DO-(该死的)-H! Class1.persist() 的签名是 public void Class1.persist()咳咳。因此

    <evaluate expression="class1.persist()" result="flowScope.class1"/>
    

    ,显然,可以非常有效地将 flowScope.class1 变量设置为 null。通过删除 result 属性将解决您(和我的!)的问题。 :)

    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. :)

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文