当前位置: 代码迷 >> Web前端 >> struts2、webwork 循环的应用,实现for功能
  详细解决方案

struts2、webwork 循环的应用,实现for功能

热度:209   发布时间:2012-10-31 14:37:31.0
struts2、webwork 循环的使用,实现for功能

在struts2及webwork中要实现如:

java 代码
  1. for(int?i=0;i<10;i++){}??

还是需要一些技巧的,我在做分页条的时候,要输出页码,怪了,用迭代器不行的,看了一下struts2的文档及例子也没发现用计数器的地方,偶然看了一下bea标签,哦,原来如此....

jsp代码

<s:bean id="counter" name="org.apache.struts2.util.Counter"><s:iterator>
  1. <s:bean?name="org.apache.struts2.util.Counter"?id="counter">??
  2. ???<s:param?name="first"?value="1"?/>??
  3. ???<s:param?name="last"?value="10"?/>??
  4. ???<s:iterator>??
  5. ?????counter:<s:property/>??
  6. ???</s:iterator>??
  7. </s:bean>??

其中first属性指定循环起始值,last指定循环终止值,其它相关属性可以查看org.apache.struts2.util.Counter类源码。在下面迭代器中输入循环的当前值,即:current

干脆把源码贴出来吧!

java 代码
  1. /* ?
  2. *?$Id:?Counter.java?471756?2006-11-06?15:01:43Z?husted?$ ?
  3. * ?
  4. *?Licensed?to?the?Apache?Software?Foundation?(ASF)?under?one ?
  5. *?or?more?contributor?license?agreements.???See?the?NOTICE?file ?
  6. *?distributed?with?this?work?for?additional?information ?
  7. *?regarding?copyright?ownership.???The?ASF?licenses?this?file ?
  8. *?to?you?under?the?Apache?License,?Version?2.0?(the ?
  9. *?"License");?you?may?not?use?this?file?except?in?compliance ?
  10. *?with?the?License.???You?may?obtain?a?copy?of?the?License?at ?
  11. * ?
  12. *??http://www.apache.org/licenses/LICENSE-2.0 ?
  13. * ?
  14. *?Unless?required?by?applicable?law?or?agreed?to?in?writing, ?
  15. *?software?distributed?under?the?License?is?distributed?on?an ?
  16. *?"AS?IS"?BASIS,?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY ?
  17. *?KIND,?either?express?or?implied.???See?the?License?for?the ?
  18. *?specific?language?governing?permissions?and?limitations ?
  19. *?under?the?License. ?
  20. */??
  21. package?org.apache.struts2.util; ??
  22. ??
  23. import?java.io.Serializable; ??
  24. ??
  25. ??
  26. /** ?
  27. *?A?bean?that?can?be?used?to?keep?track?of?a?counter. ?
  28. *?

    ?

    ?
  29. *?Since?it?is?an?Iterator?it?can?be?used?by?the?iterator?tag ?
  30. * ?
  31. */??
  32. public?class?Counter?implements?java.util.Iterator,?Serializable?{ ??
  33. ??
  34. ?????private?static?final?long?serialVersionUID?=?2796965884308060179L; ??
  35. ??
  36. ?????boolean?wrap?=?false; ??
  37. ??
  38. ?????//?Attributes?---------------------------------------------------- ??
  39. ?????long?first?=?1; ??
  40. ?????long?current?=?first; ??
  41. ?????long?interval?=?1; ??
  42. ?????long?last?=?-1; ??
  43. ??
  44. ??
  45. ?????public?void?setAdd(long?addition)?{ ??
  46. ?????????current?+=?addition; ??
  47. ?????} ??
  48. ??
  49. ?????public?void?setCurrent(long?current)?{ ??
  50. ?????????this.current?=?current; ??
  51. ?????} ??
  52. ??
  53. ?????public?long?getCurrent()?{ ??
  54. ?????????return?current; ??
  55. ?????} ??
  56. ??
  57. ?????public?void?setFirst(long?first)?{ ??
  58. ?????????this.first?=?first; ??
  59. ?????????current?=?first; ??
  60. ?????} ??
  61. ??
  62. ?????public?long?getFirst()?{ ??
  63. ?????????return?first; ??
  64. ?????} ??
  65. ??
  66. ?????public?void?setInterval(long?interval)?{ ??
  67. ?????????this.interval?=?interval; ??
  68. ?????} ??
  69. ??
  70. ?????public?long?getInterval()?{ ??
  71. ?????????return?interval; ??
  72. ?????} ??
  73. ??
  74. ?????public?void?setLast(long?last)?{ ??
  75. ?????????this.last?=?last; ??
  76. ?????} ??
  77. ??
  78. ?????public?long?getLast()?{ ??
  79. ?????????return?last; ??
  80. ?????} ??
  81. ??
  82. ?????//?Public?-------------------- ??
  83. ?????public?long?getNext()?{ ??
  84. ?????????long?next?=?current; ??
  85. ?????????current?+=?interval; ??
  86. ??
  87. ?????????if?(wrap?&&?(current?>?last))?{ ??
  88. ?????????????current?-=?((1?+?last)?-?first); ??
  89. ?????????} ??
  90. ??
  91. ?????????return?next; ??
  92. ?????} ??
  93. ??
  94. ?????public?long?getPrevious()?{ ??
  95. ?????????current?-=?interval; ??
  96. ??
  97. ?????????if?(wrap?&&?(current?<?first))?{ ??
  98. ?????????????current?+=?(last?-?first?+?1); ??
  99. ?????????} ??
  100. ??
  101. ?????????return?current; ??
  102. ?????} ??
  103. ??
  104. ?????public?void?setWrap(boolean?wrap)?{ ??
  105. ?????????this.wrap?=?wrap; ??
  106. ?????} ??
  107. ??
  108. ?????public?boolean?isWrap()?{ ??
  109. ?????????return?wrap; ??
  110. ?????} ??
  111. ??
  112. ?????public?boolean?hasNext()?{ ??
  113. ?????????return?((last?==?-1)?||?wrap)???true?:?(current?<=?last); ??
  114. ?????} ??
  115. ??
  116. ?????public?Object?next()?{ ??
  117. ?????????return?new?Long(getNext()); ??
  118. ?????} ??
  119. ??
  120. ?????public?void?remove()?{ ??
  121. ?????????//?Do?nothing ??
  122. ?????} ??
  123. } ??
  124. ??
</s:iterator>
</s:bean>
  相关解决方案