无法在 Struts Multibox 中取消选中所有内容
我有一个在 weblogic 上运行的 j2ee 应用程序。我对我的多功能盒感到困惑。
我对 multibox 的了解是,选中的项目将在提交时作为字符串数组传递。
我不知道为什么在我的应用程序中,当我取消选中一个或多个复选框时,只要单个框保持选中状态,它就可以正常工作,但是当我取消选中所有内容时,提交的数组是以前检查的多框的数组,而它应该是是空的。
你能帮我吗?
I have a j2ee application running on weblogic. I was confused with my multibox.
What I know of multibox is that the checked items will be passed as an array of strings on submit.
I don`t know why in my application it works fine when i uncheck a checkbox or more, as long as a single box remains checked but when I uncheck everything, the submitted array is the array of the previously checked multiboxes when it was supposed to be empty.
Can you help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您熟悉 ActionForm 类中的 Reset() 方法吗?
此方法的目的是重置复选框。如果您的表单中选中了复选框并提交了该复选框,则该复选框将出现在请求中。如果未选中该复选框,则不会在请求中发送任何内容(GET 提交是观察此行为的简单方法)。
当 Struts 执行请求绑定时,它会按名称将请求中的参数与表单中的参数相匹配。也就是说,如果有东西可以匹配。
现在考虑以下步骤:
上面的内容适用于多个复选框,但您会得到一个数组,而不仅仅是一个值。
输入reset()方法。这是由 Struts 在绑定请求之前调用的。您可以在此处将字段值设置为 false。如果它到达请求,Struts 会将其替换为 true =>好的。如果它没有到达请求(因为它未经检查),该值将保持 false = >再次确定。
多功能盒也是如此。您必须通过将数组长度减小为零(但不为空)来重置 ActionForm 中的值列表。
如果您的 ActionForm 有请求范围,通常并不重要,因为每次请求时都会重新创建对象。但对于带有复选框的会话范围 ActionForm,reset() 是必须的。
Are you familiar with the reset() method on the ActionForm class?
The purpose in life for this method is to reset checkboxes. If you have a checked checkbox in your form and you submit it, that checkbox will be on the request. If the checkbox is unchecked nothing will be sent on the request for it (a GET submit is a simple way to observe this behavior).
When Struts performs the request bind, it matches by name the parameters from the request to the parameters in the form. That is, if there is something to match.
Now consider these steps:
The above applies for multi checkboxes, but you get an array instead of just one value.
Enter the reset() method. This is called by Struts before binding the request. Here you can set your field value to false. If it arrives in the request Struts will replace it with true => OK. If it does not arrive on the request (because it's unchecked) the value will remain false = > OK again.
The same goes for multiboxes. You have to reset the list of values from the ActionForm by reducing the array to zero length (but not null).
If your ActionForm has a request scope, it usualy does not matter because the object is recreated at each request. But for a session scoped ActionForm with checkboxes, reset() is a must.