问题描述
我正在处理Webflow(SWF2).实体是使用Roo生成的.可以多次调用其中一个webflow视图multi-instance.jspx,以允许同一实体(MyClass)的多个持久化实例.
I'm working on a webflow (SWF2). The entities are generated using Roo. One of the webflow views, multi-instance.jspx, may be called multiple times to allow for multiple, persisted instances of the same entity (MyClass).
我想保留这些持久化实体的列表,以便在以后的流程中可以引用它们.到目前为止,我已经尝试了以下方法.
I'd like to keep a list of those persisted entities so that I can reference them on a later point in the flow. So far, I've tried the following.
我的flow.xml
的简化版本如下:
<on-start>
<evaluate expression="new java.util.ArrayList()" result="flowScope.myList" result-type="java.io.Serializable"/>
</on-start>
<view-state id="multi-instance" view="multi-instance" model="myClass">
<binder>
<binding property="field1"/>
<binding property="field2"/>
</binder>
<on-entry>
<evaluate expression="new com.test.MyClass()" result="flowScope.myClass" />
</on-entry>
<transition on="another_instance" to="multi-instance"/>
<transition on="success" to="confirm"/>
<transition on="cancel" to="abort"/>
<on-exit>
<evaluate expression="myClass.persist()"/>
<evaluate expression="flowScope.myList.add(myClass)"/>
</on-exit>
</view-state>
confirm
和abort
视图状态也在flow.xml
中定义. confirm.jspx
看起来像这样:
The confirm
and abort
view-states are defined in flow.xml
as well. The confirm.jspx
looks like this:
<div xmlns:spring="http://www.springframework.org/tags" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:form="http://www.springframework.org/tags/form" 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" />
<form:form>
<c:forEach items="${myList}" var="instance">
<li>${instance.getField1()} ${instance.getField2()}</li>
</c:forEach>
<div class="submit">
<input type="submit" id="success" name="_eventId_success" value="success"/>
<input type="submit" id="cancel" name="_eventId_cancel" value="cancel" />
</div>
</form:form>
</div>
这个问题:
每当我点击confirm.jspx
时,网络返回就会说org.springframework.webflow.engine.impl.FlowExecutionImpl.wrap(FlowExecutionImpl.java:569)抛出异常.
Whenever I hit confirm.jspx
, the web return says that there's an exception thrown at org.springframework.webflow.engine.impl.FlowExecutionImpl.wrap(FlowExecutionImpl.java:569).
Apache日志更具启发性.以下是调用堆栈顶部的代码段:
The Apache log is a little more enlightening. The following is a snippet of the top of the call stack:
SEVERE: Servlet.service() for servlet jsp threw exception org.apache.jasper.JasperException:
/WEB-INF/views/myflow/confirmation.jspx(6,7)
The function getField1 must be used with a prefix when a default namespace is not specified
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
我不确定ArrayList方法是否可行;我相信我已经读过某个地方,它在multi-instance
状态中定义的flowScope.myClass实例已由GC接收,或者至少超出了范围.我不知道.如果有人可以阐明该特定主题,我会很高兴.
I'm not sure if the ArrayList-approach is possible; I believe I've read somewhere that the flowScope.myClass instance, as it is defined in the multi-instance
-state, is picked up by the GC or at least falls out of scope. I'm not sure. If anyone can shed some light on that particular topic, I'd be thrilled.
(如果您碰巧知道保留这些持久性实体列表的更好方法,请随时让我知道!)预先感谢! :)
(And if you happen to know a better way to keep a list of these persisted entities, please feel free to let me know!) Thanks in advance! :)
更新:我可以像这样计算列表中的元素数量:
Update:I'm able to count the number of elements in my list like so:
<c:choose>
<c:when test="${myList != null}">myList exists, it contains <c:out value="${fn:length(myList)}" /> items!</c:when>
<c:otherwise>myList doesn't exist.</c:otherwise>
</c:choose>
它显示的元素数与我插入的元素数相同.但是,当我这样做时:
It shows the same number of elements as I've inserted. However, when I do this:
<c:forEach items="${myList}" var="instance">
<c:if test="${instance != null}">
<li>${instance.field1} ${instance.field2}</li>
</c:if>
</c:forEach>
不显示任何内容. (当省略null测试时,我可以确认存在正确数量的< li>元素.另外请注意,我正在尝试直接访问属性,如下所示:jstl/jsp-遍历bean的向量)我不知道该怎么想,关于这里的范围,但是很明显我无法通过ArrayList访问我的实体.
nothing is displayed. (I can confirm that there is a correct number of <li>-elements when the null-test is omitted. Note also that I'm trying to access the properties directly, as indicated here: jstl/jsp - iterating over a vector of beans) I don't know what to think, regarding the scope here, but it seems clear that I can't access my entities via an ArrayList.
推荐答案
首先,关于您关于变量作用域的问题,我建议您看一下 4.4 ,其中描述了所有不同的可用范围.
First, with regards to your question(s) around variable scoping, I would suggest looking at section 4.4 of the SWF documentation, where it describes all of the different available scopes.
流作用域变量在流的整个生命周期中均有效.因此,您的myClass变量在流退出之前不会消失.但是请记住,每次进入视图状态时,您的<on-entry>
表达式都会分配一个新实例.
Flow scope variables live through the lifetime of the flow. So your myClass variable will not go away until the flow exits. However keep in mind that your <on-entry>
expression is assigning a new instance every time that view state is entered.
第二,我认为您的解决方案可能正处于正确的轨道上.我会注意几件事:
Second, I think you are probably on the right track with your solution. I would note several things:
- 您要坚持并添加到
<on-exit>
元素中的列表中-这意味着当您离开状态时,包括在进行cancel
转换时,这两种情况将始终发生.这可能不是您想要的.相关: - 您的
<transition on="another_instance" to="multi-instance"/>
实际上正在退出视图状态并重新进入视图状态,从而触发<on-exit>
和<on-entry>
逻辑.只需执行<transition on="another_instance">
,就可以保持相同的状态.这样做将执行转换中的所有逻辑,然后重新渲染视图,而无需实际更改状态. - 您可能要考虑使用
<var>
标签用于初始化变量...您对<evaluate expression="new ..."/>
所做的工作有效,但使用<var>
可能更干净.另外,也不必说result-type="java.io.Serializable"
.需要将返回类型转换为elese时,应使用result-type
.
- You are persisting and adding to your list in the
<on-exit>
element -- this means these two things will always happen when you leave the state, including when you are doing yourcancel
transition. This may not be what you want. Related: - Your
<transition on="another_instance" to="multi-instance"/>
is actually exiting the view-state and re-entering it, triggering the<on-exit>
and<on-entry>
logic. It is possible to remain in the same state, by simply doing<transition on="another_instance">
. Doing this will execute any logic you have inside the transition, and then re-render the view without actually changing states. - You may want to consider using the
<var>
tag to initialize variables... what you are doing with<evaluate expression="new ..."/>
works but using<var>
may be cleaner. Also, it is not necessary to sayresult-type="java.io.Serializable"
.result-type
should be used when you need to convert the return type to something elese.
最后,您收到的错误似乎与webflow无关. JSTL/EL允许您访问bean属性,但不能访问方法,并且您正在尝试调用方法.有关更多信息,请参见此问题.
Finally, the error you are getting looks like it is unrelated to webflow. JSTL/EL allow you to access bean properties but not methods, and you are trying to invoke a method. See this question for more info.
这篇关于Spring Webflow:如何跟踪持久化的实体ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!