Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if variable exists in JSTL

Tags:

java

jsp

jstl

I've got a PersistenceSet and would like to check if it contains a certain variable.

How can I check in the JSTL whether subitem exists or not?

However when I try to access a non-existant variable like this:

<c:if test="${not empty item.subitem}">       
   <c:out value="${item.subitem}" /><br />
</c:if>

I get a PropertyNotFoundException:

Property 'subitem' not found on type com.company.classname

like image 203
Hedge Avatar asked Aug 11 '11 09:08

Hedge


People also ask

What is the use of cif tag?

JSTL <c:if> Core Tag It is more or like a if statement in java which evaluates a condition and executes a block of code if the result is true. Syntax: This is the basic syntax of <c:if> core tag. The set of statements enclosed within <c:if> tag gets executed if test=”true”.

What is c if test?

C if Statement The if statement evaluates the test expression inside the parenthesis () . If the test expression is evaluated to true, statements inside the body of if are executed. If the test expression is evaluated to false, statements inside the body of if are not executed.

Why we use JSTL in JSP?

JSTL stands for JSP Standard Tag Library. JSTL is the standard tag library that provides tags to control the JSP page behavior. JSTL tags can be used for iteration and control statements, internationalization, SQL etc.

What is JSTL full form?

The Jakarta Standard Tag Library (JSTL; formerly JavaServer Pages Standard Tag Library) is a component of the Java EE Web application development platform.


1 Answers

It is clear that standard setter/getter is not available for subitem in the class

If you want to check if the property is available for the class you can go for following tweak

using c:catch

<c:catch var="exception">${item.subitem}</c:catch>
<c:if test="${exception==null}">subitemnot available.</c:if>
like image 164
jmj Avatar answered Sep 30 '22 07:09

jmj