java spring form taglib disabled 属性真的必须解析为字符串吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2187851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:55:35  来源:igfitidea点击:

Does spring form taglib disabled attribute really have to resolve to a string?

javaspringjspspring-mvc

提问by The Trav

I've been playing around with the spring form taglib lately and came across a fairly disturbing phenomenon.

我最近一直在玩 spring 形式的 taglib,遇到了一个相当令人不安的现象。

<form:select path="whatever" disabled="${true}">

Will render a select element that is NOT disabled

将呈现未禁用的选择元素

<form:select path="whatever" disabled="${'true'}">

Will render a select element that IS disabled.

将呈现禁用的选择元素。

This indicates to me that the tag expects a string in that attribute and is refusing to coerce any boolean values (possibly checking the type first).

这向我表明该标记需要该属性中的字符串,并且拒绝强制任何布尔值(可能首先检查类型)。

The impact is that I'm unable to do something like <form:select path="whatever" disabled="${someOtherfield.selectedId != -1}" />which is something that happens fairly often in our system.

影响是我无法做类似<form:select path="whatever" disabled="${someOtherfield.selectedId != -1}" />在我们系统中经常发生的事情。

Am I simply missing some part of the form taglibs functionality? Is this a legitimate design decision? A defect?

我是否只是缺少表单 taglibs 功能的某些部分?这是一个合法的设计决定吗?缺陷?

采纳答案by The Trav

Ok, I did some more digging around this one, because the work-arounds were looking too ugly.

好的,我对这个做了更多的挖掘,因为变通方法看起来太难看了。

http://forum.springsource.org/showthread.php?t=84102

http://forum.springsource.org/showthread.php?t=84102

The problem was that the JSP was evaluating the el, and blindly comparing the result of that evaluation using "true".equals

问题在于 JSP 正在评估 el,并使用“true”.equals 盲目地比较该评估的结果

Comparing a String to a Boolean using that method will always return false because the type's don't match so it's definitely a defect.

使用该方法将字符串与布尔值进行比较将始终返回 false,因为类型不匹配,因此这绝对是一个缺陷。

Fortunately the isDisabled method at fault is a protected one liner, so I have been able to work around it by extending the 8 input tag's effected and overriding that method to do a slightly more robust comparison.

幸运的是,错误的 isDisabled 方法是一个受保护的单行代码,所以我已经能够通过扩展 8 个输入标签的影响并覆盖该方法来做一个稍微更健壮的比较来解决它。

So the answer is, yes, it is a defect, and from skaffman's comments it looks like it's a bit of a problem with an old library not being very well updated as JSP EL was implemented.

所以答案是,是的,这是一个缺陷,从 skaffman 的评论来看,随着 JSP EL 的实施,旧库没有很好地更新,这似乎有点问题。

Thanks for your answers guys

谢谢各位的回答

回答by skaffman

This is a bit odd, right enough. The Spring source code shows that the disabledproperty of SelectTagis String, not boolean. This is clearly not the right thing to do, but I suspect it's still that way for legacy reasons (spring-form.tld pre-dates JSP EL).

这有点奇怪,够了。Spring 源代码显示的disabled属性是SelectTagis String, not boolean。这显然不是正确的做法,但我怀疑由于遗留原因(spring-form.tld 早于 JSP EL),它仍然是这种方式。

That leaves it up to the JSP runtime to coerce a booleaninto a String, and apparently it won't do this. I'm less surprised about this, since JSP EL is notoriously limited.

这让 JSP 运行时将 a 强制boolean转换为 a String,显然它不会这样做。我对此并不感到惊讶,因为 JSP EL 的局限性是出了名的。

So you're caught between two semi-broken implementations. You'll just need to make sure you pass String values to that attribute.

所以你被夹在两个半损坏的实现之间。您只需要确保将字符串值传递给该属性。

回答by axtavt

The cause of this design is that they have a special fallback code to force EL expression evaluation when container doesn't evaluate it. For example, this code:

这种设计的原因是他们有一个特殊的回退代码,当容器不评估它时,强制执行 EL 表达式评估。例如,这段代码:

<%@ page isELIgnored = "true" %>
...
${'Simple text'} <spring:message text = "${'Message text'} />"

produces ${'Simple text'} Message text

产生 ${'Simple text'} Message text

Probably, it's useful for some strange legacy container configurations.

也许,它对一些奇怪的遗留容器配置很有用。

That is, the following code wouldn't work if they make disabledattribute boolean:

也就是说,如果它们 makedisabled属性,以下代码将不起作用boolean

<%@ page isELIgnored = "true" %>
...
<form:select ... disabled = "${true}" />