[英]<s:if> test expression evaluation for boolean value doesn't work as expected
I want to check value of variable bool_val
using Struts 2 tag <s:if>
but it's not working.我想使用 Struts 2 标签
<s:if>
检查变量bool_val
值,但它不起作用。
<%@ taglib prefix="s" uri="/struts-tags" %>
<%boolean bool_val=true;%>
real value : <%=bool_val%></br>
expression evaluated value :
<s:if test="%{bool_val==true}">
TRUE
</s:if><s:else>
FLASE
</s:else>
I also tried following test expressions too, but still not working.我也尝试过以下测试表达式,但仍然无法正常工作。
<!--
bool_val
bool_val==true
%{bool_val}
%{bool_val==true}
%{bool_val=="true"}
-->
Use struts tag to create a variable like this使用 struts 标签创建这样的变量
<s:set var="bool_val" value="true" />
expression evaluated value :
<s:if test="%{#bool_val == true}">
TRUE
</s:if><s:else>
FALSE
</s:else>
Here is a sample tutorial .这是一个示例教程。
There is a shorter version to the one suggested by Visruth CV : Visruth CV 建议的版本有一个较短的版本:
<s:set var="foo" value="true" />
expression evaluated value :
<s:if test="foo">
TRUE
</s:if><s:else>
FALSE
</s:else>
In case you want to check the boolean value against an Action attribute, here is the way to go :如果您想根据 Action 属性检查布尔值,可以使用以下方法:
class FooAction extends ActionSupport {
private Boolean _bar = true;
public Boolean isBar() { return _bar; }
}
And in the jsp file :在jsp文件中:
expression evaluated value :
<s:if test="isBar()">
TRUE
</s:if>
<s:else>
FALSE
</s:else>
You can't use a scriptlet variable in Struts tags unless you put this variable to the value stack.您不能在 Struts 标签中使用 scriptlet 变量,除非您将此变量放入值堆栈。 But you'd better not use a scriptlet variable, but the variable value.
但是最好不要使用scriptlet 变量,而是使用变量值。
<%@ taglib prefix="s" uri="/struts-tags" %>
<%boolean bool_val=true;%>
real value : <%=bool_val%><br/>
expression evaluated value :
<s:set var="bool_val"><%=bool_val%></s:set>
<s:if test="#bool_val == 'true'">
TRUE
</s:if><s:else>
FALSE
</s:else>
If getter method for your boolean variable in Action class is isBool()
then use <s:if test="bool">
.如果 Action 类中布尔变量的 getter 方法是
isBool()
则使用<s:if test="bool">
。 The key is to remove is
from the method name and use.关键是去除
is
从方法的名称和用途。 For example, if method is isApple()
use <s:if test="apple">
.例如,如果方法是
isApple()
使用<s:if test="apple">
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.