JSP + Iteration Output - java

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>

Related

How to check if int variable is not equal to String in jstl?

I have a "size" stored in database its value can be 9, M or null. When I'm extracting the data in jstl using <c:when> condition and comparing if it's not equal to null string I get this error:
Cannot convert null of type class java.lang.String to class java.lang.Long
Code I have used to compare the value:
<c:choose>
<c:when test="${product.productBeanSize ne 'null'}">
<td>${product.productBeanSize}</td>
</c:when>
<c:otherwise>
<td></td>
</c:otherwise>
</c:choose>
It's not clear if "null" is stored as a string in your DB or if it indicates simply a not valorized value.
In the latter case simply check:
<c:if test="${not empty product.productBeanSize}">
<td>${product.productBeanSize}</td>
</c:if>
<c:if test="${empty product.productBeanSize}">
<td></td>
</c:if>
Otherwise (but why you should save the "null" string, simply leave the column not valorized) you can try with <c:catch> to check if the value is numeric.
Example:
<c:set var="size" value="${product.productBeanSize}" />
<c:catch var="isNumber">
<c:set var="size" value="${size * 1}" />
</c:catch>
If isNumber is == null then you have a numeric value, otherwise a string one.

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?

How to show the variable values at the top of the table in jsp?

I am passing an object from servlet to jsp and then I am iterating that object in the jsp and showing the results in a table as shown below. And while iterating the table, I calculate some counts and store it in a variable.
These are my count variable -
kCount
rCount
totalCount
And then I am showing the actual values of these count variables in fieldset as shown below and it works fine.
<div>
<!-- if I try to show the fieldset here, then I see all the values coming as zero. -->
<TABLE>
<TR>
<TH>VALUE1</TH>
<TH>VALUE2</TH>
<TH>VALUE3</TH>
<TH>VALUE4</TH>
</TR>
<c:set var="kCount" value="0" scope="page"/>
<c:set var="rCount" value="0" scope="page"/>
<c:set var="totalCount" value="${CV.getValue().size()}" scope="page"/>
<c:forEach var="i" begin="0" end="${CV.getValue().size() - 1}">
<TR>
<TD>
${CV.getValue().get(i)}
</TD>
<TD>
${CV.getHasR().get(i)}
<c:if test="${CV.getHasR().get(i) == 'True'}">
<c:set var="rCount" value="${rCount + 1}" scope="page"/>
</c:if>
</TD>
<TD>
${CV.getType().get(i)}
<c:if test="${CV.getType().get(i) == 'K'}">
<c:set var="kCount" value="${k + 1}" scope="page"/>
</c:if>
</TD>
</TR>
</c:forEach>
</TABLE>
<!-- I don't want to show the fieldset here, it should be shown at the top of the table -->
<fieldset>
<legend><b>Window</b></legend>
<table>
<tr>
<th>VALUE2</th>
<th>VALUE3</th>
</tr>
<tr>
<td>Total rows:</td>
<td>${totalCount}</td>
</tr>
<tr>
<td>R Count:${rCount}</td>
<td>K count:${kCount}</td>
</tr>
</table>
</fieldset>
</div>
Now If I try to copy this fieldset and try to show at the top of the table instead of showing at the bottom of the table, then I see all the values as zero. My main goal is to show the values of each variables in the fieldset but it should be shown at the top of the table, not at the bottom.
Any thoughts how this can be one if possible at all?
There are at least three options here (I am sure there are many more):
Calculate your totals in the servlet and pass them to the JSP for display
In the JSP, loop through your data first to calculate the totals and display them, then loop again to display the data
In the JSP, loop through and display your data while calculating your totals, then use jQuery or similar to write the totals back into the correct location using javascript.

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 to use if clause inside a netui data repeater

I'm looking for method to inject value into a <c:if test>. The code below works if I substitute
<% out.print(request.getSession().getAttribute("UserName").toString()); %>
with some constants. But how can I parse a value in <%%> tag into a <c:if test>?
<netui-data:repeater dataSource="pageFlow.availableBooks">
<tr>
<td><netui:label value="${container.item.bookName}" /></td>
<td>
<c:if test="${container.item.createdBy == '<% out.print(request.getSession().getAttribute("UserName").toString()); %>'}">
<netui:anchor action="removeBookAction" value="Remove" formSubmit="true">
<netui:parameter name="bookID" value="${container.item.bookID}" />
</netui:anchor>
</c:if>
</td>
</tr>
</netui-data:repeater>
Thanks in advance!
You can use sessionScope to read the value from session scope. The following will help you to resolve:
<c:if test="${container.item.createdBy == sessionScope.UserName}">

Categories