当前位置: 代码迷 >> Web前端 >> webwork2 jsp list 不能自动IOC到Action有关问题
  详细解决方案

webwork2 jsp list 不能自动IOC到Action有关问题

热度:602   发布时间:2012-10-24 14:15:58.0
webwork2 jsp list 不能自动IOC到Action问题
模仿webworkshowcase做个批量的修改,但是将list对象在jsp种iterator出来之后,在转到另一个action中这个list对象count总是为空。。什么原因?

我的xwork配置以及src如下
java 代码
?
  1. package?com.sms.webwork.action;??
  2. ??
  3. import?java.util.List;??
  4. import?java.util.Iterator;??
  5. import?java.util.ArrayList;??
  6. import?com.opensymphony.xwork.ActionContext;??
  7. import?com.opensymphony.xwork.ActionSupport;??
  8. import?com.sms.model.User;??
  9. import?com.sms.service.UserExistsException;??
  10. public?class?UserToolAction?extends?BaseAction{??
  11. //??private?static?final?long?serialVersionUID?=?6776558938712115191L;??
  12. ????String?username;??
  13. ????String?phoneNumber;??
  14. ????String?message;??
  15. ????List?users=new?ArrayList();??
  16. ????List?telephones=new?ArrayList();??
  17. ??????
  18. ????public?UserToolAction()??
  19. ????{??
  20. ??
  21. ????}??
  22. ??????
  23. ????public?String?getUsername()?{??
  24. ????????return?username;??
  25. ????}??
  26. ??
  27. ????public?void?setUsername(String?username)?{??
  28. ????????this.username?=?username;??
  29. ????}??
  30. ??????
  31. ????public?String?getPhoneNumber()?{??
  32. ????????return?phoneNumber;??
  33. ????}??
  34. ??
  35. ????public?void?setPhoneNumber(String?phoneNumber)?{??
  36. ????????this.phoneNumber?=?phoneNumber;??
  37. ????}??
  38. ??????
  39. ????public?String?doSubmit()?{??
  40. ??????????
  41. ????????User?newUser=new?User();??
  42. ????????newUser.setUsername(username);??
  43. ????????newUser.setPhoneNumber(phoneNumber);??
  44. ??????????
  45. ????????try??
  46. ????????{??
  47. ????????????userManager.saveUser(newUser);??
  48. ????????}??
  49. ????????catch(UserExistsException?ex)??
  50. ????????{??
  51. ????????????return?INPUT;??
  52. ??????????????
  53. ????????}??
  54. ????????return?SUCCESS;??
  55. ????}??
  56. ??????
  57. ????public?String?doEdit()??
  58. ????{??
  59. ????????Iterator?itr=users.iterator();??
  60. ????????while(itr.hasNext())??
  61. ????????{??
  62. ????????????try??
  63. ????????????{??
  64. ????????????????userManager.saveUser((User)itr.next());??
  65. ????????????}??
  66. ????????????catch(Exception?e)??
  67. ????????????{??
  68. ????????????????e.printStackTrace();??
  69. ????????????}??
  70. ??
  71. ????????}??
  72. ????????return?SUCCESS;??
  73. ????}??
  74. ????public?String?doList()??
  75. ????{??
  76. ????????//users=userManager.getUsers(new?User());??
  77. ????????users.addAll(userManager.getUsers(new?User()));??
  78. ????????//users.addAll(userManager.getUsers(new?User()));??
  79. ????????return?SUCCESS;??
  80. ??
  81. ????}??
  82. ????public?String?doListSubmit()??
  83. ????{??
  84. ????????return?SUCCESS;??
  85. ????}??
  86. ????/**?
  87. ?????*?A?default?implementation?that?does?nothing?an?returns?"success".?
  88. ?????*?
  89. ?????*?@return?{@link?#SUCCESS}?
  90. ?????*/??
  91. ????public?String?save()?throws?Exception?{??
  92. ??
  93. ????????for?(?Iterator?iter?=?users.iterator();?iter.hasNext();)?{??
  94. ????????????User?p?=?(User)?iter.next();??
  95. ????????????userManager.saveUser(p);??
  96. ????????}??
  97. ????????return?SUCCESS;??
  98. ????}??
  99. ????public?String?execute()?throws?Exception?{??
  100. ??????????
  101. //??????users=userManager.getUsers(new?User());??
  102. ????????users.addAll(userManager.getUsers(new?User()));??
  103. ????????//users.addAll(userManager.getUsers(new?User()));??
  104. ????????return?SUCCESS;??
  105. ????}??
  106. ??????
  107. ????public?List?getUsers()?{??
  108. ????????return?users;??
  109. ????}??
  110. ??
  111. ????public?void?setUsers(List?users)?{??
  112. ????????this.users?=?users;??
  113. ????}??
  114. ????public?List?getTelephones()?{??
  115. ????????return?telephones;??
  116. ????}??
  117. ??
  118. ????public?void?setTelephones(List?telephones)?{??
  119. ????????this.telephones?=?telephones;??
  120. ????}??
  121. }??
