• 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

A more dynamic way of accessing property values? (Like a "property bag"?)

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to do something which I think should be simple, but I am probably going about it the wrong way.

Imagine I have an XML file that contains name-value pairs (in reality it could be a database or naything else I plug into the back-end).

And then I have some page that gets those values from the backing bean, maybe using them as defaults

That's fairly trivial and at some point in the future I can add another "h:inputText" to use "bar", but I would need to change the backing class and that is something I'd like to avoid. I could use EL 2.2 and have a parameter, but based on Tim's post I fear I might be breaking the whole MVC concept and I don't want to do that either! Also, I don't think it would work when the user makes changes to the values, it would keep getting the defaults from the XML file as it has no way of accessing the setter.

I was thinking of updating a hidden "h:inputText" or something to hold key/value pairs; but that struck me as very messy and still left me with the problem of reading the value back at render time.
So is there a way to do what I need? To call a single "property" and get/set values for "foo" and "bar" as appropriate (a bit like a "property bucket" or "property bag"), without having to modify the backing bean for each new control that gets added?

Thanks in advance.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen attempts to make JSF use non-determinate views and backing beans and they're usually not very pretty. You'd generally be better off chucking JSF entirely if that's the way you want to go, since you're losing the good with the bad. And JSF was designed to make MVC simple and obvious, not to underpin a "one-view/bean-fits-all" approach. Also one of the disadvantages of using a single view, single bean approach is that you can produce some really ugly web pages that way, since you don't have direct human control over where things display on the page and how.

That's not to say that there aren't times when a little flexibility isn't good. I've got a database table editor written in JSF where the list and detail pages are dynamically created based on the schema of the table being edited. For that app, the prototype View contains an anchor element and the backing bean uses that element as a basis for dynamically adding child controls.

You can also split the difference. If only some items in the backing bean are changeable, you can put them in a mapped object and use an EL expression that uses mapped data references such as "#{myBean.dataMap[mapKey]}".
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer my own question....use a value change listener with a dummy "set" method.

And then the bean...

When the form gets submitted, the value change listener will set the new value in the private map and this value will be read back in the getter. The setter is not actually used, but is still required by the framework. I would rather not use a value change listener for clarity in the page, but I was having issues getting the current component and this workaround seems to be OK.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:You can also split the difference. If only some items in the backing bean are changeable, you can put them in a mapped object and use an EL expression that uses mapped data references such as "#{myBean.dataMap[mapKey]}".


Thanks Tim! You replied just as I was editing my own post. I'll look into using a map approach and see if it fits my needs.
I am not trying to do "one bean to bind them all" (or, at least, I don't think I am) it's just that a few of the use cases are a bit more dynamic than others.
Your solution of adding child controls dynamically may be something else I'll need to look at as it seems to be in a similar area to what I am investigating.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having another period of frustration. Reading from the data map is easy enough, but how do you set a value on it?

And in the backing bean

I've been Googling my little fingers off, but I cannot find where this is documented. Is it even possible?
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're doing too much work! As long as you have the "get" method for the map, the actual map entry get/set functions are provided automatically by EL. No need to write a "set" method for map entries.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I was first trying it I was getting a NullPointerException from the EL value expression. That, coupled with Eclipse flagging the EL expression as being invalid, made me think I needed a special handler of some kind.

And, of course, when I now go to get a copy of the stack trace it works perfectly - which is what I thought was supposed to happen, hence why I was so confused!

Thanks for the help, I'll check the bug reports for Eclipse. It shouldn't be flagging that EL.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic