Struts 2:s:if 内的 s:iterator 不工作

发布于 2024-12-02 03:57:25 字数 525 浏览 2 评论 0原文

我有一个关于 struts 2 和 s:if 标签的问题。根据 s:if 文档中的描述,以下示例应该有效:

...
<s:iterator value="questao.alternativas" status="alternativa">
    <tr>
        <td>
            <s:if test="#alternativa.correta == true">Correta!</s:if>
        </td>
    </tr>
</s:iterator>
...

但这不适用于我的情况,您能帮忙吗?更多详细信息:

在操作中找到 questao.alternativas 并且工作正常,所有“alternativas”都被“迭代”!

在上述示例中永远不会调用 getCorreta() 方法。

当使用值 true 时被打印。

有什么想法吗?

谢谢!

I have a question regarding struts 2 and s:if tags. As per described on the documentation of s:if the following example was supposed to work:

...
<s:iterator value="questao.alternativas" status="alternativa">
    <tr>
        <td>
            <s:if test="#alternativa.correta == true">Correta!</s:if>
        </td>
    </tr>
</s:iterator>
...

But this is not working on my case, could you please help? More details:

questao.alternativas is found on the action and it works fine, all "alternativas" are "iterated"!

the getCorreta() method is never call on the example described above.

when using the value true is printed.

Any ideas??

Thanks!

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

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

发布评论

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

评论(2

吲‖鸣 2024-12-09 03:57:25

iterator 标记中指定 status 将推送 IteratorStatus 在值堆栈上。您需要查阅 IteratorStatus 的文档以了解要使用的有效属性,并且 correta 不是有效属性。如果您想访问当前迭代的对象,请指定 var 属性。

编辑评论:
正如四元数所说,您不需要指定 var 来访问当前迭代的对象,这是正确的。它已经位于价值堆栈的顶部。

Specifying status in iterator tag will push an instance of IteratorStatus on value stack. You need to consult the documentation of IteratorStatus for valid attributes to use and correta is not a valid attribute. If you want to access the current iteration's object specify a var attribute instead.

Edit for comment:
That's right as Quaternion said you don't need to specify var for accessing current iteration's object. It's already on top of value stack.

娜些时光,永不杰束 2024-12-09 03:57:25

快速查看您的代码,我发现您正在引用条件(if)语句中的状态。您的条件应该直接引用您正在迭代的列表中的对象的访问器方法,例如:

<s:iterator value="questao.alternativas" status="stat">
    <tr>
        <td>
            // If correta is a boolean value, there is not need to use an 
            // == operator at all
            <s:if test="%{correta}">
                Correta!
            </s:if>
        </td>
    </tr>
</s:iterator>

当迭代器遍历提供的列表时,假设 questao.alternativas 是具有访问器方法的对象列表,您可以直接引用您的列表中的方法迭代器。

迭代器中的状态使您可以访问迭代器的位置。例如,如果您想确定循环是否位于列表中的最后一个元素以执行特殊操作,您可以执行以下操作:

<s:if test="%{#stat.last}">--- END OF LIST ---</s:if>

或者获取数组或列表中当前元素的索引位置:

<s:property value=%{#stat.index} />

Quickly looking at your code I see that you are referencing the status in your conditional(if) statement. Your conditional should directly reference the accessor method of the object in the list your are iterating through, for example:

<s:iterator value="questao.alternativas" status="stat">
    <tr>
        <td>
            // If correta is a boolean value, there is not need to use an 
            // == operator at all
            <s:if test="%{correta}">
                Correta!
            </s:if>
        </td>
    </tr>
</s:iterator>

When an iterator traverses a supplied list, assuming questao.alternativas is a list of objects with accessor methods, you directly reference the methods inside your iterator.

The status in an iterator gives you access to the iterator's position. For example, if you want to determine if your loop is at the last element in the list to perform something special you would do something like:

<s:if test="%{#stat.last}">--- END OF LIST ---</s:if>

Or to get the indexed position of the current element in your array or list:

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