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
Related
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?
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>
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>
I want to display dropdown on the basis of if condition,My code snippet is as below
<%if(userType.equalsIgnoreCase("O")) {String user=(String)session.getAttribute("executorname"); %>
<html:select style="width:190;" value="<%=user%>" property="jobOwner">
<html:option value="<%=user %>"><%=user %></html:option>
</html:select>
<%} %>
I get org.apache.jasper.JasperException: /jsp/frmQueryExecutor.jsp(73,23) quote symbol expected
I want the value os session variable in dropdown.
Try it this way,
<c:choose>
<c:when test="${not empty sessionScope.executorname}"> //if loop
<html:select style="width:190;" value="<%=user%>" property="jobOwner">
<html:option value="${sessionScope.executorname}"><${sessionScope.executorname}></html:option>
</html:select>
</c:when>
<c:otherwise> // else loop
//do whatever
</c:otherwise>
</c:choose>
and try to avoid scriplets all together.
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 ;-)