我是 strut 2 的新手,虽然我已经在 struts 1.2 上工作过。在现有的项目 jsp 文件之一中,我有以下代码:
<script type="text/javascript">
var relationshipData = { // line1
page : '<s:property value="displayPage" />', // line2
records : '<s:property value="customerRelations.size" />', // line3
rows : [ <s:iterator value="customerRelations" status="iterStatus"> // line4
{ id : '<s:property value="relationId" />',
cell : [ '<s:property value="relationDesc" escapeJavaScript="true" />' ] } <s:if test="!#iterStatus.last">,</s:if> //line5
</s:iterator>] // line6
};
</script>
请求即将到来 CustomerRelationAction.java
,它有方法 getCustomerRelations()< /code> 和 getRelationId()
。
以下是问题:-
-
我在方法 getCustomerRelations()
中放置了断点。我看到流程在该方法中出现了四次。在第 3 行两次,在第 4 行再两次。
根据我的理解,流程应该只出现 1 次,即在第 3 行。一旦完成第 3 行的 getCustomerRelations ,就不应该将其值放入值堆栈中,以便
它可以在下一次被引用时引用它(就像它再次在第 14 行被引用一样)?
-
getCustomerRelations()
方法返回 CustomerRelationData
对象列表,其中 CustomerRelationData
类还包含 getRelationId()
方法.现在上线
5 我们在第 5 行引用 value="relationId。在哪个对象(CustomerRelationAction.java 或 CustomerRelationData)上,将调用 getRelationId() 方法?
即使我不确定列表对象 CustomerRelationData 是否会出现在值堆栈中?如果是,它将被放入值堆栈的哪一行?
-
现在迭代器在第 6 行完成。之后,现在我再次引用代码
,On Which object(CustomerRelationAction.java 或 CustomerRelationData) ),
getRelationId() 方法将被调用?
I am new to strut 2 though I have worked on struts 1.2.In one of the pexisting project jsp file I have following code:
<script type="text/javascript">
var relationshipData = { // line1
page : '<s:property value="displayPage" />', // line2
records : '<s:property value="customerRelations.size" />', // line3
rows : [ <s:iterator value="customerRelations" status="iterStatus"> // line4
{ id : '<s:property value="relationId" />',
cell : [ '<s:property value="relationDesc" escapeJavaScript="true" />' ] } <s:if test="!#iterStatus.last">,</s:if> //line5
</s:iterator>] // line6
};
</script>
Request is coming CustomerRelationAction.java
which has method getCustomerRelations()
and getRelationId()
.
here are the questions :-
-
I put breakpoint inside method getCustomerRelations()
.i see flow is coming four time inside this method. Two times at line 3 and another two times at line 4.
As per my understanding flow should come only 1 time i.e at line 3. Once it completes getCustomerRelations at line 3 , should not put its value in value stack so that
it can refer to it nextime it is refered (like it is being reffered at line 14 again)?
-
getCustomerRelations()
method returns the list of CustomerRelationData
objects where CustomerRelationData
class also contains the getRelationId()
method.Now at line
5 we are refering value="relationId at line 5. On Which object(CustomerRelationAction.java or CustomerRelationData), getRelationId() method will be called?
even i am not sure will the list object CustomerRelationData will be present on value stack or not?If yes at which line it will be put in value stack?
-
Now the iterator completes at line 6.After that,now i refer the code <s:property value="relationId" />
again, On Which object(CustomerRelationAction.java or CustomerRelationData),
getRelationId() method will be called?
发布评论
评论(1)
1) 我不知道为什么您认为调用
customerRelations
的属性,然后在迭代器标记中使用customerRelations
只会调用getCustomerRelations()
一次;你使用它两次,所以至少它会被调用两次。如果您想保留对它的引用,请使用
创建对该集合的新引用。然而,我认为这样做没有意义,除非你的 getter 正在做一些耗时的事情。我没有看到相同的行为。给定问题的
片段,它会这样呈现(假设一个带有示例数据的虚拟三元素列表):
并且日志输出(在 getter 中带有调试语句)是这样的:
I我更有可能相信 JSP/JS/等。在此刻。
2) 迭代器标记将每个对象放在堆栈顶部,如 标记文档。堆栈顶部是第一个用于获取
relationId
值的对象。如果在栈顶没有找到它,OGNL 将遍历值栈,直到找到该属性,或者没有更多的栈。3) 请参阅前面的答案:一旦退出迭代器,堆栈上就不再存在客户关系,并且您将返回到操作。
1) I don't know why you think calling for a property of
customerRelations
and then usingcustomerRelations
in an iterator tag would only callgetCustomerRelations()
once; you're using it twice, so at a minimum it'll get called twice.If you want to keep a reference to it, use
<s:set>
to create a new reference to the collection. I don't see a point to doing so, however, unless your getter is doing something time-consuming.I don't see the same behavior. Given the question's
<script>
snippet, it renders thusly (assuming a dummy, three-element list with sample data):And the log output, with a debug statement in the getter, is this:
I'm more likely to believe the JSP/JS/etc. at this point.
2) The iterator tag puts each object on the top of the stack, as described in the tag docs. The top of the stack is the first object that will be used to get the value of
relationId
. If it isn't found on the stack top, OGNL will traverse the value stack until either the property is found, or there's no more stack.3) See the previous answer: once you're out of the iterator, there's no longer a customer relation on the stack, and you're back to the action.