• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Breaking forEach loop using jstl tag

 
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am currently using "forEach" loop of jstl to iterate through the collection(ArrayList). If I find a particular value, I want this "forEach" loop to stop iterating. I will then set the "value" to a "var" and display the content.
I have tried with "if" condition of jstl inside forEach loop, but problem is if the ArrayList has duplicate then the loop will not "break". Is there any alternative to break in jstl?
PS: I am writing the above file in .jsp format. Appreciate your answers.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I would suggest doing that logic in the controller servlet instead. Have the servlet go through the collection, find the particular value, and send that to the JSP as a request attribute.
 
Nizamuddin Shaikh
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul, I have already written set the parameter in a java file(Servlet) and retrieving the parameter in a jsp file, problem is forEach loop is not breaking after finding the particular value.
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Paul is saying is don't pass the whole list to the JSP.
Have the servlet pick out the required value and send that to the JSP.
 
Nizamuddin Shaikh
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working in a scenario where Servlet will give entire collection and jsp will check the collection and display the value.
FYI I am working in ATG project where there is a product which has multiple SKUs, I need to choose an ACTIVE SKU(only 1 will be active at a time) among the multiple SKUs and send it for display purpose
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And that selection of an active SKU should not be done on the JSP.

If the JSP should only display the active one then it should be supplied with only the active one.

Indeed, assuming that the List comes from a database (bit of an assumption there) then the query that provides the list should be written to only supply the thing(s) you are interested in.
 
Nizamuddin Shaikh
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is "forEach" loop in jstl and for every "loop" there should be a break condition,correct?(I always learnt that there is a "break" for every loop)
So I want to break the iteration upon finding the required condition.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JSTL tag is for iterating over a collection.
It's not intended to search through a collection.

 
Nizamuddin Shaikh
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't we just break a loop? It could be done? If not now, then perhaps someone can take the pain to update jstl with extra functionality.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why?
That is not the intention of JSTL.
It's a set of tags to help with displaying data.
It was not intended as a set of tags for filtering data as it was expected that that job had already been done by the servlet, which is why you have been pointed to filtering your data in the servlet.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not be surprised when the wrong tool for a job doesn't work well for a job for which it is not intended.

 
Nizamuddin Shaikh
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please suggest me right tools to filter my collection data within "jsp"?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A servlet.
Seriously.
Why do you have to do it in a JSP?
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your use case is "iterate through a list looking for a specific value"

Just as a suggestion: if you convert that list to a Map keyed on the thing you are looking up on (SKU), then you could get rid of the iteration all together.

 
Nizamuddin Shaikh
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot all :) I appreciate your answers. I had to make custom tags to fetch my needs.
 
Saloon Keeper
Posts: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And in doing so, ignored all the advice. Tags are for viewing data. Not processing.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you used duct tape and chicken wire to get it to "work".
 
Ranch Hand
Posts: 99
1
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shaikh Nizamuddin wrote:Thanks a lot all :) I appreciate your answers. I had to make custom tags to fetch my needs.



Why ask at all if you are going to ignore all the good advice and do it the wrong way?
 
Nizamuddin Shaikh
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question was to break forEach loop in a jsp(only JSP not Servlet). I don't want to mess with the integrity of the coding that has been done earlier in the Servlet, that's why I asked breaking loop in jsp(once again not Servlet). I haven't ignored any advice given by you. I was looking for the possibilities that taglibs can do.
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shaikh Nizamuddin wrote:My question was to break forEach loop in a jsp(only JSP not Servlet). I don't want to mess with the integrity of the coding that has been done earlier in the Servlet, that's why I asked breaking loop in jsp(once again not Servlet). I haven't ignored any advice given by you. I was looking for the possibilities that taglibs can do.


You are again getting it all wrong my friend.
Everyone who has advised previously has understood your question clearly.
And everyone who has advised previously has suggested the correct way of doing things in a Java web application.
Because programming IMHO is not getting a solution, but is getting a solution in a correct way.
You are not learning the correct way to do things and making things more complicated for yourself and for the people who will happen to work on that code in future.

And this forum is not only for giving a solution to your problem. Here people see a bigger picture and provide help which will be helpful in the long run.
People who has given advice previously few of them are programming since the time I was not even born...so listen to them and try to use their knowledge. :)
 
Nizamuddin Shaikh
Ranch Hand
Posts: 35
1
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can't say every senior Devs, who might have more than a decade experience are always right. They may not be aware of their tools capability to the fullest extent. Everyone refrains from using the tools which doesn't have their required functionality, instead they look for something else and come up with a solution(which might have been integrated to original tool) and name it as new tool. I am not asking what is already present in the book. I am asking what else can be done further. You are beating the bush around and approaching same solution which is already mentioned in other forums. I was expecting a solution like "making custom tags to break forEach loop". It would have been really insulting to the ones who couldn't provide this solution. If I am not wrong, everyone googled what I have asked and found the same solution and posted to me.
 
Stephan van Hulst
Saloon Keeper
Posts: 15491
363
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a difference between what a tool can do, and what it should be used for. Take a look again at the image Bear posted. This is exactly what you're doing.

It's not that you can't solve this problem with JSTL tags. It's that you SHOULDN'T, because you're creating a more unmaintainable situation for the next programmer.

I didn't Google your question, because from experience I know how you should solve this problem. I'm pretty confident that most of the people who responded to your question didn't Google it either.

We're all telling you, from experience, that this is a BAD IDEA. I'm pretty sure that you found the same answer at other forums, because the people there think this is a BAD IDEA.

That our solution isn't what you want, doesn't mean we are not familiar with the tools we use.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shaikh Nizamuddin wrote:If I am not wrong, everyone googled what I have asked and found the same solution and posted to me.


You are wrong. The combined experience of people who have tried to help you in this topic is staggering. No one "googled" to give you an answer; the answers are born out of the experience of having been there and done that, and having the scars to show for it.

If you were looking for some answer on how to do thing by "cleverly" subverting tools to do things a certain way, rather than the way that makes the most sense, you have certainly posted in the wrong place.
 
Greenhorn
Posts: 16
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that this post has been a few weeks old. Perhaps the OP might not consider reading any more replies. Wanted add a voice, in case either the OP or anyone else stumble upon this post. Wanted to highlight some fundamentals that might justify answers to use Servlet and not JSP.

There are somethings known as design patterns in software development industry. These are known to be proven solutions for known problem patterns. These advice on how to neatly isolate functionality alongside collaborative communication towards the bigger goal of the problem. One of these patterns is Model-View-Controller also known as MVC pattern.

It was olden days when developers had only a choice of core programming languages to choose from to develop software solutions. But that is not the case anymore. Technologies like EJBs, Servlets, JSP, etc. have been built with a purpose. One of the main purposes is to provide a framework to implement well known design patterns with ease. The Servlets-JSP basically provides a framework to develop solution in MVC pattern, by Servlets taking the role of Controllers and JSPs taking the role of View. Why? it makes sense for logic to reside in a Java source file (a Servlet) and View (generally HTML) to reside in JSP, a tag-based syntax file. JSTL has been added to facilitate access to dynamic state (Model of MVC) or in simple words "data", that needs to be presented in view while keeping the tag-based syntax.

It is great that the frameworks (like JSTL) are extensible and allows to develop custom functionality. But the intention of this provision is never to break the fundamental purposes. The consequences of breaking the fundamentals are the loss of the good abilities (Maintainability, Testability, etc.) the software should incorporate.

Having said that, it all depends on how much one (could be a developer, a team, a company) is concerned about all these fundamentals of software development.
reply
    Bookmark Topic Watch Topic
  • New Topic