• 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

For loop

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there. I have a little question about loops in JSF.
Is there any possibility how to create for loop in JSP using JSF? Something like c:foreach?

My concrete case is that I have List<String> and Im trying to find some mechanism what can help me do the page more transparent, not a lot of h:column tags.
I tried using classic <% loops but didnt work. There is a little part of JSP


also not working


Is there any easy solution how to do loops?
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That can with Tomahawk's t:dataList, or Facelets' ui:repeat, or Ajax4jsf's a4j:repeat.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should add, you should never ever use scriptlets when you're already using taglibs and EL. It's a bad design and receipt for trouble.
 
Zdenek Obst
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote:I should add, you should never ever use scriptlets when you're already using taglibs and EL. It's a bad design and receipt for trouble.



ok, thanks. So there is no any possibility for loop in standart JSF (jsf/core&amp;html).. ?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is the h:dataTable, but that renders a table.
The JSTL c:forEach is applicable in certain circumstances, but the aforementioned taglibs are far more efficient and recommended.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To amplify Bauke's admonition: DON'T code logic in the View!!! Not scriptlets, not logic JSP tags (à la JSTL), not nothing!

JSF is designed to be as close as possible to a "pure" MVC architecture. In MVC, the Model contains data, the View manages presentation and the Controller determines how the Model and View communicate.

Because of this clear separation of concerns, a programmer should never have to hunt between files for what does what. If it's display logic, it's in the Controller, if it's data, it's in the Model. If it's business logic, it's on back-end side of the Model.

When you don't have clear rules for where things are located - whether it's logic on Views or stored procedures in databases doing things best done in application logic, you increase the cost of creating and maintaining the app in 2 ways: 1, finding out where something's done turns into a Treasure Hunt instead of a straightforward "Go Here". Secondly, when funcitonality is smeared all over the landscape, unexpected side-effects result. Small and centralized is easier to manage that broad and diffuse.

There is one type of logic that's unavoidable in JSF, and that's the render/don't render logic and its close relative, AJAX. That's because the view definition is normally static, and you need some sort of view-side indicator if you want to show, hide, update content in various sub-views. However looping, conditional behavior relating to business rather than rendering, stuff like that should not be in the JSF View page - it belongs in upstream layers.
 
Zdenek Obst
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:To amplify Bauke's admonition: DON'T code logic in the View!!! Not scriptlets, not logic JSP tags (à la JSTL), not nothing!

JSF is designed to be as close as possible to ... JSF View page - it belongs in upstream layers.



Thanks Tim a lot... It seems more comprehensible for me. But anyway, what is the right solution?
For example I have in model two dimensional (e.g. 20x30) matrix full of numbers 0-5 and I want table output to jsf page. And I need mechanism for setting CSS class for every td tag corresponding to data from model(matrix).

The most easiest way is to do <c:foreach ... >, but you mentioned that using JSTL is bad. So how? Could you show up some example code how to do this?

E.g. some data in model

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Choose one of the components which I mentioned in my first reply.
 
Zdenek Obst
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote:Choose one of the components which I mentioned in my first reply.[/quote

i dont know them but its not what i wanted. Im trying to understand how to do this clearly with MVC standarts. Could you show me code of controller and then how it will be on page? If loops are not recommended to do?

 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't usually write Controller code in JSF. The Controller logic is implemented by the JSF tag libraries. So generally, all you need is the Model (backing bean(s)) and View (JSF page).

If you want to show a 2-dimensional table, the core JSF control that supports that is the HTML dataTable tag. It references a javax.faces.DataModel object in the backing bean, which provides the row and column data. You can't feed an actual matrix object to this tag because extra support data is required to assist it in its iterations. That extra data is supplied by the DataModel object, and the DataModel wraps the actual row/column data.

The DataTable supports CSS for columns and rows (including the ability to alternate styles for a "ledger-line" appearance), plus whatever individual style information you supply to the elements within a table cell.

Unlike HTML tables, there's no explicit demarcation of cells (td tag). Each new renderable object is in the row template is considered to occupy one cell. You can override that using the columnGroup tag to wrap multiple items into a single cell. Unfortunately, the reverse (rowspan/colspan) isn't supported by the core dataTable, although some third-party JSF tagsets offer span-like features.
 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you to all!

This info help a lot, specially <t:dataList>
 
reply
    Bookmark Topic Watch Topic
  • New Topic