aspose file tools*
The moose likes JSF and the fly likes Iteration in JSF, how ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Iteration in JSF, how ?" Watch "Iteration in JSF, how ?" New topic
Author

Iteration in JSF, how ?

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
I need to iterate a List in JSF, that is filled with instances of a
POJO class I have implemented, but I have no idea how to do this
iteration, the cast, and then access the getter methods from the JSP
with the JSF/myfaces tags.

Can anybody help?

many thanks
Frank


Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Use h:dataTable to iterate over a collection of row objects in tabular format.
Use t:dataList to iterate over a collection the plain way.
If you´re using Facelets, the ui:repeat does less or more the same as t:dataList.
If you´re using JSTL, you might consider c:forEach which is also similar as t:dataList, but it does its task much less efficiently.
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
Thanks
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You're welcome.
suresh dasari
Ranch Hand

Joined: Oct 05, 2009
Posts: 121

if we use iteration in JSF using ui:repeat tag, how to set user modified values back to backing bean???


Sun Certified Java Programmer with 93 percent
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

ui:repeat isn't a logic operation - it's just what its name implies: a way of repeating copies of UI elements.

Logic operations in JSF views aren't recommended. They blur the lines between Model, View, and Controller.

When you repeat a control using ui:repeat, you have to ensure that each copy has its own unique value reference. That's something that's actually a lot easier to do in a dataTable.


Customer surveys are for companies who didn't pay proper attention to begin with.
suresh dasari
Ranch Hand

Joined: Oct 05, 2009
Posts: 121

hi Tim,

my requiremennt can be done by using datatable too. but the problem I am facing is how to validate the values entered by the user in any control inside the datatable?

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

suresh dasari wrote:hi Tim,

my requiremennt can be done by using datatable too. but the problem I am facing is how to validate the values entered by the user in any control inside the datatable?



Validation in JSF is normally done as a part of the lifecycle that comes before the backing bean is updated. In fact, if validation fails, the bean will not be updated. Whether controls are in a dataTable or not, the same rules apply.

If the data completely validates, the dataTable's DataModel will be updated and the action processor would retrieve the updated items from there.
suresh dasari
Ranch Hand

Joined: Oct 05, 2009
Posts: 121

Tim,

Thanks for your reply, I guess I should explain more clearly about my problem.

here is my code:


I written a listener to dynamically render the image associated with each element in side the ui:repeat
when I am looping all the UIComponents, I cant able to iterate the elements inside the datatable, here ui:repeat is considered as UIComponent and its id is "form:uiRepeatId", but I want to validate the elements inside it.
id's for the elements inside datatable are like "form:uiRepeatId:0:state", "form:uiRepeatId:1:state" and so on.
when I am getting the children of datatable I am getting only 3 elements (output label, inputtext and image) and its id is "form:uiRepeatId:state". so I cant able to retrive all the elements.

I think now its more clear
I tried using datatable too. facing the same problem.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

If you do that as a datatable, you should be able to iterate listOutOfStateDlCollectionDataModel. Or actually, do a "getWrappedData" on listOutOfStateDlCollectionDataModel and iterate that. You should find your values have been updated in the data model.
suresh dasari
Ranch Hand

Joined: Oct 05, 2009
Posts: 121

Tim,

could you please explain more clearly.
I am iterating the UIComponent in my listener class (before Phase) with phaseId = validationPhase.
when I am iterating I will get datatable object which dosent have any getWrappedData method to get user entered values.
instead I can able to see those values in saved HashMap, but I am not able to retrive this hashmap from HtmlDataTableEx object.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

suresh dasari wrote:Tim,

could you please explain more clearly.
I am iterating the UIComponent in my listener class (before Phase) with phaseId = validationPhase.
when I am iterating I will get datatable object which dosent have any getWrappedData method to get user entered values.
instead I can able to see those values in saved HashMap, but I am not able to retrive this hashmap from HtmlDataTableEx object.


That's because you're making a complicated process out of something that was designed to be simple.

One of the biggest mistakes most people make when attempting JSF is to heavily indulge in JSF-specific coding. JSF was designed to do as much work as possible with POJOs. If you're pulling in lots of javax.faces classes other than the model classes, there's a very strong probability that you're not using JSF effectively. When you start mucking around with JSF's internal datastructures, the likelihood you're making too much work for yourself (and raising the cost of app maintenance) goes up even further.

In most cases, the only reason for messing around with JSF's UI objects in Java code is if you intend to dynamically modify the layout of the page. Since mutating page layouts are confusing to users, that's something that should only be done after a lot of careful thought. I've only done it a handful of times myself in the last 5 years and never just to do something that a set of table tags could do for me a lot easier. Certainly not for something as trivial as presenting a collection of input controls and images.

JSF is an implementation of the Model/View/Controller architecture. Normally the view is presented more or less statically via JSF tags, the controller is the FacesServlet plus the JSF validation and converter tags. The model is the backing bean. Typically the backing bean also contains business logic and navigation functions. Not infrequently, it contains listeners that facilitate cases where the view controls interact, such as cascading dropdown lists. Only rarely does it contain more elaborate constructs, since the architecture was designed to "Make simple things simple and complex things possible" (Alan Kay).

In the case of menu and datatable controls, the backing data isn't presented directly to the view. That's because the controller requires some supporting metadata. In the case of the dataTable, it's primarily to provide cursor functionality as the system iterates the rows in the model. The actual data itself is bound to the DataModel object using the setWrappedData() method (implicitly called by some constructors), and the DataModel object itself is presented as a property of the backing bean bound to the dataTable control.

The opposite of setWrappedData is getWrappedData, and that method can be used to get the actual data collection as it appears after the form has been submitted. Wrapping the data collection is completely transparent to the data being wrapped, so you could actually just save a pointer to the collection when you wrap it and reference it directly, as far as it goes.

Unfortunately, there seems to be a lot of old stale documentation on JSF that was created before these much simpler and tidier arrangements were developed. One problem with Internet searches is that often obsolete stuff hangs around at the top of the search results long after it should have faded into the night.
suresh dasari
Ranch Hand

Joined: Oct 05, 2009
Posts: 121

Thanks for educating me.
I cant change the existing structure in my project as it renders the corresponding image when the validation fails.
So is there any way to render my graphicImage when the validation fails for a particular element in side the datatable?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

suresh dasari wrote:Thanks for educating me.
I cant change the existing structure in my project as it renders the corresponding image when the validation fails.
So is there any way to render my graphicImage when the validation fails for a particular element in side the datatable?


Hmmm. Now THAT's a good one. The built-in validators for JSF have been primarily used to display text messaged. However, you can attach images to CSS styles, so try this:

1. Create 2 CSS style classes, let's call one "data_good" and one "data_bad".

2. Code a messages tag to hold the results of the validation:


3. In the style named "data_good", place a URL reference to a green check mark image

4. In the style named "data_bad", place a URL reference to a red "X" image.

Embellish as desired. You can usually override the validation error message for an input control, although since this isn't one of the ones I'm familiar with, I can't guarantee it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Iteration in JSF, how ?