Struts 标签属性如何工作?
在这段代码中:
<html:text property="txtItem5" disabled="disTxtItem5">
我可以看到“txtItem5”来自 ActionForm 中的 getTxtItem5(),但是在项目中搜索“disTxtItem5”的其他子字符串似乎没有揭示任何远程相关的内容,尽管显然框架以某种方式从该字符串中提取布尔值,这显然意味着它比我目前的理解更复杂。
有人可以很好地解释如何评估这些表达式,或者为我指出一个方向吗?
In this code:
<html:text property="txtItem5" disabled="disTxtItem5">
I can see that "txtItem5" comes from a getTxtItem5() in the ActionForm, but searching the project for other substrings of "disTxtItem5" seemingly reveals nothing remotely related, though clearly somehow the framework is pulling a boolean from this string, which clearly means that it's more complicated than my current understanding.
Could someone give a good explanation of how these expressions are evaluated, or point me in the direction of one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:在我最初的回复中(见下文)我说Struts 管理转换,但我错了。我不记得到底发生了什么,所以我拿出了 Struts 的源代码并看了一下。原来是服务器进行了转换。 JSP 在执行之前被转换为 servlet,并且此处 false 用于非布尔值。
例如,我使用了以下标记:
已翻译为以下 html(未禁用):
这发生在 servlet 中。以下是我的 servlet 包含的上述标记内容:
可以看出,禁用值是直接使用 false 生成的。我对 Jasper 编译器(我使用 Tomcat)做了一些深入研究,我认为是 org.apache.jasper.compiler.JspUtil 类负责转换,代码如下:
由于我在禁用中插入了 BlaBla该字段应该回退到
Boolean.valueOf(s).booleanValue();
,它执行以下操作:这样,BlaBla 结果为 false。
ORIG: 以下是我最初的回复,但不正确。我所描述的实际上是当请求参数绑定到操作表单时发生的情况。disabled
属性是布尔类型,因此它应该只接收映射到布尔值的值。
disabled="disTxtItem5"
会抛出 ConversionException,因为文本disTxtItem5
未映射到布尔值。Struts 使用 CommonBeanUtils 进行转换,因此将使用 BooleanConverter,代码如下
:关于这一点,我不记得 Struts 是否只是记录异常并失败,默默地将 false 设置为参数值,或者传播异常(自从我使用 Struts 以来已经有一段时间了:D,但我更倾向于认为它只是设置为 false 并继续)。
即使被忽略,日志也应该指出异常。为
org.apache.commons.beanutils
或org.apache.struts
设置记录器应该指示任何转换错误。EDIT: In my original response (see below) I said that Struts manages the conversion, but I was wrong. I didn’t remember exactly what was going so I pulled out Struts’s sources and took a look. It turns out that the conversion is made by the server. The JSP is converted to a servlet before execution and it is here that false is used for non boolean values.
For example, I used the following tag:
Which was translated to the following html (no disabled):
This happened in the servlet. Here is what my servlet contains for the above tag:
As it can be seen, the disabled value is generated with false, directly. I did some more digging into the Jasper compiler (I used Tomcat) and I think that it is the org.apache.jasper.compiler.JspUtil class that is responsible for the conversion, with the following code:
Since I inserted BlaBla in the disabled field this should fallback to
Boolean.valueOf(s).booleanValue();
which does the following:This way, BlaBla results in false.
ORIG: The following was my original response, but was incorrect. What I was describing was in fact what happens when request parameters are binded to the action form.
The disabled attribute is of type boolean, so it should receive only values that map to a boolean.
disabled="disTxtItem5"
would throw a ConversionException because the textdisTxtItem5
does not map to a boolean.Struts uses CommonBeanUtils to make conversions, so a BooleanConverter will be used, with code like the following:
At this point I don’t remember if Struts just logs the exception and fails silently setting false as the value of the parameter or, the exception is propagated (it’s been a while since I used Struts :D but I’m more inclined to think that it just sets false and continues).
The logs should point out the exception even if it is ignored. Setting a logger for
org.apache.commons.beanutils
ororg.apache.struts
should indicate any conversion errors.