File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes How to reference a Map<String, List<String>> via ValueExpression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to reference a Map<String, List<String>> via ValueExpression" Watch "How to reference a Map<String, List<String>> via ValueExpression" New topic
Author

How to reference a Map<String, List<String>> via ValueExpression

Daniel Barcellos
Greenhorn

Joined: Dec 18, 2012
Posts: 11
Hey guys,

Basically I need to get referenced a value expression based on a Map<String, List<String>>. I won't get in deeper in why I need that but just that we need this complex data structure here.

Well, I have a <ui:repeat> that iterates over a List then each object builds a <p:inputText that maked reference to this "complex" Map. See bellow:



Note that I'm doing a #{searchBean.searchParameterValues[p.sigla]}" so this not getting success yet.

So my question is How can I reference a Map<String, List<String>> using ValueExpression?

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

You really shouldn't use ui:repeat when you're creating a tablular display. The h:dataTable is much better suited. Plus it avoids the sin of writing code on the View Template.


Customer surveys are for companies who didn't pay proper attention to begin with.
Daniel Barcellos
Greenhorn

Joined: Dec 18, 2012
Posts: 11
Tim Holloway wrote:You really shouldn't use ui:repeat when you're creating a tablular display. The h:dataTable is much better suited. Plus it avoids the sin of writing code on the View Template.


Hey thanks for you answer. But I'm not sure how that could help me and I'm not sure what do you mean of writing code on the View Template. I'm not coding there... I guess... hehe

Did you have any example on how to do that?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

"repeat" isn't a logical (programming) statement?

As far as it goes the model support for a dataTable is somewhat cleaner than that of a ui:repeat - not to mention the View Definition.
Daniel Barcellos
Greenhorn

Joined: Dec 18, 2012
Posts: 11
Tim Holloway wrote:"repeat" isn't a logical (programming) statement?

As far as it goes the model support for a dataTable is somewhat cleaner than that of a ui:repeat - not to mention the View Definition.


so i guess i'll have to refactoring interely my view right? because in that example I'm just "repeating" the <tr> tag's so that can be possible to partial update my table...

However, answering my question, did you have any example on how can I achieve that i mean how can I make a reference to a Map<String, List<String>> by using expression language?

I noticed that to reference a Map<String, String> for example you just code this #{bean.map['key']} and everything is all right.

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

Well, I'm just a nasty old who tends to knee-jerk react to JSF View definitions loaded with gratuitous programming-type constructs and raw HTML. Although, admittedly after all these years, I have a few scars to justify why I make these particular recommendations, not solely being pompous and arrogant (pompous and arrogant come free at no additional cost). Plus, stuff like that tends to indicate that someone's new to JSF and trying to forcibly conform it to their more traditional concepts, so it's often a tip-off that more insidious atrocities are present as well.

Anyway, consider what you are trying to accomplish. HTML and HTML requests are based on plain text. VERY plain text, in fact, since the original Internet ran as often on EBCDIC machines (IBM mainframes) as it did on ASCII machines, so only 7-bit basic characters could safely be used due to code page translation concerns.

A List is a binary object. Even if it's a List of String or character objects, the List itself is binary. HTML isn't binary. That means that rendering or accepting List objects requires appropriate conversion from/to binary to text. The HTML processors on the average web client don't have a clue, so any translation would have to be done server-side.

JSF has Converters for that, but there's another consideration. A List is a collection of objects. The HTML INPUT control that is the result of rendering the JSF h:inputText element is a scalar (single-value) control. How is it supposed to handle a collection
Daniel Barcellos
Greenhorn

Joined: Dec 18, 2012
Posts: 11
Tim Holloway wrote:
JSF has Converters for that, but there's another consideration. A List is a collection of objects. The HTML INPUT control that is the result of rendering the JSF h:inputText element is a scalar (single-value) control. How is it supposed to handle a collection


Well your are pretty rright in saying that "there's someone new on JSF trying to do weird stuff"! I loved that! lol However this not answer my question at least gave me some light over this all.

I refactored my code so now I'm not using List's anymore. Now I did this Map<String,ValueContent> and now I want to reference this value map at my <p:inputText />

Then I try this:



And I got this:



Then I tried this out:



And once again it's thrown my that:



Thanks,

Daniel
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

Well, at least you're beginning to see why I caution against using complex expressions in EL. They're absolute murder to debug. As a general rule, you're better off both in terms of debugging ability and in abstraction if you put that kind of stuff in the Model, not in the View.

One of the top 10 mistakes that newbies make with JSF is in thinking that the sub-model that backs a complex sub-view (such as a dataTable) must be an absolute business or persistence (Domain Model) object. Actually, the JSF sub-model frequently works better if you define a distinct UI model which can act as a façade or decorator to a business or domain model object. Remember, Lists aren't discrete sets of objects, they're containers for object references, so the overhead of fronting a List (or Map) with a more UI-friendly List is not that high. And if you can make the properties in your façade model handle the Map references themselves, so much the better.

JSF2 has a lot to answer for, I'm afraid. It made certain things possible that JSF1 didn't but that newbies abuse. One of them is being able to use a dataTable to reference a List or Array directly. Sounds convenient. Who needs that nasty old DataModel anyway?

Well, actually, you do. Even if you don't construct and supply a DataModel for a dataTable, JSF will construct one, because the DataModel decorates the actual data with important stuff used to manage the dataTable - which is why Request-scope backing beans for tables fail. The problem with this automatically-constructed DataModel is that there are useful methods on that model, such as the one that determines what row you clicked on when you submit data back from the table. The automatically-constructed DataModel isn't easy to get at compared to one that you construct yourself.
Daniel Barcellos
Greenhorn

Joined: Dec 18, 2012
Posts: 11
Hi, see bellow a few things that I want to discuss with you if you possible.

Tim Holloway wrote:And if you can make the properties in your façade model handle the Map references themselves, so much the better.


In that you are trying to advise me that if I make for each inputText an attribute in my backin' bean would be less murder to debug, right? Isn't a good solution or worst a an ugly approach? Did you mention the fact that I could use an UI Data Model pattern but i'm afraid to say that I'm not using DataTable to iterate over. I'm using <ui:repeat /> to create <tr>'s of my <table> via <p:ajax /> requests and it's working pretty well by the way. The problem is that I need to get those references sent through <p:inputText /> back to my managed bean and I was wondering that this could be achieved by using, now, a Map<String,ValueContent>.

Can you help me?

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

"Working pretty well" and the amount of grief you're experiencing don't seem to quite go together.



Maybe I'm biased, but I think this is a whole lot more elegant. You had 2 apparently identical inputText controls on the same row, though. I didn't know what to make of that.

The DataModel object would wrap a List of "p" objects, and the p set/getInitValue methods could front for their corresponding Map entries.

And BTW, a ValueExpression is also a binary object, so the same issues apply as when it was a List.
Daniel Barcellos
Greenhorn

Joined: Dec 18, 2012
Posts: 11
Hi Tim,

I Followed your tips and I solved that out by using a simple Converter. Then I could make references on the <p:inputText /> by using that weird complex expression language. So I guess I learned a lot with this first post and I hope I can count on you with some others "murder to debug" questions! lol

My Best regards to you, friend!

Daniel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to reference a Map<String, List<String>> via ValueExpression