如何在struts set tag中获取随机值

时间:2013-10-22 09:03:55

标签: java javascript jquery jsp struts2

我为我的应用程序使用struts2标签。我需要将值从脚本随机更改为s:set tag并填充到javascript数组中。如果我动态添加意味着我得到 2012 - diff + 1 的输出,但我的预期输出为 2011 。我怎样才能实现这一点,我的代码是

var threeDatas = [];
var prevyear = [];
var diff;
var flagthree = false;

<% int j = 0;%>

<s:iterator value="testlist">
    prevyear.push(<s:property value="year"/>);
    if (<%= j - 1%> !== -1) {
        if (prevyear[<%= j - 1%>] !== prevyear[<%= j%>] - 1) {
            diff = prevyear[<%= j%>] - prevyear[<%= j - 1%>];
        }
        if (diff > 1) {                                                   
            for (inc = 1; inc < diff; inc++) {
                <s:set var="incyear"> <s:property value="year"/> - diff+1</s:set>
                 threeDatas.push(['<s:property value="#incyear"/>', null, null]);
            }
            flagthree = true;
            diff = 0;
        }

<% j++;%>

</s:iterator>

我需要我的最终数组值应该是链接

['x', 'Cats','Dogs'],
     ['2008',   10, null],
     ['2009',   20, null],
     ['2010',   null, null],
     ['2011',   null, 23],
     ['2012',   null, 34]

2 个答案:

答案 0 :(得分:2)

您可以在JavaScript中使用eval()方法。所以,如果你可以生成像

这样的东西

var incyear = eval('<s:property value="year"/> - diff + 1');

答案 1 :(得分:0)

我通过使用set

找到了另一种方法
<s:set var="incyear"> <s:property value="year"/> - diff+1</s:set>
                 threeDatas.push([<s:property value="#incyear"/>+"", null, null]);

以及以下代码:

var threeDatas = [];
var prevyear = [];
var diff;
var flagthree = false;
<% int j = 0;%>


<s:iterator value="testlist">
    prevyear.push(<s:property value="year"/>);
    if (<%= j - 1%> !== -1) {
        if (prevyear[<%= j - 1%>] !== prevyear[<%= j%>] - 1) {
            diff = prevyear[<%= j%>] - prevyear[<%= j - 1%>];
        }
        if (diff > 1) {                                                   
            for (inc = 1; inc < diff; inc++) {
                <s:set var="incyear"> <s:property value="year"/> - diff+1</s:set>
                 threeDatas.push([<s:property value="#incyear"/>+"", null, null]);
            }
            flagthree = true;
            diff = 0;
        }

<% j++;%>

</s:iterator>

通过使用它,你可以得到输出

['x', 'Cats','Dogs'],
 ['2008',   10, null],
 ['2009',   20, null],
 ['2010',   null, null],
 ['2011',   null, 23],
 ['2012',   null, 34]