日期:2014-05-19  浏览次数:20739 次

struts2的iterator标签中使用if标签判断的问题
action中定义了myList
List myList = new ArrayList();
myList.add("a");
myList.add("b");
myList.add("c");
并且定义了public List getMyList() { return myList; }
在jsp页面中这样遍历
<s:iterator var="iter" value="myList" status="sts">
  <s:if test="#iter=='b'">
  b is found!!!
  </s:if>
</s:iterator>
其中判断当前值为b的这句会导致报这样一个异常:
警告: Caught an exception while evaluating expression '#iter=='b'' against value stack
java.lang.NumberFormatException: For input string: "a"
另外把判断语句改成<s:if test="myList[#sts.index]=='b'">也不行,想请问下这是什么原因,应该怎样改?谢谢

------解决方案--------------------
可改为

<s:iterator var="iter" value="myList" status="sts">
${iter == "b" ? "b is found!!!" : ""}
</s:iterator>




------解决方案--------------------
楼主 ,我干肯定是因为 你获取的#iter 是 String类型的,然后跟char做比较就出问题了,,,
可以 
<s:if test='#iter.equals("b")'>
b is found!!!
</s:if>

<s:if test='#iter=="b"'>
b is found!!!
</s:if>
都是可以的。。。
------解决方案--------------------
探讨
楼主 ,我干肯定是因为 你获取的#iter 是 String类型的,然后跟char做比较就出问题了,,,
可以
<s:if test='#iter.equals("b")'>
b is found!!!
</s:if>

<s:if test='#iter=="b"'>
b is found!!!
</s:if>
都是可以的。。。

------解决方案--------------------
探讨

楼主 ,我干肯定是因为 你获取的#iter 是 String类型的,然后跟char做比较就出问题了,,,
可以
<s:if test='#iter.equals("b")'>
b is found!!!
</s:if>

<s:if test='#iter=="b"'>
b is found!!!
</s:if>
都是可以的。。。

------解决方案--------------------
2楼正解,顶一下