• Post Reply Bookmark Topic Watch Topic
  • New Topic

loops in jstl  RSS feed

 
Dubois Geoffrey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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
cursor [i]. GetQuestion ()
to the original question.

Then
cursor [i] getQuestion. GetLeftnode ()
For yes and
cursor [i]. GetRightnode (). GetQuestion
for no.

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



thank you very much
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if the "cursors" attribute is an array type (as you sort of said it was), then an ordinary c:forEach loop can be used to go through the array.



The other thing you missed is that JSTL strongly prefers that your objects are Java Beans. Then if your object has a "getQuestion()" method, i.e. an ordinary getter, you can treat that as a "question" attribute as I did in the above rewrite.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing to consider.
The code as it is is not really amenable to looping, because you are checking the value of a request parameter to find out what button was just pushed.
If you did want to loop over all questions, and display all the answers, you would need to keep a record of the answers as well.

Also remember JSTL is executed server side. It doesn't dynamically update the page based on user interactions. You have to reload the whole page to do that, or make an AJAX call and update part of the page via javascript.
 
Dubois Geoffrey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for your help. I found the solution. For those who are interested, here is the code that works



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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that by putting things in application scope, you have created threading issues such that two people using your app at the same time will stomp all over each other's data.
 
Dubois Geoffrey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

yes I made ​​a mistake. I changed this



to this



Here, with this change i boot cursor IF it is empty !

it's same result

bye
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!