Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSF and the fly likes Using mapped properties with jsf forms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Using mapped properties with jsf forms" Watch "Using mapped properties with jsf forms" New topic
Author

Using mapped properties with jsf forms

Wojciech Sielski
Greenhorn

Joined: Dec 18, 2012
Posts: 7
Hi,

I'm trying to make form to modify custom user properties and set them one of provided values (0, 1, -1). To do this I've decided to use Map<Integer, Integer> when key is property identifier and value is one of 0, -1, 1. Following code is used to display form:



Displaying filled form works well but when I try to save following form my tmpPrivilege variable gets some strange values instead of one of integers (0, -1, 1). Strange thing is that setter I've made for this Map is never called. Maybe my setter should look different? How to make form like this?

Here is my setter:


I'm using primefaces is it's relevant.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

You don't code a setter for that.

Only the top-level qualifier for an EL variable reference is fetched/stored. The EL processor (which is commonly Apache BeanUtils) will then get that property and recurse on it down to the bottom-level property, where it will apply any keys or indexes.

So your backing bean only needs to supply the "getTmpPrivilege()" method, which should return the Map. BeanUtils will then invoke getPrivilegeId() on the current privilege row to get the index value, which it will look up in the tmpPrivilege map ("get" references). Or put() the selection value there (for "set" references).


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

Joined: Dec 18, 2012
Posts: 7
Ok but still values which I get after calling actionListener are all messed up. For example after checking in each row radioButton with value 1, I'm getting some random values.

getter is simple creating map key => value where value {0,-1,1}
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

Actually, I don't encourage using actionListener. Action methods are better in almost all cases. But that's not important here.

Normally at this point, I'd set debug breakpoints on the Map get/set methods. If that wasn't possible, I'd subclass the Map class and add print statements to the set/get methods just to be sure I knew what values were being set. And under which keys and when.
Wojciech Sielski
Greenhorn

Joined: Dec 18, 2012
Posts: 7
I've tried to debug this but couldn't find reason. I'll will try to subclass Map like you said.
Wojciech Sielski
Greenhorn

Joined: Dec 18, 2012
Posts: 7
Ok now I had some time for checking out what is happening with my code. Unfortunately there is something strange going on with values. Instead of using two times put method it's calling it like 4 times more. Another thing is, even if there were so many calls for put method none of them had value passed from form. It looks like it's creating Map like few times and keys are always ok but values are only (0 or 1) even if values available was only {99,-1,60}
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

It is common and proper for the get/set property methods to be invoked multiple times during the JSF lifecycle. That is why get/set methods should not have side effects and should do no heavy work.

It also sounds like you might have your backing bean defined in Request scope. Request Scope is almost totally useless in JSF, on account of the way JSF form postbacks work.
Wojciech Sielski
Greenhorn

Joined: Dec 18, 2012
Posts: 7
Ok I've found problem. It looks like Map using integer as value won;t work without proper Converter. That's why there were problem with converting String to Integer and every time I've tried to save form values were messed up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using mapped properties with jsf forms