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 A more dynamic way of accessing property values?  (Like a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "A more dynamic way of accessing property values?  (Like a "property bag"?)" Watch "A more dynamic way of accessing property values?  (Like a "property bag"?)" New topic
Author

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

Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
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.


SCJP6
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

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]}".


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

Joined: Jun 09, 2009
Posts: 327
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

Joined: Jun 09, 2009
Posts: 327
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

Joined: Jun 09, 2009
Posts: 327
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

Joined: Jun 25, 2001
Posts: 16145
    
  21

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

Joined: Jun 09, 2009
Posts: 327
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A more dynamic way of accessing property values? (Like a "property bag"?)