How to compare two variables in jsp - java

I am using the following code to compare two variables, but it seems it has an error as the page does not get loaded at all.
Based on this answer my code should be correct but surprisingly does not work.
If I remove the <c:when> line it shows
the values of ${name} and ${myvar.employee.name}.
<c:forEach var="myvar" items="${user.names}">
${name}
${myvar.employee.name}
<c:when test="${name eq myvar.employee.name}">
hello
</c:when>

The structure with <c:when> requires <c:choose>
<c:choose>
<c:when test="${your condition here}">
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
otherwise you will get error.

As Alex has pointed out, you cannot have non-wrapped <c:when> in your code.
But it looks like, in your case, <c:choose> is not required, because you have only a single clause. In this case <c:if> may be a better option:
<c:if test="${name eq myvar.employee.name}">
hello
</c:if>

Related

loops binary tree in jstl

I create a binary tree and now I would like to display the result in my jsp with conditions.
Each node has 4 elements (id, question, answer, and leftnode rightnode)
If the user clicks "Yes", the program goes to the left of the tree and if he answers "no" to the right of the tree.
For example, the initial question is "you're a man?" If he answers "yes" we will go to the left and page view "you're a singer?" If he answers "no" page displays "you're French? ".
In java, it would to the original question.
cursor [i]. GetQuestion ()
Then For yes
cursor [i] getQuestion. GetLeftnode ()
and for no
cursor [i]. GetRightnode (). GetQuestion
Until the, everything works normally, but when I want to loop until there is no longer any issue by
<c:when test="${not empty cursor[i].getQuestion() }">
the program stops at the first loop and stops it that cursor is not empty
here is my complete code jstl
<c:choose>
<c:when test="${not empty cursor[i].getQuestion() }">
<c:if test="${param.btn eq 'Oui'}" var="oui">
<c:set var="cursor" value="${cursor[i].getLeftnode() }" scope="session"></c:set>
<c:set var="i" value="${i+1 }" scope="session"></c:set>
</c:if>
<c:if test="${param.btn eq 'Non'}" var="non">
<c:set var="cursor" value="${cursor[i].getRightnode() }" scope="session"></c:set>
<c:set var="i" value="${i+1 }" scope="session"></c:set>
</c:if>
</c:when>
</c:choose>
<c:out value="${cursor.getQuestion() }"></c:out>
thank you
Rather than using methods like ${cursor.getQuestion() } you have to use property names like this: ${cursor.question }
P.S. And make sure you have a proper getter names (get should be lower case)
Thank you for your help.
I found the solution. For those who are interested, here is the code that works
<c:set var="cursor" value="${nodes }" scope="application"></c:set> <form method="get">
<c:choose>
<c:when test="${not empty cursor.getQuestion() }">
<c:if test="${param.btn eq 'yes'}" var="yes">
<c:set var="cursor" value="${cursor.getLeftnode() }" scope="session"></c:set>
</c:if>
<c:if test="${param.btn eq 'no'}" var="no">
<c:set var="cursor" value="${cursor.getRightnode() }" scope="session"></c:set>
</c:if>
</c:when>
</c:choose>
<c:out value="${cursor.getQuestion() }"></c:out>
The problem was scope. At each loop, with "session" scoped my variable was reset each time. By "application" in scope, it is initialized only once and I can get what I want
Thank

XSLT <xsl:element> equivalent in JSP?

