我有一个 JSF 页面,我根据托管 bean 属性控制渲染器属性。
<p:commandLink action="#{smartphoneBean.drillDown(smartphone.ldapuser,smartphone.productGrp)}"
rendered="#{!(smartphone.ldapuser.charAt(0) ge '0' and smartphone.ldapuser.charAt(0) le '9')}" value="#{smartphone.ldapuser}">
一旦我运行我的代码。仍会呈现此值以任意数字开头的 commandLink。
我希望比较 char 值的整数表示形式。
你对这个问题有什么想法吗?
最佳答案
String#charAt(int)
返回一个 char
(Unicode 代码点),在 EL 中解释为 Number
。 '0'
被计算为 String
,而不是 Number
。因此比较总是会产生不想要的结果。
字符 0
到 9
的 Unicode 代码点为 48 to 57 。所以,应该这样做:
rendered="#{!(smartphone.ldapuser.charAt(0) ge 48 and smartphone.ldapuser.charAt(0) le 57)}" value="#{smartphone.ldapuser}">
另请参阅:
关于java - JSF 字符比较未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15520637/