JSTL NumberFormatException - java

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>

Related

JSP if statement - String to number

I am new to this so be gentle. I am working on a JSP page and would like implement an if statement.
command.total is set previously and I can't change its type.
<c:set var="number1" value="${5}"/>
<c:set var="number2" value="${0}"/>
<c:set var="number3" value="${command.total}"/>
<c:out value="${number1}" />
<c:out value="${command.total}" />
<c:choose>
<c:when test="${number1 < number2}">
${"number1 is less than number2"}
</c:when>
<c:when test="${number1 > number3}">
${"aaaaaaaaaaaaaaaaaaaa"}
</c:when>
<c:otherwise>
${"cccccccccccccccccccccccccccccccccc"}
</c:otherwise>
</c:choose>
This always fails and I believe that the problem is that command.total is returned as a string.
I have tried to convert the string with the following:
<fmt:parseNumber value="command.total" var="test" integerOnly="TRUE" type="NUMBER"></fmt:parseNumber>
<c:out value="${test}"></c:out>
Is it possible to convert this string to a number?

JSP + Iteration Output

I'm trying to iterate through a list of Birthdate objects. Inside this object there are 2 variables. A Date object for the birthdate itself and a String for a partially masked date (ie. --/--/2015)
The When portion is working fine and displaying the date properly but when testing out the Otherwise portion I get the following error:
java.lang.IllegalArgumentException: Cannot convert --/--/1956 of type class java.lang.String to class java.util.Date
Am I just missing something totally obvious here? From the examples I've found this should be possible. Below is my code:
<c:choose>
<c:when test="${person.showBirthDates}">
<c:forEach var="bdate" items="${person.birthdates}" varStatus="status">
<fmt:formatDate value="${bdate.birthdate}" pattern="MM/dd/yyyy"/>
</td> </tr>
<tr><td></td><td>
</c:forEach>
</c:when>
<c:otherwise>
<c:forEach var="bdate" items="${person.birthdates}" varStatus="status">
<c:out value= "${bdate.birthdateMask}" />
</td> </tr>
<tr><td></td><td>
</c:forEach>
</c:otherwise>
</c:choose>

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

How to compare two variables in jsp

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>

How to make an option selected from a dropdown list in jsp?

In my project. I want to populate the drop down list on a jsp from a database.
<select id="names" name="names"> <c:forEach items="${names}" var="names">
<option><c:out value="${names}"/></option>
</c:forEach>
</select>
The ${names} is a list of names from the database. I want to select an option dynamically in the drop down list. Suppose there are three names in the database Rohan, Dean, Justin. If Dean is logged, i want select the option Dean as selected.
I try a code like this but this does not work.
<option value="${names}" ${names == names ? 'selected' : ''}>${names}</option>
Try like this assuming that loggedInUser variable holds the String value of the currently logged in user.
<select id="names" name="names">
<c:forEach items="${names}" var="names">
<c:when test="${loggedInUser eq names}">
<option value ="<c:out value="${names}"/>" selected="selected">${names}</option>
</c:when>
<c:otherwise>
<option><c:out value="${names}"/></option>
</c:otherwise>
</c:forEach>

Categories