Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Boolean variable in c:if

Tags:

java

jsf

jstl

I am using this code in JSF.:

<c:if test="#{sV.done.booleanValue()}">
     <option value="#{sV.id}" selected="selected">#{sV.text}</option>
</c:if>
<c:if test="#{not sV.done.booleanValue()}">
     <option value="#{sV.id}">#{sV.text}</option>
</c:if>

sv is my class containing data (pojo), done is an Boolean variable, I want to display option tag with selected attribute if sV.done is true. But I couldn't make it. Don't know where I am wrong.

Otherwise there can be something wrong with c, because c:forEach was not working before some time in my case in same page? It can be the reason? Where I am wrong?

Every time it displays option tag without selected attribute.

like image 550
Patriks Avatar asked Oct 06 '12 09:10

Patriks


People also ask

Can you use a boolean in an if statement?

The if statement has the following syntax: if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.

Is == a boolean expression?

A boolean expression is an expression that evaluates to a boolean value. The equality operator, == , compares two values and produces a boolean value related to whether the two values are equal to one another.

How do you use boolean in C?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

What is boolean condition in C?

In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically, the bool type value represents two types of behavior, either true or false. Here, '0' represents false value, while '1' represents true value. In C Boolean, '0' is stored as 0, and another integer is stored as 1.

How to declare a Boolean variable in C++?

For this, C++ has a bool data type, which can take the values true (1) or false (0). A boolean variable is declared with the bool keyword and can only take the values true or false:

How to check if a Boolean variable is true or false?

In the above example, we have included the stdbool.h header file as we are using the bool type variable in our program, then we have declared a variable x of type boolean using bool and initialized it with value false. After that, we applied the condition checks using the if-else block to determine if the value of variable x is true or false.

Can a bool variable have a value of 1?

In C#, variable of type bool can have one of two values, true or false, but they don't act as numbers, so you can't say they are 1 and 0 (although they are usually implemented that way). Also, in most languages, if you mean “ a and b are both at most x ”, you can't write it as a && b <= x.

How do I use bool in C language?

Use of bool in C. The C99 standard for C language supports bool variables. Unlike C++, where no header file is needed to use bool, a header file “stdbool.h” must be included to use bool in C. If we save the below program as .c, it will not compile, but if we save it as .cpp, it will work fine.


2 Answers

Try this: -

<c:if test="${sV.done == true}">...</c:if>  // or
<c:if test="${sV.done eq true}">...</c:if>  // or
<c:if test="${sV.done}">...</c:if>   // or

And for negation (If sV.done is false): -

<c:if test="${! sV.done}">...</c:if>    /// OR
<c:if test="${not sV.done}">...</c:if>  /// OR
<c:if test = "${sV.done != true}">...</c:if>     // OR
<c:if test = "${sV.done ne true}">...</c:if>  // OR

For more on if with operators check out this link: - JSTL if

like image 194
Rohit Jain Avatar answered Oct 12 '22 23:10

Rohit Jain


Your syntax is fine, provided that you're using EL 2.2. So, none of the JSTL <c:xxx> tags are been interpreted? You need to import the JSTL core taglib. It's unclear what view technology and JSTL version you're using, so here are import examples for both JSP and Facelets.

JSP with JSTL 1.0:

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

JSP with JSTL 1.1/1.2:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Facelets 1.x with JSTL 1.1/1.2:

<html ... xmlns:c="http://java.sun.com/jstl/core">

Facelets 2.x with JSTL 1.2:

<html ... xmlns:c="http://java.sun.com/jsp/jstl/core">

See also:

  • Our JSTL wiki page

Unrelated to the concrete problem, have you considered using a JSF UISelectOne component instead of fiddling with <option> elements yourself? You can find some concrete examples in our h:selectOneMenu wiki page.

like image 5
BalusC Avatar answered Oct 13 '22 00:10

BalusC