JSP if statement - String to number - java

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?

Related

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>

How to read a variable before perform <c:choose> operation in JSP?

I can read my variable using getRating() method. When I print it using
<c:out value="${obj.getRating()}" />
it prints the value. But what I need is to display an image according to the rating as below.
<c:choose>
<c:when test="#{obj.getRating() eq 5 }"><img src="images/5star.png" /></c:when>
<c:when test="#{obj.getRating() eq 4 }"><img src="images/4star.png" /></c:when>
<c:when test="#{obj.getRating() eq 3 }"><img src="images/3star.png" /></c:when>
<c:when test="#{obj.getRating() eq 2 }"><img src="images/2star.png" /></c:when>
<c:otherwise> <img src="images/1star.png" /></c:otherwise>
</c:choose>
Then It gives syntax error. How can I do that? Do I need to read the variable and store in somewhere before go to ? please help.
It should be
<c:when test="${obj.getRating() eq 5 }">
instead of
<c:when test="#{obj.getRating() eq 5 }">
use $ instead of #

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 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>

Assigning outcome of another JSTL tag as value of one JSTL tag

I've got this, which is working:
<c:choose>
<c:when test="${sometest}">
Hello, world!
</c:when>
<c:otherwise>
<fmt:message key="${page.title}" />
</c:otherwise>
</c:choose>
And I want to change it to this:
<c:choose>
<c:when test="${sometest}">
<c:set var="somevar" scope="page" value="Hello, world!"/>
</c:when>
<c:otherwise>
<c:set var="somevar" scope="page" value="<fmt:message key="${page.title}">"
</c:otherwise>
</c:choose
But of course the following line ain't correct:
<c:set var="somevar" scope="page" value="<fmt:message key="${page.title}">"
How can I assign to the somevar variable the string resulting from a call to fmt:message?
The fmt:message has a var attribute as well which does effectively what you want.
<fmt:message key="${page.title}" var="somevar" />
That's all. Bookmark the JSTL tlddoc, it may come in handy.
It is also possible to specify the value to set using the contents of the body, rather than through the value attribute:
<c:set var="somevar" scope="page">
<fmt:message key="${page.title}"/>
</c:set>
You'll have to do with:
<c:set var="title"><fmt:message key="${page.title}"></c:set>
<c:set var="somevar" scope="page" value="${title}" />
Since you can't use <fmt:message .. /> on that spot is my experience, has to do with nesting like you suggested. Or go with #balusc suggestion ;-)

Categories