xml 代码
?
  1. <!---->>??
  2. ??
  3. <xwork>??
  4. ????<include?file="webwork-default.xml"/>??
  5. ??
  6. ????<package?name="default"?extends="webwork-default">??
  7. ??
  8. ????????<default-action-ref?name="home"?/>??
  9. ??
  10. ????????<action?name="home"?class="com.opensymphony.webwork.example.HomeAction">??
  11. ????????????<result>/WEB-INF/pages/home.jspresult>??
  12. ????????action>??
  13. ??
  14. ????????<!---->??
  15. ????????<action?name="addUser"?class="com.sms.webwork.action.UserAction">??
  16. ????????????<result>/WEB-INF/pages/home.jspresult>??
  17. ????????????<result?name="input">/WEB-INF/pages/adduser.jspresult>??
  18. ????????action>??
  19. ????????<action?name="addUserSubmit"?class="com.sms.webwork.action.UserAction"?method="doSubmit">??
  20. ????????????<result>/WEB-INF/pages/adduserSubmited.jspresult>??
  21. ????????????<result?name="input">/WEB-INF/pages/adduser.jspresult>??
  22. ????????action>??
  23. ?????????<!---->??
  24. ????????????<result?type="redirect">listUser.actionresult>??
  25. ????????action>??
  26. ??????????
  27. ????package>??
  28. ??????
  29. ????<package?name="ui-tags"?extends="webwork-default"?namespace="/tags/ui">??
  30. ????????<action?name="example"?class="com.opensymphony.webwork.showcase.UITagExample">??
  31. ????????????<result>example.jspresult>??
  32. ????????????<result?name="input">example.jspresult>??
  33. ????????action>??
  34. ????????<action?name="exampleSubmit"?class="com.opensymphony.webwork.showcase.UITagExample"?method="doSubmit">??
  35. ????????????<result>exampleSubmited.jspresult>??
  36. ????????????<result?name="input">example.jspresult>??
  37. ????????action>?????????????????
  38. ????package>??
  39. ??????
  40. ????<package?name="sms"?extends="webwork-default"?namespace="/sms">??
  41. ????????<action?name="sms"?class="com.sms.webwork.action.SmsAction"?method="doSearch">??
  42. ????????????<result>/WEB-INF/pages/sms.jspresult>??
  43. ????????????<result?name="input">/WEB-INF/pages/sms.jspresult>??
  44. ????????action>??
  45. ????????<action?name="smsSubmit"?class="com.sms.webwork.action.SmsAction"?method="doSubmit">??
  46. ????????????<result>/WEB-INF/pages/smsSubmited.jspresult>??
  47. ????????????<result?name="input">/WEB-INF/pages/sms.jspresult>??
  48. ????????action>????
  49. ????????????????
  50. ????package>??
  51. ??????
  52. xwork>??
java 代码
?
  1. <!---->??
  2. <!---->??
  3. ??
  4. ??
  5. ??????
  6. ??
  7. ??
  8. ??
  9. ???method="post"?namespace="/user"?theme="simple"?validate="false">??
  10. ???? ?? ?? ID User?Name Telephone
    ??
  11. ?????????????????????/>??
  12. ????????????????
  13. ??
  14. ????????
  15. ????????????
  16. ????????????
  17. ????????????
  18. ????????
  19. ???????????value="users">??
  20. ????????????
  21. ????????????????
  22. ???????????????? ??
  23. ?????????????????????name="users(%{id}).username"?value="%{username}"?theme="simple"?/>??
  24. ????????????????
  25. ??
  26. ??
  27. ????????????
  28. ????????
  29. ????
  30. ??
  31. ????/>??
  32. ??
    • ??
    ??
  33. ?????action="home"/>??
  34. ?
  35. ??
  36. ??
  37. ??
?主要是user/editUser.action到user/editUserSubmit.action会出现users在save时总是为空的现象。

报错是
[sms] ERROR [http-8080-Processor23] ParametersInterceptor.setParameters(146) | Developer Notification (set webwork.devMode to false to disable this message):
Error setting expr 'users(6).username' with value '[Ljava.lang.String;@2ae278'

说明users里面根本没有对象。但是jsp上的list的确是users,java action中的list也是users,为什么到action还是没有呢?
1 楼 famoushz 2007-01-04  
我把完整的包给传上来了,去掉了jar
2 楼 famoushz 2007-01-04  
javaeye 的xml 显示有问题,还有jsp文件,还是大家看包里面的。
3 楼 famoushz 2007-01-04  
问题已经解决
4 楼 iseeiconquer 2007-01-17  
怎么解决的?
我也想知道,jsp页面里的整个集合的内容能否传到webwork action里面.
例如:<ww:select name="usertype" list="types"/>
我在刚开始进到相应页面list设置好后,再提交,如果处理这个提交的action方法里不重新设置list再result返回到那个jsp时就会报错:
could not be resolved as a collection/array/map/enumeration/iterator type
5 楼 dwangel 2007-01-17  
list的子元素要初始化。
就是说users(6)是null,当然会出错
  相关解决方案