简体   繁体   English

struts2 s:if 标签对字符串和 boolean 的行为不同

[英]struts2 s:if tag behaves differently for string and boolean

I have an s:iterator with s:if like below我有一个s:iterators:if如下

<s:iterator var="connections" value="serviceItems">
   <s:if test='disableCheckBox == "Y"'>
      disableCheckBox works
   </s:if>
   <s:if test='#{troubleSupported.equals("true")}'>
     troubleWorks
   </s:if>
</s:iterator>

The above code works, but if I change the second if like below, it does not work.上面的代码有效,但如果我像下面这样更改第二个代码,它就不起作用。

<s:if test="troubleSupported=='true'">
<s:if test="troubleSupported.equals('true')">

Can someone please explain what could be reason for this difference.有人可以解释造成这种差异的原因。 Am I doing something wrong here?我在这里做错了什么吗?

The only difference I could see is, the action variable is declared as a boolean for troubleSupported and String for disableCheckBox (Note: Not directly, but through some intermittent Vectors).我能看到的唯一区别是,对于 troubleSupported,action 变量被声明为boolean ,对于 disableCheckBox,声明为String (注意:不是直接声明,而是通过一些间歇性向量声明)。 Could this affect the outcome like what I am seeing.这会像我所看到的那样影响结果吗?

PS: To complicate this a bit further, if I swap the single and double quotes for the working scenario, PS:为了让这个更复杂一点,如果我交换工作场景的单引号和双引号,

<s:if test="disableCheckBox == 'Y'"> does not work, <s:if test="disableCheckBox == 'Y'">不起作用,

but <s:if test="#{troubleSupported.equals('true')}"> continues to work.但是<s:if test="#{troubleSupported.equals('true')}">继续工作。

Currently, I'm not looking at this.目前,我不看这个。 I am confused enough with my original issue.我对我原来的问题很困惑。 :) :)

<s:if test="troubleSupported=='true'"> is not a correct way to check equality in java, and many more languages. <s:if test="troubleSupported=='true'">不是检查 java 和更多语言中是否相等的正确方法。 Correct way is: <s:if test="troubleSupported===true"> .正确的方法是: <s:if test="troubleSupported===true"> Far better is: <s:if test="troubleSupported"> because you want to test if your boolean is true, so keep it simple.更好的是: <s:if test="troubleSupported">因为您想测试您的 boolean 是否为真,所以保持简单。

You can check boolean values with thymeleaf-like expressions too, but it is more verbose: <s:if test="#{troubleSupported.equals('true')}"> .您也可以使用类似 thymeleaf 的表达式检查 boolean 值,但它更冗长: <s:if test="#{troubleSupported.equals('true')}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM