Struts html:复选框查询
我有以下代码:
<td> <html:checkbox name="userForm" property="isActive" /></td>
在表单中,我有一个名为 isActive
类型为 char
的属性,我将如何获取检查的值?如果选中,我会收到一些像 o
的符号。
我正在使用 userform.getIsActive()。我哪里出错了?我想要“y”或“n”值。
I have the following code:
<td> <html:checkbox name="userForm" property="isActive" /></td>
In the form I have a property called isActive
of type char
, how would I get the checked value? I am getting some symbols like o
if checked.
I am using userform.getIsActive(). Where am I going wrong? I want 'y' or 'n' values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,当提交包含复选框的 HTML 表单时,如果选中该复选框,则发送的值是字符串“on”,如果未选中,则没有该复选框的字段 < em>根本在表单数据中。因此,您需要测试某个值是否存在。
您可以使用
value
属性更改选中复选框时发送的值(该属性适用于标准 HTML 标记和 Strutshtml:checkbox
标记,根据到 Struts 文档)。使用标准 HTML,您无法指定如果未选中复选框则应发送值。我对你说你要回复“o”感到有点困惑。 Struts 文档说相应的属性将是一个布尔值,而不是一个字符串。
By default, when an HTML form containing a checkbox is submitted, if the checkbox is checked the value that gets sent is the string "on", and if it's not checked there is no field for that checkbox at all in the form data. So you test for the presence of a value, or the absense of one.
You can change what value gets sent when the checkbox is checked by using the
value
attribute (which works both on a standard HTML tag and on a Strutshtml:checkbox
tag, according to the Struts docs). You can't, with standard HTML, specify that a value should be sent if the checkbox is not checked.I'm a bit confused at your saying you're getting "o" back. The Struts docs say the corresponding property will be a
boolean
, not aString
.如果窗体上的 isActive 属性为 char 类型,则它只能容纳一个字符。也许这就是为什么在您的情况下“on”值被截断为“o”的原因。
无论如何,我建议您将表单上的属性数据类型更改为布尔值。这样更容易、更合乎逻辑。每当您想要使用表单中的值时,您都可以非常轻松地在 Java 代码中执行
(form.getIsActive()?"y":"n")
或类似的操作,如果您 < em>必须获取该确切格式的值。希望这有帮助。If the isActive property on the form is of type char, it can hold only one character. Perhaps that's why the "on" value is being truncated to "o" in your case.
In any case, I'd advise you to change the property datatype to boolean on the form. It's just easier and more logical that way. Whenever you want to use the value from the form, you can very easily do
(form.getIsActive()?"y":"n")
or something like that in your Java code, if you must get the value in that exact format. Hope this helps.为了弥补HTML表单复选框的奇怪行为,struts 1.x中为静态表单bean提供了reset方法。
即使 Web 表单上有复选框,如果 form-bean 位于请求范围内,则不需要在 form-bean 类中使用重置方法。
如果 form-bean 处于会话范围内(默认情况下)并且其相应的 Web 表单具有复选框,则 formbean 中需要重置方法。
To make up the strange behaviour of check boxes of HTML form,reset method is given for static form beans in struts 1.x.
Even if checkbox is there on web form, if the form-bean is in request scope there is no need of reset method in form-bean class.
If form-bean is in session scope (which is by default )and its corresponding web form has check box(es),reset method is required in formbeans.