日期:2014-05-17  浏览次数:20606 次

struts的logic标签
我定义了
Java code

HashMap[] map = new HashMap[4];
for(int i=1;i<=4;i++){
                map[i-1].put("x1", i+i);
                map[i-1].put("x2", (i+i)*(i+i));
                map[i-1].put("x3", (i+i)*(i+i)*(i+i));
}



我想在页面上执行4次,并且每一次都取x1,x2,x3的值,我应该在页面上怎样写?
麻烦大家了~

------解决方案--------------------
楼主,你上面代码运行过没?hashmap是一组键值对,map[0]未初始化,会报空指针的,
HashMap[] map = new HashMap[4];
for(int i=1;i<=4;i++){
map[i-1]=new HashMap();
map[i-1].put("x1", Integer.valueOf(i+i));
map[i-1].put("x2", Integer.valueOf((i+i)*(i+i)));
map[i-1].put("x3", Integer.valueOf((i+i)*(i+i)*(i+i)));
}
具体在咋用标签写,还没想出来,静待佳音
------解决方案--------------------
<%

int i = 1;
HashMap map=new HashMap();
map.put("x1", Integer.valueOf(i+i));
map.put("x2", Integer.valueOf((i+i)*(i+i)));
map.put("x3", Integer.valueOf((i+i)*(i+i)*(i+i)));
request.setAttribute("xKey",map);




%>
<logic:iterate id="element" indexId="ind" name="xKey">
<bean:write name="ind"/>. 
<bean:write name="element" property="key"/>:
<bean:write name="element" property="value"/><BR>
</logic:iterate><P>




这样可以实现。。。但是你为什么要把同样的东西存放四次我实在不明白。如果你非要输出四次的话就在底下的循环外面再加一个迭代循环,控制循环的次数是可以的,不要存的时候就存四次了。这是我的意见,可能个人用以不同吧,见谅。。。。