1

Нello,

I am programming in a jsp environment, where I need to do the following:
In an iterator inside an < s:if> I have to make a query, that takes a value from one input field and subtracts it with another value from another input field and sets this to a variable, then does the same, but it adds the second value this time, and sets the result to a second variable, and in the end passes both results to the query. The subtraction works, but instead of addition, it just appends the value of the 2nd input field to the value of the first, when setting the second variable. How can I resolve this in s:property? I tried using < s:property value="${"(...)first"+"(...)second"}"/>, but it gives me an error, that it doesn't support runtime expressions. Code:

    <s:set name="thequery">
        <s:iterator value="#attr.var" status="status">
            <%--(...)other unrelevant code--%>
            <s:if test="#attr.xinput3[#status.index] != 0">
                <%--the one below works fine-->
                <s:set name="around1">
                        <s:property value="#attr.xinput[#status.index]-#attr.xinput3[#status.index]" />
                </s:set>
                <%--this one just appends the value of xinput3[] to the value of xinput[], instead of adding them-->
                <s:set name="around2">
                        <s:property value="#attr.xinput[#status.index]+#attr.xinput3[#status.index]" />
                </s:set>
                <s:property value="#attr.xvar[#status.index]" />=<s:property
                        value="around1" />!<s:property value="around2" />;</s:if>
            <s:else>
                            <%--unrelevant code--%>
            </s:else>
        </s:iterator>
    </s:set>

The result: for, say, xinput[] = 12 and xinput3[] = 2:

10!122;

While I need it to be

10!14;

How can I fix this?

4

2 回答 2

0

您的值被解释为字符串而不是数字。如果您的值是整数,则可以在添加之前创建它们的新 Integer 对象。

<s:property value="new java.lang.Integer(#attr.xinput[#status.index]) +
                   new java.lang.Integer(#attr.xinput3[#status.index])" />
于 2013-08-09T12:22:23.590 回答
0

我自己找到了答案:

<s:if test="#attr.xinput3[#status.index] != 0">
                    <s:set name="around1">${xinput[status.index]-xinput3[status.index]}</s:set>
                    <s:set name="around2">${xinput[status.index]+xinput3[status.index]}</s:set>
                    <s:property value="#attr.xvar[#status.index]" />=<s:property
                        value="around1" />!<s:property value="around2" />;</s:if>

结果表明 < s:set> 支持运行时表达式,如果按照上面的方式编写的话。它现在工作正常。

于 2013-08-09T14:27:41.153 回答