When writing XML-compliant JSP, it is difficult to generate different HTML tags according to the input (e.g. when outputting the different tags in a <table>).
The standard solution is to use if or choose tags, but sharing code inside the HTML tag becomes a problem. E.g.
<c:choose>
<c:when test="${i.tag = 'th'}><th> <!-- 100 lines of code --> </th></c:when>
<c:when test="${i.tag = 'td'}><td> <!-- 100 lines of code duplicated?! --> </td></c:when>
</c:choose>
XSLT offers an <xsl:element> tag which allows you to build a tag and its attributes with standard XML syntax. Is there such a tag in any tag library in JSP?
Shouldn't you be doing this like?
<c:choose>
<c:when test="${i.tag = 'th'}"><th></c:when>
<c:when test="${i.tag = 'td'}"><td></c:when>
</c:choose>
<!-- 100 lines of code -->
<c:choose>
<c:when test="${i.tag = 'th'}"></th></c:when>
<c:when test="${i.tag = 'td'}"></td></c:when>
</c:choose>
If you want your markup to be valid XML; make use of a custom tag that wraps all the Java code you want to avoid duplicating. Your markup would then look something like
<c:choose>
<c:when test="${i.tag = 'th'}">
<th>
<my:customTag anyAttributes="th-related-values-if-any" ... />
</th>
</c:when>
<c:when test="${i.tag = 'td'}">
<td>
<my:customTag anyAttributes="td-related-values-if-any" ... />
</td>
</c:when>
</c:choose>
Alternatively, the following hack (suggested by #Uooo) can be used to pass XML validators.
<c:choose>
<c:when test="${i.tag = 'th'}"><c:out value="<th&gt%" /></c:when>
<c:when test="${i.tag = 'td'}"><c:out value="<td&gt%" /></c:when>
</c:choose>
<!-- 100 lines of code -->
<c:choose>
<c:when test="${i.tag = 'th'}"><c:out value="</th&gt%" /></c:when>
<c:when test="${i.tag = 'td'}"><c:out value="</td&gt%" /></c:when>
</c:choose>
You could create a seperate JSP containing your "100 lines of code" and include it.
Replace your <!-- 100 lines of code --> with:
<jsp:include page="hundredLines.jsp">
<jsp:param name="beanParam" value="beanValue"/>
</jsp:include>
And in your hundredLines.jsp:
Bean can be used like ${beanParam}
No. JSP requires knowing the tags when it builds its trees. That makes the operation of JSP more robust
However, you can address the duplicated code by factoring it into a separate JSP file and including it, depending on the implementation of JSP you are using.
Alternatively, you can create a JSP tag that holds the 100 lines of code. It is probably not worth while to do this.
Finally, you could, instead of using th and td, use styles on td to make td look like th. That would make the element look like
<td style="${i.tag == 'th' ? '' : 'font-style: bold; text-align: center;'}">
100 lines of code
</td>
I know I don't have the style quite right, but you could use styleClass instead and play with it in your CSS.

with JSTL how can I determine which variable is null before a forEach

I have two lists. only one can be not null at a time.
I want to iterate over the one that's not null but I don't want to use <c:choose> because I would have to repeat the code in <c:when> and <c:otherwise>
how can I do something like
if list1 is not null then iterate over list 1 otherwise list2
<c:forEach items="${list1}" var="staffMember">
>html here that I don't want to repeat in my source code...<
Use the ternary operator to access the right list within <c:forEach>:
<c:forEach items="${(empty list1) ? list2 : list1}" var="staffMember">
....
</c:forEach>
Of course, it implies that both list containt instances of the same type.
If you don't want to use then you can try .
And check in that for empty. some thing like this..
<c:if test="{not empty testList1}"> Execute First</c:if>
<c:if test="{not empty testList2}"> Execute Second</c:if>
Hope it helps.
try this:
<c:choose>
<c:when test="${not empty testList1}">
<c:set var="TargetList" value="${testList1}"/>
</c:when>
<c:otherwise>
<c:set var="TargetList" value="$testList2}"/>
</c:choose>
<c:forEach items="${TargetList}" var="staffMember">
...
</c:forEach>

JSTL NumberFormatException

I have a spring+hibernate application where I make a native query in my DAO layer. The query looks like the following
select name, amount from myTable where id=:id
It is clear that the selected fields are of different data types (String, Number).
In the JSP I want to print the returned result in table so I am using foreach loop to go through each record in the returned set.
I want to put the negative numbers between braces so I am using the following code
<c:forEach var="item" items="${resultSet}">
<tr>
<c:forEach var="v" items="${item}" varStatus="st">
<td>
<c:choose>
<c:when test="${v != null}">
<c:choose>
<c:when test="${v<0}">
<c:out value="(${v})"></c:out>
</c:when>
<c:otherwise>
<c:out value="${v}"></c:out>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<c:out value="-"></c:out>
</c:otherwise>
</c:choose>
</td>
</c:forEach>
</tr>
</c:forEach>
As the first item in the query is string, this code fires NumberFormateException.
I know two solutions for this problem. The first is put the braces in the SQL query but I can not use this solution as the application contains many queries and it will take a lot of time modifying all the queries.
The second solution is to use resultTransformer and convert the returned data into one Object but this is not suitable for the same previous reason.
Is there any workaround to solve this problem ?
Have you tried like this ??
<c:when test=${v<0}>
<c:out value="(${v})"></c:out>
</c:when>
<c:otherwise>
<c:out value="${v}"></c:out>

How can I make JSTL give an error when referencing an undefined attribute

Inside a jsp page, I would like JSTL to behave strictly when referencing an undefined variable.
Example:
The servlet passes:
request.setAttribute("firstName", "hello");
request.setAttribute("lastName", "there");
The jsp page:
${firstName} ${middleName} ${lastName}
I would like JSTL to give me an error that middleName is undefined instead of silently ignoring it.
<c:if test="${empty middleName}">
<c:out value="Middle name is empty"/>
</c:if>
OR
<c:choose>
<c:when test="${empty middleName}">
<c:out value="Middle name is empty"/>
</c:when>
<c:otherwise>
<c:out value="Middle name is NOT empty"/>
</c:otherwise>
</c:choose>

Categories