Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<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.

<%@ 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"}
 -->
like image 814
Akash Jain Avatar asked Apr 15 '14 05:04

Akash Jain


People also ask

How do you check if a boolean value is in if in python?

Remember that True and False are Booleans in Python. This means that if and other conditional statements will use Boolean math to compute their Boolean state. You should also note the need for a colon ( : ) at the end of the if statement. This is needed at the end of a control flow statement.

What do you call any value or expression that can be evaluated to be true or false in if function?

Logical_test is any value or expression that can be evaluated to TRUE or FALSE. For example, [Quarter1]=100 is a logical expression; if the value in one row of the column, [Quarter1], is equal to 100, the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.

How do you verify a boolean?

To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean.

How do you do a boolean in an if statement?

An if statement checks a boolean value and only executes a block of code if that value is true . To write an if statement, write the keyword if , then inside parentheses () insert a boolean value, and then in curly brackets {} write the code that should only execute when that value is true .


2 Answers

Use struts tag to create a variable like this

<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.

like image 127
Visruth Avatar answered Oct 07 '22 12:10

Visruth


If getter method for your boolean variable in Action class is isBool() then use <s:if test="bool">. The key is to remove is from the method name and use. For example, if method is isApple() use <s:if test="apple">.

like image 25
Rohit Reddy Abbadi Avatar answered Oct 07 '22 12:10

Rohit Reddy Abbadi